Skip to content

Commit

Permalink
Add posts migration and relation
Browse files Browse the repository at this point in the history
  • Loading branch information
BoogieMonster1O1 committed Jan 5, 2024
1 parent 452e725 commit 459fc9d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions Sources/App/Migrations/002_CreateRegisteredUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct CreateRegisteredUser: Migration {
.field("id", .int, .custom("GENERATED ALWAYS AS IDENTITY"))
.unique(on: "collegeId")
.unique(on: "email")
.unique(on: "id")
.create()
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/App/Migrations/004_CreatePosts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct CreatePosts: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
return database.schema("posts")
.field("id", .string, .required)
.field("user", .int, .required, .references("registeredUsers", "regNo"))
.field("userId", .int, .required, .references("registeredUsers", "id"))
.field("content", .string, .required)
.field("created_at", .datetime, .required)
.field("deleted", .bool, .required)
Expand Down
8 changes: 4 additions & 4 deletions Sources/App/Models/Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ final class Post: Model, Content {
@ID(custom: "id", generatedBy: .user)
var id: String?

@Field(key: "user")
var user: Int
@Parent(key: "userId")
var user: RegisteredUser

@Field(key: "content")
var content: String
Expand All @@ -30,9 +30,9 @@ final class Post: Model, Content {
}

// TODO: attachments (media), comments enabled, edited
init(id: String, regNo user: Int, content: String) {
init(id: String, userId: Int, content: String) {
self.id = id
self.user = user
self.$user.id = userId
self.content = content
self.deleted = false
}
Expand Down
1 change: 1 addition & 0 deletions Sources/App/configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public func configure(_ app: Application) async throws {
app.migrations.add(CreateUser())
app.migrations.add(CreateRegisteredUser())
app.migrations.add(CreateUserAuth())
app.migrations.add(CreatePosts())

app.jwt.google.gSuiteDomainName = AppConfig.googleWorkspaceDomain
app.jwt.signers.use(.hs256(key: AppConfig.jwtSigningKey))
Expand Down

0 comments on commit 459fc9d

Please sign in to comment.