-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60afd35
commit adf65cd
Showing
18 changed files
with
492 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
Sources/Website/Controllers/Areas/Administration/FeedAdminController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import HTMLKitVapor | ||
import Vapor | ||
|
||
// [/area/admin/feed] | ||
final class FeedAdminController { | ||
|
||
// [/index] | ||
func getIndex(_ request: Request) async throws -> View { | ||
|
||
let page: Int = request.query["page"] ?? 1 | ||
|
||
let pagination = try await FeedRepository(database: request.db) | ||
.find() | ||
.map(FeedModel.Output.init) | ||
.page(page: page, per: 10) | ||
|
||
let viewModel = FeedAdminPageModel.IndexView(pagination: pagination) | ||
|
||
return try await request.htmlkit.render(FeedAdminPage.IndexView(viewModel: viewModel)) | ||
} | ||
|
||
// [/create] | ||
func getCreate(_ request: Request) async throws -> View { | ||
|
||
let viewModel = FeedAdminPageModel.CreateView() | ||
|
||
return try await request.htmlkit.render(FeedAdminPage.CreateView(viewModel: viewModel)) | ||
} | ||
|
||
// [/create/:model] | ||
func postCreate(_ request: Request) async throws -> Response { | ||
|
||
try FeedModel.Input.validate(content: request) | ||
|
||
let model = try request.content.decode(FeedModel.Input.self) | ||
|
||
try await FeedRepository(database: request.db) | ||
.insert(entity: FeedEntity(input: model)) | ||
|
||
return request.redirect(to: "/area/admin/feed/index") | ||
} | ||
|
||
// [/edit/:id] | ||
func getEdit(_ request: Request) async throws -> View { | ||
|
||
guard let id = request.parameters.get("id", as: UUID.self) else { | ||
throw Abort(.badRequest) | ||
} | ||
|
||
guard let entity = try await FeedRepository(database: request.db) | ||
.find(id: id) else { | ||
throw Abort(.notFound) | ||
} | ||
|
||
let viewModel = FeedAdminPageModel.EditView(feed: FeedModel.Output(entity: entity)) | ||
|
||
return try await request.htmlkit.render(FeedAdminPage.EditView(viewModel: viewModel)) | ||
} | ||
|
||
// [/edit/:model] | ||
func postEdit(_ request: Request) async throws -> Response { | ||
|
||
guard let id = request.parameters.get("id", as: UUID.self) else { | ||
throw Abort(.badRequest) | ||
} | ||
|
||
try FeedModel.Input.validate(content: request) | ||
|
||
let model = try request.content.decode(FeedModel.Input.self) | ||
|
||
try await FeedRepository(database: request.db) | ||
.update(entity: FeedEntity(input: model), on: id) | ||
|
||
return request.redirect(to: "/area/admin/feed/index") | ||
} | ||
|
||
// [/delete/:id] | ||
func getDelete(_ request: Request) async throws -> Response { | ||
|
||
guard let id = request.parameters.get("id", as: UUID.self) else { | ||
throw Abort(.badRequest) | ||
} | ||
|
||
try await FeedRepository(database: request.db) | ||
.delete(id: id) | ||
|
||
return request.redirect(to: "/area/admin/feed/index") | ||
} | ||
} | ||
|
||
extension FeedAdminController: RouteCollection { | ||
|
||
func boot(routes: RoutesBuilder) throws { | ||
|
||
routes.group("feed") { routes in | ||
|
||
routes.get("index", use: self.getIndex) | ||
routes.get("create", use: self.getCreate) | ||
routes.post("create", use: self.postCreate) | ||
routes.get("edit", ":id", use: self.getEdit) | ||
routes.post("edit", ":id", use: self.postEdit) | ||
routes.get("delete", ":id", use: self.getDelete) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Fluent | ||
import Foundation | ||
|
||
final class FeedEntity: Model { | ||
|
||
static let schema = "feeds" | ||
|
||
@ID(key: "id") | ||
var id: UUID? | ||
|
||
@Field(key: "message") | ||
var message: String | ||
|
||
@Timestamp(key: "created_at", on: .create) | ||
var createdAt: Date? | ||
|
||
@Timestamp(key: "modified_at", on: .update) | ||
var modifiedAt: Date? | ||
|
||
init() {} | ||
|
||
init(id: UUID? = nil, message: String, createdAt: Date? = nil, modifiedAt: Date? = nil) { | ||
|
||
self.id = id | ||
self.message = message | ||
self.createdAt = createdAt | ||
self.modifiedAt = modifiedAt | ||
} | ||
|
||
convenience init(input: FeedModel.Input) { | ||
|
||
self.init(message: input.message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Fluent | ||
|
||
struct FeedMigration: AsyncMigration { | ||
|
||
func prepare(on database: Database) async throws { | ||
|
||
try await database.schema("feeds") | ||
.id() | ||
.field("message", .string, .required) | ||
.field("created_at", .datetime) | ||
.field("modified_at", .datetime) | ||
.create() | ||
} | ||
|
||
func revert(on database: Database) async throws { | ||
|
||
try await database.schema("feeds") | ||
.delete() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Vapor | ||
import HTMLKitComponents | ||
|
||
struct FeedModel { | ||
|
||
struct Input: Content, Validatable { | ||
|
||
var message: String | ||
|
||
static func validations(_ validations: inout Validations) { | ||
|
||
validations.add("message", as: String.self, is: !.empty) | ||
} | ||
|
||
static let validators = [ | ||
Validator(field: "message", rule: .value) | ||
] | ||
} | ||
|
||
struct Output: Content { | ||
|
||
var id: UUID | ||
var message: String | ||
|
||
init(id: UUID, message: String) { | ||
|
||
self.id = id | ||
self.message = message | ||
} | ||
|
||
init(entity: FeedEntity) { | ||
|
||
self.init(id: entity.id!, message: entity.message) | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Sources/Website/Models/PageModels/FeedAdminPageModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import HTMLKitComponents | ||
|
||
enum FeedAdminPageModel { | ||
|
||
struct IndexView { | ||
var title: String = "Show feed" | ||
let pagination: Pagination<[FeedModel.Output]> | ||
} | ||
|
||
struct CreateView { | ||
var title: String = "Create feed" | ||
} | ||
|
||
struct EditView { | ||
|
||
var title: String = "Edit feed" | ||
let feed: FeedModel.Output | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Fluent | ||
import Foundation | ||
|
||
final class FeedRepository { | ||
|
||
let database: Database | ||
|
||
init(database: Database) { | ||
|
||
self.database = database | ||
} | ||
|
||
func find(id: UUID) async throws -> FeedEntity? { | ||
|
||
return try await FeedEntity.query(on: database) | ||
.filter(\.$id == id) | ||
.first() | ||
} | ||
|
||
func find() async throws -> [FeedEntity] { | ||
|
||
return try await FeedEntity.query(on: database) | ||
.sort(\.$modifiedAt, .descending) | ||
.all() | ||
} | ||
|
||
func insert(entity: FeedEntity) async throws { | ||
try await entity.create(on: database) | ||
} | ||
|
||
func patch<Field: QueryableProperty>(field: KeyPath<FeedEntity, Field>, to value: Field.Value, on id: UUID) async throws where Field.Model == FeedEntity { | ||
|
||
try await FeedEntity.query(on: database) | ||
.filter(\.$id == id) | ||
.set(field, to: value) | ||
.update() | ||
} | ||
|
||
func update(entity: FeedEntity, on id: UUID) async throws { | ||
|
||
try await FeedEntity.query(on: database) | ||
.filter(\.$id == id) | ||
.set(\.$message, to: entity.message) | ||
.update() | ||
} | ||
|
||
func delete(id: UUID) async throws { | ||
|
||
try await FeedEntity.query(on: database) | ||
.filter(\.$id == id) | ||
.delete() | ||
} | ||
|
||
func count() async throws -> Int { | ||
|
||
return try await FeedEntity.query(on: database) | ||
.count() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.