Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ let package = Package(
.macOS(.v15)
],
dependencies: [
.package(url: "https://github.com/swift-server-community/dynamo-db-tables", from: "0.1.0")
.package(url: "https://github.com/swift-server-community/dynamo-db-tables", from: "0.1.0"),
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"),
.package(url: "https://github.com/swift-server/swift-openapi-hummingbird", from: "2.0.1"),
.package(url: "https://github.com/hummingbird-project/hummingbird", from: "2.0.0"),
.package(url: "https://github.com/tachyonics/smockable", from: "1.0.0-alpha.1"),
],
targets: [
.target(
Expand All @@ -22,9 +27,38 @@ let package = Package(
.product(name: "DynamoDBTables", package: "dynamo-db-tables"),
]
),
.target(
name: "TaskAPI",
dependencies: [
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")
],
plugins: [
.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")
]
),
.target(
name: "TaskClusterApp",
dependencies: [
"TaskAPI",
"TaskClusterModel",
.product(name: "Hummingbird", package: "hummingbird"),
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
.product(name: "OpenAPIHummingbird", package: "swift-openapi-hummingbird"),
]
),
.executableTarget(
name: "task-cluster"
),
.testTarget(
name: "TaskClusterTests",
dependencies: [
"TaskClusterApp",
"TaskClusterModel",
.product(name: "Smockable", package: "smockable"),
.product(name: "Hummingbird", package: "hummingbird"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
]
),
.testTarget(
name: "TaskClusterDynamoDBModelTests",
dependencies: [
Expand Down
1 change: 1 addition & 0 deletions Sources/TaskAPI/TaskAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Placeholder to silence SPM "no source files" warning.
7 changes: 7 additions & 0 deletions Sources/TaskAPI/openapi-generator-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
generate:
- types
- server
accessModifier: package
filter:
tags:
- Tasks
1 change: 1 addition & 0 deletions Sources/TaskAPI/openapi.yaml
25 changes: 25 additions & 0 deletions Sources/TaskClusterApp/Application+build.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Hummingbird
import Logging
import OpenAPIHummingbird // swiftlint:disable:this unused_import
import TaskClusterModel

package func buildApplication<Repository: TaskRepository>(
repository: Repository,
configuration: ApplicationConfiguration,
logger: Logger
) throws -> some ApplicationProtocol {
let router = Router(context: BasicRequestContext.self)

router.addMiddleware {
LogRequestsMiddleware(.info)
}

router.get("/health") { _, _ in
HTTPResponse.Status.ok
}

let controller = TaskController(repository: repository)
try controller.registerHandlers(on: router)

return Application(router: router, configuration: configuration, logger: logger)
}
100 changes: 100 additions & 0 deletions Sources/TaskClusterApp/TaskController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import Foundation
import TaskAPI
import TaskClusterModel

package struct TaskController<Repository: TaskRepository>: APIProtocol {
var repository: Repository

package func createTask(_ input: Operations.createTask.Input) async throws -> Operations.createTask.Output {
let body =
switch input.body {
case .json(let value): value
}

guard (1...10).contains(body.priority) else {
return .badRequest(.init())
}

let task = TaskItem(
title: body.title,
description: body.description,
priority: body.priority,
dueBy: body.dueBy
)

let created = try await repository.create(task: task)
return .created(.init(body: .json(created.toResponse())))
}

package func getTask(_ input: Operations.getTask.Input) async throws -> Operations.getTask.Output {
guard let taskId = UUID(uuidString: input.path.taskId) else {
return .notFound(.init())
}

guard let task = try await repository.get(taskId: taskId) else {
return .notFound(.init())
}

return .ok(.init(body: .json(task.toResponse())))
}

package func updateTaskPriority(
_ input: Operations.updateTaskPriority.Input
) async throws -> Operations.updateTaskPriority.Output {
let body =
switch input.body {
case .json(let value): value
}

guard (1...10).contains(body.priority) else {
return .badRequest(.init())
}

guard let taskId = UUID(uuidString: input.path.taskId) else {
return .notFound(.init())
}

guard var task = try await repository.get(taskId: taskId) else {
return .notFound(.init())
}

task.priority = body.priority
task.updatedAt = Date()
let updated = try await repository.update(task: task)
return .ok(.init(body: .json(updated.toResponse())))
}

package func cancelTask(_ input: Operations.cancelTask.Input) async throws -> Operations.cancelTask.Output {
guard let taskId = UUID(uuidString: input.path.taskId) else {
return .notFound(.init())
}

guard var task = try await repository.get(taskId: taskId) else {
return .notFound(.init())
}

guard task.status == .pending || task.status == .running else {
return .conflict(.init())
}

task.status = .cancelled
task.updatedAt = Date()
let updated = try await repository.update(task: task)
return .ok(.init(body: .json(updated.toResponse())))
}
}

extension TaskItem {
package func toResponse() -> Components.Schemas.TaskItem {
.init(
taskId: taskId.uuidString,
title: title,
description: description,
priority: priority,
dueBy: dueBy,
status: .init(rawValue: status.rawValue)!,
createdAt: createdAt,
updatedAt: updatedAt
)
}
}
11 changes: 11 additions & 0 deletions Tests/TaskClusterTests/MockTaskRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation
import Smockable

@testable import TaskClusterModel

@Smock
protocol TestTaskRepository: TaskRepository {
func create(task: TaskItem) async throws -> TaskItem
func get(taskId: UUID) async throws -> TaskItem?
func update(task: TaskItem) async throws -> TaskItem
}
Loading
Loading