Initial commit
This commit is contained in:
108
Sources/ExodaiAcademy/Infrastructure/Database/ArticleModel.swift
Normal file
108
Sources/ExodaiAcademy/Infrastructure/Database/ArticleModel.swift
Normal file
@@ -0,0 +1,108 @@
|
||||
//
|
||||
// ArticleModel.swift
|
||||
// ExodaiAcademy
|
||||
//
|
||||
// Created by Exodai on 23/01/2026.
|
||||
//
|
||||
|
||||
import Fluent
|
||||
|
||||
final class ArticleModel: Model, @unchecked Sendable {
|
||||
static let schema: String = Database.articles.rawValue
|
||||
|
||||
// MARK: - ID
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
// MARK: - CMS Fields (all optional)
|
||||
|
||||
@OptionalField(key: FieldKeys.title)
|
||||
var title: String?
|
||||
|
||||
@OptionalField(key: FieldKeys.description)
|
||||
var description: String?
|
||||
|
||||
@OptionalField(key: FieldKeys.slug)
|
||||
var slug: String?
|
||||
|
||||
@OptionalField(key: FieldKeys.excerpt)
|
||||
var excerpt: String?
|
||||
|
||||
@OptionalField(key: FieldKeys.image)
|
||||
var image: String?
|
||||
|
||||
@OptionalEnum(key: FieldKeys.status)
|
||||
var status: Status?
|
||||
|
||||
@OptionalField(key: FieldKeys.authorID)
|
||||
var authorID: UserModel.IDValue?
|
||||
|
||||
@OptionalField(key: FieldKeys.categories)
|
||||
var categories: [CategoryModel.IDValue]?
|
||||
|
||||
@OptionalField(key: FieldKeys.tags)
|
||||
var tags: [TagModel.IDValue]?
|
||||
|
||||
// MARK: - Timestamps
|
||||
|
||||
@Timestamp(key: FieldKeys.createdAt, on: .create)
|
||||
var createdAt: Date?
|
||||
|
||||
@Timestamp(key: FieldKeys.updatedAt, on: .update)
|
||||
var updatedAt: Date?
|
||||
|
||||
@OptionalField(key: FieldKeys.publishDate)
|
||||
var publishDate: Date?
|
||||
|
||||
// MARK: - Initializer
|
||||
|
||||
init() {}
|
||||
}
|
||||
|
||||
|
||||
extension ArticleModel {
|
||||
struct FieldKeys {
|
||||
static var title: FieldKey { "title" }
|
||||
static var description: FieldKey { "description" }
|
||||
static var slug: FieldKey { "slug" }
|
||||
static var excerpt: FieldKey { "excerpt" }
|
||||
static var image: FieldKey { "image" }
|
||||
static var status: FieldKey { "status" }
|
||||
static var authorID: FieldKey { "authorID" }
|
||||
static var categories: FieldKey { "categories" }
|
||||
static var tags: FieldKey { "tags" }
|
||||
static var createdAt: FieldKey { "createdAt" }
|
||||
static var updatedAt: FieldKey { "updatedAt" }
|
||||
static var publishDate: FieldKey { "publishDate" }
|
||||
}
|
||||
}
|
||||
|
||||
import Fluent
|
||||
|
||||
extension ArticleModel {
|
||||
struct Migration: AsyncMigration {
|
||||
|
||||
func prepare(on database: any FluentKit.Database) async throws {
|
||||
try await database.schema(ArticleModel.schema)
|
||||
.id()
|
||||
.field(FieldKeys.title, .string)
|
||||
.field(FieldKeys.description, .string)
|
||||
.field(FieldKeys.slug, .string)
|
||||
.field(FieldKeys.excerpt, .string)
|
||||
.field(FieldKeys.image, .string)
|
||||
.field(FieldKeys.status, .string)
|
||||
.field(FieldKeys.authorID, .uuid)
|
||||
.field(FieldKeys.categories, .array(of: .uuid))
|
||||
.field(FieldKeys.tags, .array(of: .uuid))
|
||||
.field(FieldKeys.createdAt, .datetime)
|
||||
.field(FieldKeys.updatedAt, .datetime)
|
||||
.field(FieldKeys.publishDate, .datetime)
|
||||
.create()
|
||||
}
|
||||
|
||||
func revert(on database: any FluentKit.Database) async throws {
|
||||
try await database.schema(ArticleModel.schema).delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user