Skip to content

Instantly share code, notes, and snippets.

@k1r0s
Created August 26, 2018 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k1r0s/c061559f37d97cc315b09384c96394d4 to your computer and use it in GitHub Desktop.
Save k1r0s/c061559f37d97cc315b09384c96394d4 to your computer and use it in GitHub Desktop.
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