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
import FluentSQLite | |
import Vapor | |
/// Called before your application initializes. | |
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws { | |
// Register providers first | |
try services.register(FluentSQLiteProvider()) | |
// Register routes to the router |
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
import Vapor | |
public func routes(_ router: Router) throws { | |
let postController = PostController() // 1 | |
// 2 | |
/// GET /posts | |
router.get("posts", use: postController.index) | |
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
import Vapor | |
/// Controls basic CRUD operations on `Post`s. | |
final class PostController { | |
/// Returns a list of all `Posts`s. | |
/// GET /posts | |
func index(_ req: Request) throws -> Future<[Post]> { | |
return Post.query(on: req).all() //1 |
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 PostAndComments { | |
var post: Post | |
var comments: [Comment] | |
} | |
func showPostsWithComments() -> Future<[PostAndComments]> { | |
return getPostsFromDatabase().flatMap { posts -> Future<[PostAndComments]> in | |
return posts.flatMap { post -> Future<[PostAndComments]> in | |
return post.getComments().map { comments -> [PostAndComments] in | |
return PostAndComment(post: post, comments: comments) |
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
let futurePosts = getPostsFromDatabase().map { posts in | |
// posts: [Post] | |
for post in posts { | |
print(post.title) | |
} | |
} |
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
import Vapor | |
import FluentMySQL // 1 | |
final class Post: MySQLModel { // 2 | |
var id: Int? | |
var title: String | |
var body: String // 3 | |
init(id: Int? = nil, title: String, body: String) { // 4 | |
self.id = id |
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
func validate(input: String) -> Bool { | |
let digits = input.map { Int(String($0)) } | |
guard digits.count == 10 && digits.count == input.count else { | |
return false | |
} | |
let check = digits[9]! | |
let remainder = digits.prefix(upTo: 9).enumerated().reduce(0) { |
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
import Vapor | |
import Crypto // 1 | |
/// Controls basic CRUD operations on `User`s. | |
final class UserController: RouteCollection { | |
func boot(router: Router) throws { | |
let users = router.grouped("users") // 2 | |
users.get(use: index) |
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
import Vapor | |
public func routes(_ router: Router) throws { | |
let userController = UserController() | |
router.post("users", use: UserController.create) | |
router.get("users", use: UserController.index) | |
router.get("users", User.parameter, use: UserController.show) | |
router.patch("users", use: UserController.update) | |
router.delete("users", User.parameter, use: UserController.delete) |
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
import Vapor | |
public func routes(_ router: Router) throws { | |
try router.register(collection: UserController()) | |
} |
NewerOlder