Skip to content

Commit

Permalink
implementing reviewer's suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
reskimulud committed Feb 20, 2022
1 parent 58bb657 commit afeded2
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 21 deletions.
29 changes: 29 additions & 0 deletions migrations/1645000146296_create-table-users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable camelcase */

exports.shorthands = undefined;

exports.up = (pgm) => {
pgm.createTable('users', {
id: {
type: 'VARCHAR(50)',
primaryKey: true,
},
username: {
type: 'VARCHAR(50)',
notNull: true,
unique: true,
},
password: {
type: 'TEXT',
notNull: true,
},
fullname: {
type: 'TEXT',
notNull: true,
},
});
};

exports.down = (pgm) => {
pgm.dropTable('users');
};
36 changes: 27 additions & 9 deletions src/api/albums/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,33 @@ class AlbumsHandler {
}

async getAlbumsHandler() {
const albums = await this._service.getAlbums();

return {
status: 'success',
message: 'Albums fetched',
data: {
albums,
},
};
try {
const albums = await this._service.getAlbums();

return {
status: 'success',
message: 'Albums fetched',
data: {
albums,
},
};
} catch (err) {
if (err instanceof ClientError) {
const response = h.response({
status: 'fail',
message: err.message,
});
response.code(err.statusCode);
return response;
}

const response = h.response({
status: 'error',
message: 'Internal server error',
});
response.code(500);
return response;
}
}

async getAlbumByIdHandler(request, h) {
Expand Down
38 changes: 28 additions & 10 deletions src/api/songs/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,34 @@ class SongsHandler {
}

async getSongsHandler(request) {
const {title, performer} = request.query;
const songs = await this._service.getSongs({title, performer});

return {
status: 'success',
message: 'Songs fetched',
data: {
songs,
},
};
try {
const {title, performer} = request.query;
const songs = await this._service.getSongs({title, performer});

return {
status: 'success',
message: 'Songs fetched',
data: {
songs,
},
};
} catch (err) {
if (err instanceof ClientError) {
const response = h.response({
status: 'fail',
message: err.message,
});
response.code(err.statusCode);
return response;
}

const response = h.response({
status: 'error',
message: 'Internal server error',
});
response.code(500);
return response;
}
}

async getSongByIdHandler(request, h) {
Expand Down
3 changes: 2 additions & 1 deletion src/validator/albums/schema.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const Joi = require('joi');
const currentYear = new Date().getFullYear();

const AlbumPayloadSchema = Joi.object({
name: Joi.string().required(),
year: Joi.number().required(),
year: Joi.number().min(1900).max(currentYear).required(),
});

module.exports = {AlbumPayloadSchema};
3 changes: 2 additions & 1 deletion src/validator/songs/schema.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const Joi = require('joi');
const currentYear = new Date().getFullYear();

const SongPayloadSchema = Joi.object({
title: Joi.string().required(),
year: Joi.number().required(),
year: Joi.number().min(1900).max(currentYear).required(),
genre: Joi.string().required(),
performer: Joi.string().required(),
duration: Joi.number(),
Expand Down

0 comments on commit afeded2

Please sign in to comment.