Created
June 25, 2018 09:25
-
-
Save aquaibm/07a9f9407e699bb4af787946d5596afa to your computer and use it in GitHub Desktop.
UserController
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
struct UserController { | |
func getSubscribeTopics(request: Request) throws -> Future<[Topic]> { | |
let id = try request.parameters.next(Int.self) | |
return User.query(on: request).filter(\.id == id).first().flatMap({ (user) in | |
guard let user = user else { | |
throw Abort(.notFound, reason: "没有此用户", identifier: nil) | |
} | |
return try user.subscribeTopics.query(on: request).all() | |
}) | |
} | |
func subscribe(_ req: Request, pair: SiblingPair) throws -> Future<HTTPResponseStatus> { | |
return Topic.query(on: req).filter(\.name ~= pair.topicName).first().flatMap({ (topic) in | |
guard let topic = topic else { | |
throw Abort(.badRequest, reason: "主题不存在", identifier: nil) | |
} | |
let pivot = try UserTopicPivot(id: nil, userID: pair.userID, topicID: topic.requireID()) | |
return pivot.save(on: req).transform(to: .created) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment