Last active
November 4, 2020 18:25
-
-
Save saroar/9e2ba6ae0701d93d288b2a84a0bd8bf8 to your computer and use it in GitHub Desktop.
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
public final class User: Model, Content, Hashable { | |
public static var schema = "users" | |
public init() {} | |
public init(id: ObjectId? = nil ,phoneNumber: String) { | |
self.id = id | |
self.phoneNumber = phoneNumber | |
self.firstName = nil | |
self.lastName = nil | |
} | |
@ID(custom: "id") public var id: ObjectId? | |
@Field(key: "phoneNumber") public var phoneNumber: String | |
@OptionalField(key: "firstName") public var firstName: String? | |
@OptionalField(key: "lastName") public var lastName: String? | |
@OptionalField(key: "email") public var email: String? | |
@Children(for: \.$owner) public var event: [Event] | |
@Children(for: \.$sender) public var sender: [Message] | |
@Children(for: \.$recipient) public var recipient: [Message] | |
@Siblings(through: UserConversation.self, from: \.$member, to: \.$conversation) | |
public var membersConversaions: [Conversation] | |
@Siblings(through: UserConversation.self, from: \.$admin, to: \.$conversation) | |
public var adminsConversations: [Conversation] | |
@Timestamp(key: "createdAt", on: .create) public var createdAt: Date? | |
@Timestamp(key: "updatedAt", on: .update) public var updatedAt: Date? | |
@Timestamp(key: "deletedAt", on: .delete) public var deletedAt: Date? | |
public func hash(into hasher: inout Hasher) { | |
hasher.combine(id) | |
} | |
public static func == (lhs: User, rhs: User) -> Bool { | |
lhs.id == rhs.id | |
} | |
} | |
public final class Conversation: Model, Content { | |
public static var schema = "conversations" | |
public init() {} | |
public init(title: String) { | |
self.title = title | |
} | |
@ID(custom: "id") public var id: ObjectId? | |
@Field(key: "title") public var title: String | |
@Children(for: \.$conversation) public var events: [Event] | |
@Children(for: \.$conversation) public var messages: [Message] | |
@Siblings(through: UserConversation.self, from: \.$conversation, to: \.$member) | |
public var members: [User] | |
@Siblings(through: UserConversation.self, from: \.$conversation, to: \.$admin) | |
public var admins: [User] | |
@Timestamp(key: "createdAt", on: .create) public var createdAt: Date? | |
@Timestamp(key: "updatedAt", on: .update) public var updatedAt: Date? | |
@Timestamp(key: "deletedAt", on: .delete) public var deletedAt: Date? | |
public func addUser(userId: ObjectId, req: Request) { | |
User.find(userId, on: req.db) | |
.unwrap(or: Abort(.notFound, reason: "Cant find user") ) | |
.flatMap { user in | |
self.$admins.attach(user, on: req.db).flatMap { | |
self.$members.attach(user, on: req.db) | |
} | |
} | |
} | |
} | |
public final class UserConversation: Model { | |
public static let schema = "user_conversation_pivot" | |
@ID(custom: "id") public var id: ObjectId? | |
//@Parent(key: "user_id") var user: User | |
@Parent(key: "memberId") public var member: User | |
@Parent(key: "adminId") public var admin: User | |
@Parent(key: "conversationId") public var conversation: Conversation | |
public init() { } | |
public init(id: ObjectId? = nil, user: User, conversation: Conversation) throws { | |
self.id = id | |
self.$member.id = try user.requireID() | |
self.$admin.id = try user.requireID() | |
self.$conversation.id = try conversation.requireID() | |
} | |
} | |
User.query(on: req.db) | |
//.filter(User.self, \._$id == req.payload.userId) | |
.with(\.$membersConversaions).with(\.$membersConversaions.$pivots) | |
.with(\.$adminsConversations).with(\.$adminsConversations.$pivots) | |
.first() | |
.unwrap(or: Abort(.notFound, reason: "No ")) | |
.map { user in | |
//let conversations = user.$membersConversaions.$pivots // + user.$adminsConversations | |
print(#line, user) | |
// how do i find user conversations (where user are admin + member) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment