This file contains hidden or 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 UserTopicPivot: SQLitePivot { | |
var id: Int? | |
var userID: Int | |
var topicID: Int | |
typealias Left = User | |
typealias Right = Topic | |
static let leftIDKey: LeftIDKey = \.userID | |
static let rightIDKey: RightIDKey = \.topicID | |
} |
This file contains hidden or 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() | |
}) | |
} |
This file contains hidden or 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 TopicController { | |
func getTopicSubscriber(_ req: Request) throws -> Future<[User]> { | |
let name = try req.parameters.next(String.self) | |
guard let convert = name.removingPercentEncoding else { | |
throw Abort(.expectationFailed, reason: "主题名称无效", identifier: nil) | |
} | |
return Topic.query(on: req).filter(\.name ~= convert).first().flatMap({ (topic) in | |
guard let topic = topic else { | |
throw Abort(.badRequest, reason: "主题不存在", identifier: nil) |
This file contains hidden or 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 func routes(_ router: Router) throws { | |
let uCtrller = UserController() | |
let usersRouter = router.grouped("api","users") | |
usersRouter.get("subscribetopics",Int.parameter, use: uCtrller.getSubscribeTopics) | |
usersRouter.post(SiblingPair.self, at: "subscribe", use: uCtrller.subscribe) | |
let tpCtrller = TopicController() | |
let topicsRouter = router.grouped("api","topics") | |
topicsRouter.get("subscribers",String.parameter, use: tpCtrller.getTopicSubscriber) |
This file contains hidden or 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
Personal | |
guiliuvapor | |
Overview | |
Resources | |
Deploy | |
Metrics | |
Activity | |
Access |