Created
August 26, 2018 21:57
-
-
Save k1r0s/c061559f37d97cc315b09384c96394d4 to your computer and use it in GitHub Desktop.
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 { AbstractResource } from "@ritley/core"; | |
import UserModel from "../models/user.model"; | |
import ParseReqBody from "../decorators/req-body-json.decorator"; | |
import ValidateSession from "../decorators/validate-session.decorator"; | |
import { | |
Dependency, | |
Default, | |
Catch, | |
InternalServerError, | |
BadRequest, | |
Conflict, | |
Created, | |
Ok | |
} from "@ritley/decorators"; | |
@Dependency("userModel", UserModel) | |
export default class UserResource extends AbstractResource { | |
constructor(_database) { | |
super("/users"); | |
} | |
@Default(Created) | |
@ParseReqBody | |
async post(req, res, payload) { | |
await this.validate(payload, res); | |
await this.isUnique(payload, res); | |
return await this.create(payload, res); | |
} | |
@Default(Ok) | |
@ValidateSession | |
async get(req, res, session) { | |
return this.readUsers(req, res); | |
} | |
@Catch(BadRequest, "missing fields, required: [name, mail, pass]") | |
validate(payload) { | |
return this.userModel.validate(payload); | |
} | |
@Catch(Conflict, "mail is already taken, try another one") | |
isUnique(payload) { | |
return this.userModel.isUnique(payload); | |
} | |
@Catch(InternalServerError, "there was an error creating your user, try again") | |
create(payload) { | |
return this.userModel.create(payload); | |
} | |
@Catch(InternalServerError, "there was an error trying to retrieve user list") | |
readUsers() { | |
return this.userModel.searchBy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment