Skip to content

Commit

Permalink
DTO/model conversions should be functions, not properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynne committed May 6, 2024
1 parent e3f4d4a commit 729b5da
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Sources/App/Controllers/TodoController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ struct TodoController: RouteCollection {
}

func index(req: Request) async throws -> [TodoDTO] {
try await Todo.query(on: req.db).all().map(\.dto)
try await Todo.query(on: req.db).all().map { $0.toDTO() }
}

func create(req: Request) async throws -> TodoDTO {
let todo = try req.content.decode(TodoDTO.self).model
let todo = try req.content.decode(TodoDTO.self).toModel()

try await todo.save(on: req.db)
return todo.dto
return todo.toDTO()
}

func delete(req: Request) async throws -> HTTPStatus {
Expand Down
2 changes: 1 addition & 1 deletion Sources/App/DTOs/TodoDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct TodoDTO: Content {
var id: UUID?
var title: String?

var model: Todo {
func toModel() -> Todo {
let model = Todo()

model.id = self.id
Expand Down
2 changes: 1 addition & 1 deletion Sources/App/Models/Todo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class Todo: Model, @unchecked Sendable {
self.title = title
}

var dto: TodoDTO {
func toDTO() -> TodoDTO {
.init(
id: self.id,
title: self.$title.value
Expand Down

0 comments on commit 729b5da

Please sign in to comment.