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

Commit

Permalink
Merge pull request #27 from valen90/feature/vapor-two
Browse files Browse the repository at this point in the history
Updated to vapor2
  • Loading branch information
BrettRToomey authored Apr 27, 2017
2 parents fa0b7d2 + 81b90b8 commit 38bddae
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 347 deletions.
5 changes: 2 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import PackageDescription
let package = Package(
name: "Sugar",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1),
.Package(url: "https://github.com/vapor/mysql-driver.git", majorVersion: 1),
.Package(url: "https://github.com/bygri/vapor-forms.git", majorVersion:0)
.Package(url: "https://github.com/vapor/vapor.git", Version(2,0,0, prereleaseIdentifiers: ["beta"])),
.Package(url: "https://github.com/vapor/fluent-provider.git", Version(1,0,0, prereleaseIdentifiers: ["beta"]))
]
)
2 changes: 1 addition & 1 deletion Sources/Database+MySQL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ extension Database {
var nameString = name ?? ""

// Add trailing space
if(nameString.count > 0) {
if(nameString.characters.count > 0) {
nameString += " "
}

Expand Down
69 changes: 5 additions & 64 deletions Sources/Entity+Sugar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extension Entity {
/// - Parameters:
/// - value: The value to look for.
/// - field: The field to look in.
public mutating func saveOrModify(
public func saveOrModify(
given value: NodeRepresentable,
for field: String,
beforeSave: ((Self, Self) -> ())? = nil
Expand All @@ -18,11 +18,11 @@ extension Entity {
///
/// - Parameter values: The list of values and fields to look for.
/// - Throws: On db errors.
public mutating func saveOrModify(
public func saveOrModify(
given values: [String: NodeRepresentable],
beforeSave: ((Self, Self) -> ())? = nil
) throws {
var query = try Self.query()
var query = try Self.makeQuery()
for (field, value) in values {
query = try query.filter(field, value)
}
Expand All @@ -37,8 +37,8 @@ extension Entity {
}


public mutating func saveIfUnique(given values: [String: NodeRepresentable]) throws {
var query = try Self.query()
public func saveIfUnique(given values: [String: NodeRepresentable]) throws {
var query = try Self.makeQuery()

for (field, value) in values {
query = try query.filter(field, value)
Expand All @@ -51,62 +51,3 @@ extension Entity {
try save()
}
}
// !!! WARNING: Duplicate methods due to the way Swift generics work.
extension NodesModel {
/// Saves or modifies (if already exists) a model given a value and field.
///
/// - Parameters:
/// - value: The value to look for.
/// - field: The field to look in.
public mutating func saveOrModify(
given value: NodeRepresentable,
for field: String,
beforeSave: ((Self, Self) -> ())? = nil
) throws {
return try saveOrModify(given: [field: value], beforeSave: beforeSave)
}

/// Saves of modifies (if already exists) a model given multiple values and fields.
///
/// - Parameter values: The list of values and fields to look for.
/// - Throws: On db errors.
public mutating func saveOrModify(
given values: [String: NodeRepresentable],
beforeSave: ((Self, Self) -> ())? = nil
) throws {
var query = try Self.query()
for (field, value) in values {
query = try query.filter(field, value)
}

if let instance = try query.first() {
id = instance.id
exists = true
if let beforeSave = beforeSave {
beforeSave(instance, self)
}
}

try save()
}


public mutating func saveIfUnique(given values: [String: NodeRepresentable]) throws {
var query = try Self.query()

for (field, value) in values {
query = try query.filter(field, value)
}

if var model = try query.first() {
guard Self.softDeletable else {
return
}
model.deletedAt = nil
try model.save()
return
}

try save()
}
}
12 changes: 10 additions & 2 deletions Sources/Enum+Sugar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ protocol RawStringConvertible: NodeConvertible {
extension RawStringConvertible {
public init(node: Node) throws {
guard let string = node.string else {
throw Abort.custom(status: .internalServerError, message: "Expected a String")
throw Abort(
.internalServerError,
metadata: nil,
reason: "Expected a String"
)
}

guard let state = Self.init(rawValue: string) else {
throw Abort.custom(status: .internalServerError, message: "Invalid case: \(string)")
throw Abort(
.internalServerError,
metadata: nil,
reason: "Invalid case: \(string)"
)
}

self = state
Expand Down
6 changes: 3 additions & 3 deletions Sources/FieldsetMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ public class FieldsetMiddleware: Middleware {

public func respond(to request: Request, chainingTo next: Responder) throws -> Response {
// Add fieldset to next request
request.storage[key] = try request.session().data[key]
try request.session().data[key] = nil
request.storage[key] = request.session?.data[key]
request.session?.data[key] = nil

let respond = try next.respond(to: request)

try request.session().data[key] = respond.storage[key] as? Node ?? nil
request.session?.data[key] = respond.storage[key] as? Node ?? nil

return respond
}
Expand Down
8 changes: 5 additions & 3 deletions Sources/Model+Sugar.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Vapor
import FluentProvider

extension Model {
/// Attemps to unwrap the model's id and return it as an Int.
public func idOrThrow() throws -> Int {
guard let id = self.id?.int else {
throw Abort.custom(
status: .badRequest,
message: "Missing id for \(type(of: self))"
throw Abort(
.badRequest,
metadata: nil,
reason: "Missing id for \(type(of: self))"
)
}
return id
Expand Down
71 changes: 0 additions & 71 deletions Sources/Node+Date.swift

This file was deleted.

61 changes: 0 additions & 61 deletions Sources/NodesModel.swift

This file was deleted.

16 changes: 10 additions & 6 deletions Sources/QueryRepresentable+FirstOrFail.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import Vapor
import Fluent

extension QueryRepresentable {
extension QueryRepresentable where Self: ExecutorRepresentable {

/// Returns the first entity retreived by the query
/// If no entity is found, throw Abort.Error
/// - Returns: T
/// - Throws: Error
public func firstOrFail() throws -> T {
public func firstOrFail() throws -> E {
let query = try makeQuery()
query.action = .fetch
query.limit = Limit(count: 1)

guard var model = try query.run().first else {
throw Abort.custom(status: .notFound, message: "Entity was not found")
query.limits.append(RawOr.some(Limit(count: 1)))

guard let model = try query.first() else {
throw Abort(
.notFound,
metadata: nil,
reason: "Entity was not found"
)
}

model.exists = true
Expand Down
15 changes: 0 additions & 15 deletions Sources/Response+Fieldset.swift

This file was deleted.

5 changes: 3 additions & 2 deletions Sources/String+Random.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Random
import Crypto

extension String {

Expand All @@ -11,7 +12,7 @@ extension String {
/// - Returns: string
public static func randomAlphaNumericString(_ length: Int = 64) -> String {
let letters: String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let len = letters.count
let len = letters.characters.count

var randomString = ""
for _ in 0 ..< length {
Expand All @@ -33,6 +34,6 @@ extension String {
/// - Returns: String
/// - Throws: Error
public static func random(_ length: Int = 64) throws -> String {
return try CryptoRandom.bytes(count: length).string()
return try Random.bytes(count: length).string()
}
}
6 changes: 3 additions & 3 deletions Sources/String+Sugar.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Core
import Random
import Crypto
import Foundation

extension String {
/// Generates a String of random characters (path safe)
public static func random(length: Int) throws -> String {
return try CryptoRandom.bytes(count: length).base64Encoded.string().replacingOccurrences(
return try Random.bytes(count: length).base64Encoded.string().replacingOccurrences(
of: "/", with: "_"
)
}
Expand Down Expand Up @@ -79,7 +79,7 @@ extension String {

/// The string being url encoded.
internal func urlEncoded() throws -> String {
return try percentEncoded(try self.makeBytes()).string()
return self.addingPercentEncoding(withAllowedCharacters: CharacterSet())!
}
}

Expand Down
Loading

0 comments on commit 38bddae

Please sign in to comment.