From 372278e669960ec4f56ea26a04f7ae987a4bdca5 Mon Sep 17 00:00:00 2001 From: Pau Date: Mon, 21 Apr 2025 23:18:18 +0200 Subject: [PATCH] altered users has places to better resemble API RESTful structure --- .../usersHasPlacesController.js | 4 +- .../usersHasPlacesRepository.js | 3 +- src/routes/index.js | 83 +++++++++++++++++++ 3 files changed, 87 insertions(+), 3 deletions(-) diff --git a/src/controllers/resource_types/usersHasPlacesController.js b/src/controllers/resource_types/usersHasPlacesController.js index c53483d..d1c4297 100644 --- a/src/controllers/resource_types/usersHasPlacesController.js +++ b/src/controllers/resource_types/usersHasPlacesController.js @@ -15,7 +15,7 @@ const getUserHasPlacesListController = (req, res, next, config) => { const conn = mysql.start(config) Promise.all([ - getUserHasPlacesListModel({...req.query, conn}), + getUserHasPlacesListModel({...req.query, ...req.body, ...req.params, conn}), countUserHasPlacesListModel({...req.query, conn}) ]) .then(([getResults, countResults]) => { @@ -41,7 +41,7 @@ const getUserHasPlacesByUuidController = (req, res, next, config) => { const uuid_user_has_places = req.params.uuid const conn = mysql.start(config) - getUserHasPlacesListModel({ uuid: uuid_user_has_places, conn }) + getUserHasPlacesListModel({ uuid: uuid_user_has_places, ...req.params, ...req.body, conn }) .then((response) => { if (noResults(response)) { const err = error404() diff --git a/src/repositories/resource_types/usersHasPlacesRepository.js b/src/repositories/resource_types/usersHasPlacesRepository.js index edbe530..a5bc03c 100644 --- a/src/repositories/resource_types/usersHasPlacesRepository.js +++ b/src/repositories/resource_types/usersHasPlacesRepository.js @@ -45,7 +45,8 @@ const insertUserHasPlacesQuery = ({ user_uuid, place_uuid, description, createdB const place_uuidCondition = place_uuid ? '(SELECT id FROM dbmaster.places WHERE uuid = :place_uuid)' : null; const createdByCondition = createdBy ? 'createdBy = :createdBy' : null; const descriptionCondition = description ? 'description = :description' : null; - const reportTypeCondition = report_type_uuid ? '(SELECT id FROM dbmaster.report_types WHERE uuid = :reportType)' : null; + const reportTypeCondition = report_type_uuid ? `(SELECT id FROM dbmaster.report_types WHERE uuid = :report_type_uuid)` : null; + return ` INSERT INTO dbmaster.users_has_places ( diff --git a/src/routes/index.js b/src/routes/index.js index 8cf159c..42e8dc1 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -1116,6 +1116,89 @@ export default(config) => { (result, req, res, _) => sendOkResponse(result, req, res) ); + /** + * @name GET/users/:user_uuid/reports + * @function + * @inner + * @memberof placeRouter + * @route GET /users/:user_uuid/reports + * @group Reports - Operations about reports + * @param {string} uuid.path.optional - The unique identifier for the report + * @param {string} fk_place.path.optional - The unique identifier for the place + * @param {string} fk_user.path.required - The unique identifier for the user + * @param {string} fk_report_type.path.optional - The unique identifier for the report type + * @param {string} rating.path.optional - The rating of the report + * @param {string} description.path.optional - The description of the report + * @returns {SuccessResponse} 200 - The report object + * @returns {ErrorResponse} 404 - Report not found + * @returns {ErrorResponse} 422 - Unprocessable entity + * @returns {ErrorResponse} 500 - Internal server error + * @returns {ErrorResponse} 403 - Forbidden + * @returns {ErrorResponse} 401 - Unauthorized + */ + routes.get( + '/users/:user_uuid/reports', + (req, res, next) => authenticateToken(req, res, next, config), + (req, res, next) => authorizePermission('/users/:user_uuid/reports')(req, res, next, config), + [ + uuid('user_uuid'), + uuid('uuid').optional({ nullable: false, values: 'falsy' }), + uuid('place_uuid').optional({ nullable: false, values: 'falsy' }), + uuid('report_type_uuid').optional({ nullable: false, values: 'falsy' }), + integerRange('rating', {min: 1, max: 3}).optional({ nullable: false, values: 'falsy' }), + varChar('description').optional({ nullable: true, values: 'falsy' }) + ], + (req, res, next) => payloadExpressValidator(req, res, next, config), + (req, res, next) => getUserHasPlacesListController(req, res, next, config), + (result, req, res, next) => addLinks(result, req, res, next, hasAddLinks, linkRoutes), + (result, req, res) => sendOkResponse(result, req, res) + ); + + /** + * @name GET/places/:place_uuid/reports + * @function + * @inner + * @memberof placeRouter + * @route GET /places/:place_uuid/reports + * @group Reports - Operations about reports + * @param {string} uuid.path.optional - The unique identifier for the report + * @param {string} fk_place.path.required - The unique identifier for the place + * @param {string} fk_user.path.optional - The unique identifier for the user + * @param {string} fk_report_type.path.optional - The unique identifier for the report type + * @param {string} rating.path.optional - The rating of the report + * @param {string} description.path.optional - The description of the report + * @returns {SuccessResponse} 200 - The report object + * @returns {ErrorResponse} 404 - Report not found + * @returns {ErrorResponse} 422 - Unprocessable entity + * @returns {ErrorResponse} 500 - Internal server error + * @returns {ErrorResponse} 403 - Forbidden + * @returns {ErrorResponse} 401 - Unauthorized + * @returns {ErrorResponse} 400 - Bad request + */ + routes.get( + '/places/:place_uuid/reports', + (req, res, next) => authenticateToken(req, res, next, config), + (req, res, next) => authorizePermission('/places/:place_uuid/reports')(req, res, next, config), + [ + uuid('uuid').optional({ nullable: false, values: 'falsy' }), + uuid('place_uuid'), + uuid('user_uuid').optional({ nullable: false, values: 'falsy' }), + uuid('report_type_uuid').optional({ nullable: false, values: 'falsy' }), + integerRange('rating', {min: 1, max: 3}).optional({ nullable: false, values: 'falsy' }), + varChar('description').optional({ nullable: true, values: 'falsy' }) + ], + (req, res, next) => payloadExpressValidator(req, res, next, config), + // Interceptamos el parĂ¡metro y lo pasamos como query para reutilizar el mismo controlador + (req, res, next) => { + req.query.place_uuid = req.params.place_uuid; + next(); + }, + (req, res, next) => getUserHasPlacesListController(req, res, next, config), + (result, req, res, next) => addLinks(result, req, res, next, hasAddLinks, linkRoutes), + (result, req, res) => sendOkResponse(result, req, res) + ); + + /** * @name POST/reports * @function