feat: Updates api controllers to use database client.
This commit is contained in:
@@ -1,64 +1,70 @@
|
|||||||
// import Dependencies
|
import DatabaseClient
|
||||||
// import Fluent
|
import Dependencies
|
||||||
// import Vapor
|
import SharedModels
|
||||||
//
|
import Vapor
|
||||||
// struct EmployeeApiController: RouteCollection {
|
|
||||||
//
|
struct EmployeeApiController: RouteCollection {
|
||||||
// @Dependency(\.employees) var employees
|
|
||||||
//
|
@Dependency(\.database.employees) var employees
|
||||||
// func boot(routes: any RoutesBuilder) throws {
|
|
||||||
// let protected = routes.apiProtected(route: "employees")
|
func boot(routes: any RoutesBuilder) throws {
|
||||||
// protected.get(use: index(req:))
|
let protected = routes.apiProtected(route: "employees")
|
||||||
// protected.post(use: create(req:))
|
protected.get(use: index(req:))
|
||||||
// protected.group(":employeeID") {
|
protected.post(use: create(req:))
|
||||||
// $0.get(use: get(req:))
|
protected.group(":id") {
|
||||||
// $0.put(use: update(req:))
|
$0.get(use: get(req:))
|
||||||
// $0.delete(use: delete(req:))
|
$0.put(use: update(req:))
|
||||||
// }
|
$0.delete(use: delete(req:))
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// @Sendable
|
|
||||||
// func index(req: Request) async throws -> [Employee.DTO] {
|
@Sendable
|
||||||
// let params = try req.query.decode(EmployeesIndexQuery.self)
|
func index(req: Request) async throws -> [Employee] {
|
||||||
// return try await employees.fetchAll(params.active == true ? .active : .default)
|
let params = try req.query.decode(EmployeesIndexQuery.self)
|
||||||
// }
|
return try await employees.fetchAll(params.request)
|
||||||
//
|
}
|
||||||
// @Sendable
|
|
||||||
// func create(req: Request) async throws -> Employee.DTO {
|
@Sendable
|
||||||
// try await employees.create(
|
func create(req: Request) async throws -> Employee {
|
||||||
// req.ensureValidContent(Employee.Create.self)
|
try await employees.create(
|
||||||
// )
|
req.content.decode(Employee.Create.self)
|
||||||
// }
|
)
|
||||||
//
|
}
|
||||||
// @Sendable
|
|
||||||
// func get(req: Request) async throws -> Employee.DTO {
|
@Sendable
|
||||||
// guard let id = req.parameters.get("employeeID", as: Employee.IDValue.self),
|
func get(req: Request) async throws -> Employee {
|
||||||
// let employee = try await employees.get(id)
|
guard let employee = try await employees.get(req.ensureIDPathComponent()) else {
|
||||||
// else {
|
throw Abort(.notFound)
|
||||||
// throw Abort(.notFound)
|
}
|
||||||
// }
|
return employee
|
||||||
// return employee
|
}
|
||||||
// }
|
|
||||||
//
|
@Sendable
|
||||||
// @Sendable
|
func update(req: Request) async throws -> Employee {
|
||||||
// func update(req: Request) async throws -> Employee.DTO {
|
return try await employees.update(
|
||||||
// guard let employeeID = req.parameters.get("employeeID", as: Employee.IDValue.self) else {
|
req.ensureIDPathComponent(),
|
||||||
// throw Abort(.badRequest, reason: "Employee id value not provided")
|
req.content.decode(Employee.Update.self)
|
||||||
// }
|
)
|
||||||
// let updates = try req.ensureValidContent(Employee.Update.self)
|
}
|
||||||
// return try await employees.update(employeeID, updates)
|
|
||||||
// }
|
@Sendable
|
||||||
//
|
func delete(req: Request) async throws -> HTTPStatus {
|
||||||
// @Sendable
|
try await employees.delete(req.ensureIDPathComponent())
|
||||||
// func delete(req: Request) async throws -> HTTPStatus {
|
return .ok
|
||||||
// guard let employeeID = req.parameters.get("employeeID", as: Employee.IDValue.self) else {
|
}
|
||||||
// throw Abort(.badRequest, reason: "Employee id value not provided")
|
}
|
||||||
// }
|
|
||||||
// try await employees.delete(employeeID)
|
struct EmployeesIndexQuery: Content {
|
||||||
// return .ok
|
let active: Bool?
|
||||||
// }
|
|
||||||
// }
|
var request: DatabaseClient.Employees.FetchRequest {
|
||||||
//
|
switch active {
|
||||||
// struct EmployeesIndexQuery: Content {
|
case .none:
|
||||||
// let active: Bool?
|
return .all
|
||||||
// }
|
case .some(true):
|
||||||
|
return .active
|
||||||
|
case .some(false):
|
||||||
|
return .inactive
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,61 +1,59 @@
|
|||||||
// import Dependencies
|
import DatabaseClient
|
||||||
// import Fluent
|
import Dependencies
|
||||||
// import Vapor
|
import Fluent
|
||||||
//
|
import SharedModels
|
||||||
// // TODO: Add update route.
|
import Vapor
|
||||||
//
|
|
||||||
// struct PurchaseOrderApiController: RouteCollection {
|
// TODO: Add update route.
|
||||||
//
|
|
||||||
// @Dependency(\.purchaseOrders) var purchaseOrders
|
struct PurchaseOrderApiController: RouteCollection {
|
||||||
//
|
|
||||||
// func boot(routes: any RoutesBuilder) throws {
|
@Dependency(\.database.purchaseOrders) var purchaseOrders
|
||||||
// let protected = routes.apiProtected(route: "purchase-orders")
|
|
||||||
// protected.get(use: index(req:))
|
func boot(routes: any RoutesBuilder) throws {
|
||||||
// protected.post(use: create(req:))
|
let protected = routes.apiProtected(route: "purchase-orders")
|
||||||
// protected.group(":id") {
|
protected.get(use: index(req:))
|
||||||
// $0.get(use: get(req:))
|
protected.post(use: create(req:))
|
||||||
// $0.delete(use: delete(req:))
|
protected.group(":id") {
|
||||||
// }
|
$0.get(use: get(req:))
|
||||||
// }
|
$0.delete(use: delete(req:))
|
||||||
//
|
}
|
||||||
// @Sendable
|
}
|
||||||
// func index(req: Request) async throws -> [PurchaseOrder.DTO] {
|
|
||||||
// try await purchaseOrders.fetchAll()
|
@Sendable
|
||||||
// }
|
func index(req: Request) async throws -> [PurchaseOrder] {
|
||||||
//
|
try await purchaseOrders.fetchAll()
|
||||||
// @Sendable
|
}
|
||||||
// func create(req: Request) async throws -> PurchaseOrder.DTO {
|
|
||||||
// try await purchaseOrders.create(
|
@Sendable
|
||||||
// req.ensureValidContent(PurchaseOrder.Create.self),
|
func create(req: Request) async throws -> PurchaseOrder {
|
||||||
// req.auth.require(User.self).requireID()
|
try await purchaseOrders.create(
|
||||||
// )
|
req.content.decode(PurchaseOrder.Create.self),
|
||||||
// }
|
req.auth.require(User.self).id
|
||||||
//
|
)
|
||||||
// @Sendable
|
}
|
||||||
// func get(req: Request) async throws -> PurchaseOrder.DTO {
|
|
||||||
// guard let id = req.parameters.get("id", as: PurchaseOrder.IDValue.self),
|
@Sendable
|
||||||
// let purchaseOrder = try await purchaseOrders.get(id)
|
func get(req: Request) async throws -> PurchaseOrder {
|
||||||
// else {
|
guard let purchaseOrder = try await purchaseOrders.get(req.ensureIDPathComponent(as: Int.self))
|
||||||
// throw Abort(.notFound)
|
else {
|
||||||
// }
|
throw Abort(.notFound)
|
||||||
// return purchaseOrder
|
}
|
||||||
// }
|
return purchaseOrder
|
||||||
//
|
}
|
||||||
// @Sendable
|
|
||||||
// func delete(req: Request) async throws -> HTTPStatus {
|
@Sendable
|
||||||
// guard let id = req.parameters.get("id", as: PurchaseOrder.IDValue.self) else {
|
func delete(req: Request) async throws -> HTTPStatus {
|
||||||
// throw Abort(.badRequest, reason: "Purchase order id not provided.")
|
try await purchaseOrders.delete(req.ensureIDPathComponent(as: Int.self))
|
||||||
// }
|
return .ok
|
||||||
// try await purchaseOrders.delete(id)
|
}
|
||||||
// return .ok
|
|
||||||
// }
|
// @Sendable
|
||||||
//
|
// func update(req: Request) async throws -> PurchaseOrder.DTO {
|
||||||
// // @Sendable
|
// guard let id = req.parameters.get("id", as: PurchaseOrder.ID.self) else {
|
||||||
// // func update(req: Request) async throws -> PurchaseOrder.DTO {
|
// throw Abort(.badRequest, reason: "Purchase order id not provided.")
|
||||||
// // guard let id = req.parameters.get("id", as: PurchaseOrder.IDValue.self) else {
|
// }
|
||||||
// // throw Abort(.badRequest, reason: "Purchase order id not provided.")
|
// try await purchaseOrders.delete(id: id, on: req.db)
|
||||||
// // }
|
// return .ok
|
||||||
// // try await purchaseOrders.delete(id: id, on: req.db)
|
// }
|
||||||
// // return .ok
|
}
|
||||||
// // }
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ struct UserApiController: RouteCollection {
|
|||||||
@Sendable
|
@Sendable
|
||||||
func create(req: Request) async throws -> User {
|
func create(req: Request) async throws -> User {
|
||||||
// Allow the first user to be created without authentication.
|
// Allow the first user to be created without authentication.
|
||||||
// let count = try await User.query(on: req.db).count()
|
|
||||||
let count = try await users.count()
|
let count = try await users.count()
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
guard req.auth.get(User.self) != nil else {
|
guard req.auth.get(User.self) != nil else {
|
||||||
@@ -40,25 +39,14 @@ struct UserApiController: RouteCollection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
func login(req: Request) async throws -> User {
|
func login(req: Request) async throws -> User.Token {
|
||||||
let user = try req.auth.require(User.self)
|
let user = try req.auth.require(User.self)
|
||||||
return user
|
return try await users.token(user.id)
|
||||||
// return try await users.login(user)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Sendable
|
|
||||||
// func get(req: Request) async throws -> User.DTO {
|
|
||||||
// guard let id = req.parameters.get("id", as: User.IDValue.self),
|
|
||||||
// let user = users.
|
|
||||||
// }
|
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
func delete(req: Request) async throws -> HTTPStatus {
|
func delete(req: Request) async throws -> HTTPStatus {
|
||||||
// guard let id = req.parameters.get("id", as: User.IDValue.self) else {
|
try await users.delete(req.ensureIDPathComponent())
|
||||||
// throw Abort(.badRequest, reason: "User id not provided")
|
|
||||||
// }
|
|
||||||
let id = try req.ensureIDPathComponent()
|
|
||||||
try await users.delete(id)
|
|
||||||
return .ok
|
return .ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,57 +1,51 @@
|
|||||||
// import Dependencies
|
import DatabaseClient
|
||||||
// import Fluent
|
import Dependencies
|
||||||
// import Vapor
|
import Fluent
|
||||||
//
|
import SharedModels
|
||||||
// struct VendorApiController: RouteCollection {
|
import Vapor
|
||||||
//
|
|
||||||
// @Dependency(\.vendors) var vendors
|
struct VendorApiController: RouteCollection {
|
||||||
//
|
|
||||||
// func boot(routes: any RoutesBuilder) throws {
|
@Dependency(\.database.vendors) var vendors
|
||||||
// let protected = routes.apiProtected(route: "vendors")
|
|
||||||
// protected.get(use: index(req:))
|
func boot(routes: any RoutesBuilder) throws {
|
||||||
// protected.post(use: create(req:))
|
let protected = routes.apiProtected(route: "vendors")
|
||||||
// protected.group(":id") {
|
protected.get(use: index(req:))
|
||||||
// $0.put(use: update(req:))
|
protected.post(use: create(req:))
|
||||||
// $0.delete(use: delete(req:))
|
protected.group(":id") {
|
||||||
// }
|
$0.put(use: update(req:))
|
||||||
// }
|
$0.delete(use: delete(req:))
|
||||||
//
|
}
|
||||||
// @Sendable
|
}
|
||||||
// func index(req: Request) async throws -> [Vendor.DTO] {
|
|
||||||
// let params = try req.query.decode(VendorsIndexQuery.self)
|
@Sendable
|
||||||
// return try await vendors.fetchAll(params.fetchRequest)
|
func index(req: Request) async throws -> [Vendor] {
|
||||||
// }
|
let params = try req.query.decode(VendorsIndexQuery.self)
|
||||||
//
|
return try await vendors.fetchAll(params.request)
|
||||||
// @Sendable
|
}
|
||||||
// func create(req: Request) async throws -> Vendor.DTO {
|
|
||||||
// try await vendors.create(req.ensureValidContent(Vendor.Create.self))
|
@Sendable
|
||||||
// }
|
func create(req: Request) async throws -> Vendor {
|
||||||
//
|
try await vendors.create(req.content.decode(Vendor.Create.self))
|
||||||
// @Sendable
|
}
|
||||||
// func update(req: Request) async throws -> Vendor.DTO {
|
|
||||||
// guard let id = req.parameters.get("id", as: Vendor.IDValue.self) else {
|
@Sendable
|
||||||
// throw Abort(.badRequest, reason: "Vendor id not provided.")
|
func update(req: Request) async throws -> Vendor {
|
||||||
// }
|
return try await vendors.update(req.ensureIDPathComponent(), req.content.decode(Vendor.Update.self))
|
||||||
// try Vendor.Update.validate(content: req)
|
}
|
||||||
// let updates = try req.content.decode(Vendor.Update.self)
|
|
||||||
// return try await vendors.update(id, updates)
|
@Sendable
|
||||||
// }
|
func delete(req: Request) async throws -> HTTPStatus {
|
||||||
//
|
try await vendors.delete(req.ensureIDPathComponent())
|
||||||
// @Sendable
|
return .ok
|
||||||
// func delete(req: Request) async throws -> HTTPStatus {
|
}
|
||||||
// guard let id = req.parameters.get("id", as: Vendor.IDValue.self) else {
|
}
|
||||||
// throw Abort(.badRequest, reason: "Vendor id not provided.")
|
|
||||||
// }
|
struct VendorsIndexQuery: Content {
|
||||||
// try await vendors.delete(id)
|
let branches: Bool?
|
||||||
// return .ok
|
|
||||||
// }
|
var request: DatabaseClient.Vendors.FetchRequest {
|
||||||
// }
|
if branches == true { return .withBranches }
|
||||||
//
|
return .all
|
||||||
// struct VendorsIndexQuery: Content {
|
}
|
||||||
// let branches: Bool?
|
}
|
||||||
//
|
|
||||||
// var fetchRequest: VendorDB.FetchRequest {
|
|
||||||
// if branches == true { return .withBranches }
|
|
||||||
// return .default
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -1,67 +1,65 @@
|
|||||||
// import Dependencies
|
import DatabaseClient
|
||||||
// import Fluent
|
import Dependencies
|
||||||
// import Vapor
|
import Fluent
|
||||||
//
|
import SharedModels
|
||||||
// struct VendorBranchApiController: RouteCollection {
|
import Vapor
|
||||||
//
|
|
||||||
// @Dependency(\.vendorBranches) var vendorBranches
|
struct VendorBranchApiController: RouteCollection {
|
||||||
//
|
|
||||||
// func boot(routes: any RoutesBuilder) throws {
|
@Dependency(\.database.vendorBranches) var vendorBranches
|
||||||
// let prefix = routes.apiProtected(route: "vendors")
|
|
||||||
// let root = prefix.grouped("branches")
|
func boot(routes: any RoutesBuilder) throws {
|
||||||
// root.get(use: index(req:))
|
let prefix = routes.apiProtected(route: "vendors")
|
||||||
// root.group(":id") {
|
let root = prefix.grouped("branches")
|
||||||
// $0.put(use: update(req:))
|
root.get(use: index(req:))
|
||||||
// $0.delete(use: delete(req:))
|
root.group(":id") {
|
||||||
// }
|
$0.put(use: update(req:))
|
||||||
//
|
$0.delete(use: delete(req:))
|
||||||
// prefix.group(":vendorID", "branches") {
|
}
|
||||||
// $0.get(use: indexForVendor(req:))
|
prefix.group(":vendorID", "branches") {
|
||||||
// $0.post(use: create(req:))
|
$0.get(use: indexForVendor(req:))
|
||||||
// }
|
$0.post(use: create(req:))
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// @Sendable
|
|
||||||
// func index(req: Request) async throws -> [VendorBranch.DTO] {
|
@Sendable
|
||||||
// try await vendorBranches.fetchAll()
|
func index(req: Request) async throws -> [VendorBranch] {
|
||||||
// }
|
try await vendorBranches.fetchAll()
|
||||||
//
|
}
|
||||||
// @Sendable
|
|
||||||
// func indexForVendor(req: Request) async throws -> [VendorBranch.DTO] {
|
@Sendable
|
||||||
// guard let id = req.parameters.get("vendorID", as: Vendor.IDValue.self) else {
|
func indexForVendor(req: Request) async throws -> [VendorBranch] {
|
||||||
// throw Abort(.badRequest, reason: "Vendor id not provided.")
|
guard let id = req.parameters.get("vendorID", as: Vendor.ID.self) else {
|
||||||
// }
|
throw Abort(.badRequest, reason: "Vendor id not provided.")
|
||||||
// return try await vendorBranches.fetchAll(.for(vendorID: id))
|
}
|
||||||
// }
|
return try await vendorBranches.fetchAll(.for(vendorID: id))
|
||||||
//
|
}
|
||||||
// @Sendable
|
|
||||||
// func create(req: Request) async throws -> VendorBranch.DTO {
|
@Sendable
|
||||||
// guard let id = req.parameters.get("vendorID", as: Vendor.IDValue.self) else {
|
func create(req: Request) async throws -> VendorBranch {
|
||||||
// throw Abort(.badRequest, reason: "Vendor id not provided.")
|
let id = try req.ensureIDPathComponent(key: "vendorID")
|
||||||
// }
|
let content = try req.content.decode(BranchCreateRequest.self)
|
||||||
// return try await vendorBranches.create(
|
return try await vendorBranches.create(
|
||||||
// req.ensureValidContent(VendorBranch.Create.self),
|
.init(name: content.name, vendorID: id)
|
||||||
// id
|
)
|
||||||
// )
|
}
|
||||||
// }
|
|
||||||
//
|
@Sendable
|
||||||
// @Sendable
|
func update(req: Request) async throws -> VendorBranch {
|
||||||
// func update(req: Request) async throws -> VendorBranch.DTO {
|
return try await vendorBranches.update(
|
||||||
// guard let id = req.parameters.get("id", as: VendorBranch.IDValue.self) else {
|
req.ensureIDPathComponent(),
|
||||||
// throw Abort(.badRequest, reason: "Vendor branch id not provided.")
|
req.content.decode(VendorBranch.Update.self)
|
||||||
// }
|
)
|
||||||
// try VendorBranch.Update.validate(content: req)
|
}
|
||||||
// let updates = try req.content.decode(VendorBranch.Update.self)
|
|
||||||
// return try await vendorBranches.update(id, updates)
|
@Sendable
|
||||||
// }
|
func delete(req: Request) async throws -> HTTPStatus {
|
||||||
//
|
try await vendorBranches.delete(req.ensureIDPathComponent())
|
||||||
// @Sendable
|
return .ok
|
||||||
// func delete(req: Request) async throws -> HTTPStatus {
|
}
|
||||||
// guard let id = req.parameters.get("id", as: VendorBranch.IDValue.self) else {
|
|
||||||
// throw Abort(.badRequest, reason: "Vendor branch id not provided.")
|
}
|
||||||
// }
|
|
||||||
// try await vendorBranches.delete(id)
|
private struct BranchCreateRequest: Content {
|
||||||
// return .ok
|
let name: String
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ import Vapor
|
|||||||
|
|
||||||
struct ApiController: RouteCollection {
|
struct ApiController: RouteCollection {
|
||||||
func boot(routes: any RoutesBuilder) throws {
|
func boot(routes: any RoutesBuilder) throws {
|
||||||
// try routes.register(collection: EmployeeApiController())
|
try routes.register(collection: EmployeeApiController())
|
||||||
// try routes.register(collection: PurchaseOrderApiController())
|
try routes.register(collection: PurchaseOrderApiController())
|
||||||
try routes.register(collection: UserApiController())
|
try routes.register(collection: UserApiController())
|
||||||
// try routes.register(collection: VendorApiController())
|
try routes.register(collection: VendorApiController())
|
||||||
// try routes.register(collection: VendorBranchApiController())
|
try routes.register(collection: VendorBranchApiController())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ extension RoutesBuilder {
|
|||||||
// return self
|
// return self
|
||||||
// #else
|
// #else
|
||||||
return grouped(
|
return grouped(
|
||||||
// User.credentialsAuthenticator(),
|
|
||||||
UserPasswordAuthenticator(),
|
UserPasswordAuthenticator(),
|
||||||
UserTokenAuthenticator(),
|
UserTokenAuthenticator(),
|
||||||
UserSessionAuthenticator(),
|
UserSessionAuthenticator(),
|
||||||
@@ -36,8 +35,6 @@ extension RoutesBuilder {
|
|||||||
return prefixed.grouped(
|
return prefixed.grouped(
|
||||||
UserPasswordAuthenticator(),
|
UserPasswordAuthenticator(),
|
||||||
UserTokenAuthenticator(),
|
UserTokenAuthenticator(),
|
||||||
// User.authenticator(),
|
|
||||||
// UserToken.authenticator(),
|
|
||||||
User.guardMiddleware()
|
User.guardMiddleware()
|
||||||
)
|
)
|
||||||
// #endif
|
// #endif
|
||||||
|
|||||||
@@ -9,10 +9,19 @@ import Vapor
|
|||||||
|
|
||||||
// configures your application
|
// configures your application
|
||||||
public func configure(_ app: Application) async throws {
|
public func configure(_ app: Application) async throws {
|
||||||
|
// cors middleware should come before default error middleware using `at: .beginning`
|
||||||
|
let corsConfiguration = CORSMiddleware.Configuration(
|
||||||
|
allowedOrigin: .all,
|
||||||
|
allowedMethods: [.GET, .POST, .PUT, .OPTIONS, .DELETE, .PATCH],
|
||||||
|
allowedHeaders: [.accept, .authorization, .contentType, .origin,
|
||||||
|
.xRequestedWith, .userAgent, .accessControlAllowOrigin]
|
||||||
|
)
|
||||||
|
let cors = CORSMiddleware(configuration: corsConfiguration)
|
||||||
|
app.middleware.use(cors, at: .beginning)
|
||||||
|
|
||||||
// uncomment to serve files from /Public folder
|
// uncomment to serve files from /Public folder
|
||||||
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
|
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
|
||||||
app.middleware.use(app.sessions.middleware)
|
app.middleware.use(app.sessions.middleware)
|
||||||
// app.middleware.use(User.sessionAuthenticator())
|
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
app.lifecycle.use(BrowserSyncHandler())
|
app.lifecycle.use(BrowserSyncHandler())
|
||||||
@@ -28,22 +37,10 @@ public func configure(_ app: Application) async throws {
|
|||||||
let databaseClient = DatabaseClient.live(database: app.db)
|
let databaseClient = DatabaseClient.live(database: app.db)
|
||||||
try await app.migrations.add(databaseClient.migrations())
|
try await app.migrations.add(databaseClient.migrations())
|
||||||
|
|
||||||
// app.migrations.add(Vendor.Migrate())
|
|
||||||
// app.migrations.add(VendorBranch.Migrate())
|
|
||||||
// app.migrations.add(Employee.Migrate())
|
|
||||||
// app.migrations.add(User.Migrate())
|
|
||||||
// app.migrations.add(UserToken.Migrate())
|
|
||||||
// app.migrations.add(PurchaseOrder.Migrate())
|
|
||||||
|
|
||||||
app.views.use(.leaf)
|
app.views.use(.leaf)
|
||||||
|
|
||||||
try withDependencies {
|
try withDependencies {
|
||||||
$0.database = databaseClient
|
$0.database = databaseClient
|
||||||
// $0.employees = .live(database: app.db(.sqlite))
|
|
||||||
// $0.purchaseOrders = .live(database: app.db(.sqlite))
|
|
||||||
// $0.users = .live(database: app.db(.sqlite))
|
|
||||||
// $0.vendorBranches = .live(database: app.db(.sqlite))
|
|
||||||
// $0.vendors = .live(database: app.db(.sqlite))
|
|
||||||
} operation: {
|
} operation: {
|
||||||
// register routes
|
// register routes
|
||||||
try routes(app)
|
try routes(app)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import Dependencies
|
import Dependencies
|
||||||
import DependenciesMacros
|
import DependenciesMacros
|
||||||
import SharedModels
|
import SharedModels
|
||||||
|
import Vapor
|
||||||
|
|
||||||
public extension DatabaseClient {
|
public extension DatabaseClient {
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ public extension DatabaseClient {
|
|||||||
try await fetchAll(.all)
|
try await fetchAll(.all)
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum FetchRequest {
|
public enum FetchRequest: String, Content {
|
||||||
case active
|
case active
|
||||||
case all
|
case all
|
||||||
case inactive
|
case inactive
|
||||||
@@ -24,6 +25,10 @@ public extension DatabaseClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Employee: Content {}
|
||||||
|
extension Employee.Create: Content {}
|
||||||
|
extension Employee.Update: Content {}
|
||||||
|
|
||||||
extension DatabaseClient.Employees: TestDependencyKey {
|
extension DatabaseClient.Employees: TestDependencyKey {
|
||||||
public static let testValue = Self()
|
public static let testValue = Self()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import Dependencies
|
|||||||
import DependenciesMacros
|
import DependenciesMacros
|
||||||
import Fluent
|
import Fluent
|
||||||
import SharedModels
|
import SharedModels
|
||||||
|
import Vapor
|
||||||
|
|
||||||
public extension DatabaseClient {
|
public extension DatabaseClient {
|
||||||
@DependencyClient
|
@DependencyClient
|
||||||
@@ -15,6 +16,9 @@ public extension DatabaseClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension PurchaseOrder: Content {}
|
||||||
|
extension PurchaseOrder.Create: Content {}
|
||||||
|
|
||||||
extension DatabaseClient.PurchaseOrders: TestDependencyKey {
|
extension DatabaseClient.PurchaseOrders: TestDependencyKey {
|
||||||
public static let testValue: DatabaseClient.PurchaseOrders = Self()
|
public static let testValue: DatabaseClient.PurchaseOrders = Self()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,14 +15,13 @@ public extension DatabaseClient {
|
|||||||
public var get: @Sendable (User.ID) async throws -> User?
|
public var get: @Sendable (User.ID) async throws -> User?
|
||||||
public var login: @Sendable (User.Login) async throws -> User.Token
|
public var login: @Sendable (User.Login) async throws -> User.Token
|
||||||
public var logout: @Sendable (User.Token.ID) async throws -> Void
|
public var logout: @Sendable (User.Token.ID) async throws -> Void
|
||||||
|
public var token: @Sendable (User.ID) async throws -> User.Token
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension DatabaseClient.Users {
|
extension User: Content {}
|
||||||
enum AuthRequest {
|
extension User.Create: Content {}
|
||||||
case basic(BasicAuthorization)
|
extension User.Token: Content {}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension DatabaseClient.Users: TestDependencyKey {
|
extension DatabaseClient.Users: TestDependencyKey {
|
||||||
public static let testValue: DatabaseClient.Users = Self()
|
public static let testValue: DatabaseClient.Users = Self()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import Dependencies
|
import Dependencies
|
||||||
import DependenciesMacros
|
import DependenciesMacros
|
||||||
import SharedModels
|
import SharedModels
|
||||||
|
import Vapor
|
||||||
|
|
||||||
public extension DatabaseClient {
|
public extension DatabaseClient {
|
||||||
@DependencyClient
|
@DependencyClient
|
||||||
@@ -23,6 +24,11 @@ public extension DatabaseClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension VendorBranch: Content {}
|
||||||
|
extension VendorBranch.Create: Content {}
|
||||||
|
extension VendorBranch.Update: Content {}
|
||||||
|
extension DatabaseClient.VendorBranches.FetchRequest: Content {}
|
||||||
|
|
||||||
extension DatabaseClient.VendorBranches: TestDependencyKey {
|
extension DatabaseClient.VendorBranches: TestDependencyKey {
|
||||||
public static let testValue: DatabaseClient.VendorBranches = Self()
|
public static let testValue: DatabaseClient.VendorBranches = Self()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import Dependencies
|
import Dependencies
|
||||||
import DependenciesMacros
|
import DependenciesMacros
|
||||||
import SharedModels
|
import SharedModels
|
||||||
|
import Vapor
|
||||||
|
|
||||||
public extension DatabaseClient {
|
public extension DatabaseClient {
|
||||||
@DependencyClient
|
@DependencyClient
|
||||||
@@ -31,6 +32,12 @@ public extension DatabaseClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Vendor: Content {}
|
||||||
|
extension Vendor.Create: Content {}
|
||||||
|
extension Vendor.Update: Content {}
|
||||||
|
extension DatabaseClient.Vendors.FetchRequest: Content {}
|
||||||
|
extension DatabaseClient.Vendors.GetRequest: Content {}
|
||||||
|
|
||||||
extension DatabaseClient.Vendors: TestDependencyKey {
|
extension DatabaseClient.Vendors: TestDependencyKey {
|
||||||
public static let testValue: DatabaseClient.Vendors = Self()
|
public static let testValue: DatabaseClient.Vendors = Self()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import DatabaseClient
|
|||||||
import Fluent
|
import Fluent
|
||||||
import Foundation
|
import Foundation
|
||||||
import SharedModels
|
import SharedModels
|
||||||
|
import Vapor
|
||||||
|
|
||||||
public extension DatabaseClient.Employees {
|
public extension DatabaseClient.Employees {
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,21 @@ public extension DatabaseClient.Users {
|
|||||||
guard let token = try await UserTokenModel.find(id, on: database)
|
guard let token = try await UserTokenModel.find(id, on: database)
|
||||||
else { return }
|
else { return }
|
||||||
try await token.delete(on: database)
|
try await token.delete(on: database)
|
||||||
|
} token: { _ in
|
||||||
|
guard let user = try await UserModel.query(on: database)
|
||||||
|
.with(\.$token)
|
||||||
|
.first()
|
||||||
|
else {
|
||||||
|
throw Abort(.notFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let token = user.token else {
|
||||||
|
let token = try user.generateToken()
|
||||||
|
try await token.save(on: database)
|
||||||
|
return try token.toDTO()
|
||||||
|
}
|
||||||
|
|
||||||
|
return try token.toDTO()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,6 +171,9 @@ final class UserModel: Model, @unchecked Sendable {
|
|||||||
@Timestamp(key: "updated_at", on: .update)
|
@Timestamp(key: "updated_at", on: .update)
|
||||||
var updatedAt: Date?
|
var updatedAt: Date?
|
||||||
|
|
||||||
|
@OptionalChild(for: \.$user)
|
||||||
|
var token: UserTokenModel?
|
||||||
|
|
||||||
init() {}
|
init() {}
|
||||||
|
|
||||||
init(
|
init(
|
||||||
@@ -210,6 +228,10 @@ final class UserTokenModel: Model, Codable, @unchecked Sendable {
|
|||||||
$user.id = userID
|
$user.id = userID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func toDTO() throws -> User.Token {
|
||||||
|
try .init(id: requireID(), userID: $user.id, value: value)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Authentication
|
// MARK: - Authentication
|
||||||
@@ -219,8 +241,6 @@ extension User: SessionAuthenticatable {
|
|||||||
public var sessionID: String { username }
|
public var sessionID: String { username }
|
||||||
}
|
}
|
||||||
|
|
||||||
extension User: Content {}
|
|
||||||
|
|
||||||
public struct UserPasswordAuthenticator: AsyncBasicAuthenticator {
|
public struct UserPasswordAuthenticator: AsyncBasicAuthenticator {
|
||||||
public typealias User = SharedModels.User
|
public typealias User = SharedModels.User
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import DatabaseClient
|
|||||||
import FluentKit
|
import FluentKit
|
||||||
import Foundation
|
import Foundation
|
||||||
import SharedModels
|
import SharedModels
|
||||||
|
import Vapor
|
||||||
|
|
||||||
public extension DatabaseClient.Vendors {
|
public extension DatabaseClient.Vendors {
|
||||||
|
|
||||||
|
|||||||
@@ -41,14 +41,3 @@ public extension Vendor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// public extension Vendor {
|
|
||||||
//
|
|
||||||
// static var mocks: [Self] {
|
|
||||||
// [
|
|
||||||
// .init(name: "Corken"),
|
|
||||||
// .init(name: "Johnstone"),
|
|
||||||
// .init(name: "Winstel Controls")
|
|
||||||
// ]
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
Reference in New Issue
Block a user