Skip to content

Commit

Permalink
Removed un-needed try statements
Browse files Browse the repository at this point in the history
  • Loading branch information
calebkleveter committed Jun 19, 2018
1 parent d57ae41 commit ac9da06
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
8 changes: 4 additions & 4 deletions Sources/App/Controllers/AuthController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class AuthController: RouteCollection {
try user.validate()

// Make sure no user exists yet with the email pssed in.
let count = try User.query(on: request).filter(\.email == user.email).count()
let count = User.query(on: request).filter(\.email == user.email).count()
return count.map(to: User.self) { count in
guard count < 1 else { throw Abort(.badRequest, reason: "This email is already registered.") }
return user
Expand Down Expand Up @@ -99,7 +99,7 @@ final class AuthController: RouteCollection {
let email = try request.content.syncGet(String.self, at: "email")

// Verify a user exists with the given email.
let user = try User.query(on: request).filter(\.email == email).first().unwrap(or: Abort(.badRequest, reason: "No user found with email '\(email)'."))
let user = User.query(on: request).filter(\.email == email).first().unwrap(or: Abort(.badRequest, reason: "No user found with email '\(email)'."))
return user.flatMap(to: (User, String).self) { user in

// Verifiy that the user has confimed their account.
Expand Down Expand Up @@ -144,7 +144,7 @@ final class AuthController: RouteCollection {

// Get the user with the ID that was just fetched.
let userID = refreshJWT.payload.id
let user = try User.find(userID, on: request).unwrap(or: Abort(.badRequest, reason: "No user found with ID '\(userID)'."))
let user = User.find(userID, on: request).unwrap(or: Abort(.badRequest, reason: "No user found with ID '\(userID)'."))

return user.flatMap(to: (JSON, Payload).self) { user in

Expand All @@ -167,7 +167,7 @@ final class AuthController: RouteCollection {

// Get the user from the database with the email code from the request.
let code = try request.query.get(String.self, at: "code")
let user = try User.query(on: request).filter(\.emailCode == code).first().unwrap(or: Abort(.badRequest, reason: "No user found with the given code."))
let user = User.query(on: request).filter(\.emailCode == code).first().unwrap(or: Abort(.badRequest, reason: "No user found with the given code."))

return user.flatMap(to: User.self) { user in
guard !user.confirmed else { throw Abort(.badRequest, reason: "User already activated.") }
Expand Down
2 changes: 1 addition & 1 deletion Sources/App/Controllers/UserController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ final class UserController: RouteCollection {
// Get the attribute with the matching key.
// If one exists, update its `text` property,
// otherwise create a new one.
return try Attribute.query(on: request).filter(\.key == content.attributeKey).first().flatMap(to: Attribute.self) { attribute in
return Attribute.query(on: request).filter(\.key == content.attributeKey).first().flatMap(to: Attribute.self) { attribute in
if let attribute = attribute {
attribute.text = content.attributeText
return attribute.save(on: request)
Expand Down
2 changes: 1 addition & 1 deletion Sources/App/Models/Attribute/Attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension Attribute {
static func prepare(on connection: MySQLDatabase.Connection) -> Future<Void> {
return Database.create(self, on: connection) { builder in
try addProperties(to: builder)
try builder.addReference(from: \.userID, to: \User.id)
builder.reference(from: \.userID, to: \User.id)
}
}
}
10 changes: 3 additions & 7 deletions Sources/App/Models/User/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,11 @@ final class User: Content, MySQLModel, Migration, Parameter {
self.permissionLevel = try container.decodeIfPresent(UserStatus.self, forKey: .permissionLevel) ?? .standard
self.deletedAt = try container.decodeIfPresent(Date.self, forKey: .deletedAt)
}
}

/// Conforms the `User` model to the `SoftDeletable` protocol.
/// Allows a `users` row to be temporarily deleted from the database
/// with the possibility to restore.
extension User: SoftDeletable {

/// Allows Fluent to set the `deletedAt` property to the value stored in the database.
static var deletedAtKey: WritableKeyPath<User, Date?> {
/// Allows a `users` row to be temporarily deleted from the database
/// with the possibility to restore.
static var deletedAtKey: WritableKeyPath<User, Date?>? {
return \.deletedAt
}
}
Expand Down

0 comments on commit ac9da06

Please sign in to comment.