Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Feature/final touches #58

Merged
merged 3 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# .github/workflows/documentation.yml
name: Documentation

on: [release]
on:
release:
types: [published]

jobs:
build:
Expand Down
10 changes: 9 additions & 1 deletion Sources/Submissions/UpdateRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ extension UpdateRequest {

public extension UpdateRequest {
static func update(_ model: Model, on request: Request) -> EventLoopFuture<Model> {
validated(on: request).flatMap { $0.update(model, on: request) }
validated(for: model, on: request).flatMap { $0.update(model, on: request) }
}

static func validated(for model: Model, on request: Request) -> EventLoopFuture<Self> {
validations(for: model, on: request).flatMapThrowing { validations in
try validations.validate(request).assert()
}.flatMap {
make(from: request)
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions Tests/SubmissionsTests/Models/Entities/Post.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Vapor

struct Post: Content {
let id = UUID()
let title: String
final class Post: Content {
var title: String

init(title: String) {
self.title = title
}
}
10 changes: 2 additions & 8 deletions Tests/SubmissionsTests/Models/Requests/CreatePostRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ import Vapor
import Submissions

struct CreatePostRequest: Content, CreateRequest {
typealias Model = Post
let title: String

func create(on request: Request) -> EventLoopFuture<Post> {
do {
let post = try request.content.decode(Post.self)
// add code here that creates a new post into the database
return request.eventLoop.future(post)
} catch(let error) {
return request.eventLoop.makeFailedFuture(error)
}
request.eventLoop.future(Post(title: title))
}

static func validations(on request: Request) -> EventLoopFuture<Validations> {
Expand Down
19 changes: 10 additions & 9 deletions Tests/SubmissionsTests/Models/Requests/UpdatePostRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import Vapor
import Submissions

struct UpdatePostRequest: Content, UpdateRequest {
typealias Model = Post
let title: String?

static func validations(for model: Post, on request: Request) -> EventLoopFuture<Validations> {
validations(on: request)
var validations = Validations()
if request.url.query == "fail" {
validations.add("validation", result: ValidatorResults.TestFailure())
}
return request.eventLoop.future(validations)
}

func update(_ model: Post, on request: Request) -> EventLoopFuture<Post> {
do {
let post = try request.content.decode(Post.self)
// add code here that saves the updated post into the database
return request.eventLoop.future(post)
} catch(let error) {
return request.eventLoop.makeFailedFuture(error)
func update(_ post: Post, on request: Request) -> EventLoopFuture<Post> {
if let title = title {
post.title = title
}
return request.eventLoop.future(post)
}
}
18 changes: 14 additions & 4 deletions Tests/SubmissionsTests/SubmissionsTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import XCTVapor


final class SubmissionsTests: XCTestCase {
var app: Application!

Expand All @@ -14,7 +13,7 @@ final class SubmissionsTests: XCTestCase {
app = nil
}

func test_crate() throws {
func test_create() throws {
try app.test(.POST, "posts", beforeRequest: { request in
try request.content.encode(["title": "Some title"], as: .json)
}) { response in
Expand All @@ -26,11 +25,12 @@ final class SubmissionsTests: XCTestCase {
}
}

func test_crate_includesValidation() throws {
func test_create_includesValidation() throws {
try app.test(.POST, "posts?fail", beforeRequest: { request in
try request.content.encode(["title": "Some title"], as: .json)
}) { response in
XCTAssertEqual(response.status, .badRequest)

let errorReason: String = try response.content.get(at: "reason")
XCTAssertEqual(errorReason, "validation has failed")
}
Expand All @@ -47,5 +47,15 @@ final class SubmissionsTests: XCTestCase {
XCTAssertEqual(post.title, "Updated title")
}
}


func test_update_includesValidations() throws {
try app.test(.PUT, "posts?fail", beforeRequest: { request in
try request.content.encode(["title": "Updated title"], as: .json)
}) { response in
XCTAssertEqual(response.status, .badRequest)

let errorReason: String = try response.content.get(at: "reason")
XCTAssertEqual(errorReason, "validation has failed")
}
}
}