From 2f56e3f37d7f0f1804507a77bfcc67ee38f9a4dc Mon Sep 17 00:00:00 2001 From: Pau Date: Sat, 19 Apr 2025 21:16:37 +0200 Subject: [PATCH] fixed problems regarding requests and sql syntax --- .../usersHasPlacesController.js | 24 +++++----- .../authorization/userRepository.js | 4 +- .../resource_types/placesRepository.js | 14 +++--- .../usersHasPlacesRepository.js | 48 +++++++++---------- src/routes/index.js | 24 +++++----- 5 files changed, 57 insertions(+), 57 deletions(-) diff --git a/src/controllers/resource_types/usersHasPlacesController.js b/src/controllers/resource_types/usersHasPlacesController.js index 9cbbef4..330ea18 100644 --- a/src/controllers/resource_types/usersHasPlacesController.js +++ b/src/controllers/resource_types/usersHasPlacesController.js @@ -20,7 +20,7 @@ const getUserHasPlacesListController = (req, res, next, config) => { ]) .then(([getResults, countResults]) => { next({ - _data: {usersHasPlaces: getResults}, + _data: {reports: getResults}, _page: { totalElements: countResults, limit: req.query.limit || 100, @@ -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_user_has_places, conn }) + getUserHasPlacesListModel({ uuid: uuid_user_has_places, conn }) .then((response) => { if (noResults(response)) { const err = error404() @@ -51,7 +51,7 @@ const getUserHasPlacesByUuidController = (req, res, next, config) => { const result = { _data: { - usersHasPlaces: response + reports: response } } next(result) @@ -69,7 +69,7 @@ const getUserHasPlacesByUserUuidController = (req, res, next, config) => { const uuid_user = req.params.uuid const conn = mysql.start(config) - getUserHasPlacesListModel({ uuid_user, conn }) + getUserHasPlacesListModel({ user_uuid: uuid_user, conn }) .then((response) => { if (noResults(response)) { const err = error404() @@ -79,7 +79,7 @@ const getUserHasPlacesByUserUuidController = (req, res, next, config) => { const result = { _data: { - usersHasPlaces: response + reports: response } } next(result) @@ -97,7 +97,7 @@ const getUserHasPlacesByPlaceUuidController = (req, res, next, config) => { const uuid_place = req.params.uuid const conn = mysql.start(config) - getUserHasPlacesListModel({ uuid_place, conn }) + getUserHasPlacesListModel({ place_uuid: uuid_place, conn }) .then((response) => { if (noResults(response)) { const err = error404() @@ -107,7 +107,7 @@ const getUserHasPlacesByPlaceUuidController = (req, res, next, config) => { const result = { _data: { - usersHasPlaces: response + reports: response } } next(result) @@ -128,7 +128,7 @@ const postUserHasPlacesController = (req, res, next, config) => { .then((response) => { const result = { _data: { - usersHasPlaces: response + reports: response } } next(result) @@ -149,7 +149,7 @@ const putUserHasPlacesController = (req, res, next, config) => { .then((response) => { const result = { _data: { - usersHasPlaces: response + reports: response } } next(result) @@ -168,7 +168,7 @@ const deleteUserHasPlacesController = (req, res, next, config) => { const uuid_user_has_places = req.params.uuid const deleted_by = req.auth.user || null - softDeleteUserHasPlacesModel({uuid_user_has_places, deleted_by, conn}) + softDeleteUserHasPlacesModel({uuid: uuid_user_has_places, deleted_by, conn}) .then(() => { next({}) }) @@ -186,7 +186,7 @@ const deleteUserHasPlacesByUserUuidController = (req, res, next, config) => { const uuid_user = req.params.uuid const deleted_by = req.auth.user || null - softDeleteUserHasPlacesModel({uuid_user, deleted_by, conn}) + softDeleteUserHasPlacesModel({user_uuid: uuid_user, deleted_by, conn}) .then(() => { next({}) }) @@ -204,7 +204,7 @@ const deleteUserHasPlacesByPlaceUuidController = (req, res, next, config) => { const uuid_place = req.params.uuid const deleted_by = req.auth.user || null - softDeleteUserHasPlacesModel({uuid_place, deleted_by, conn}) + softDeleteUserHasPlacesModel({place_uuid: uuid_place, deleted_by, conn}) .then(() => { next({}) }) diff --git a/src/repositories/authorization/userRepository.js b/src/repositories/authorization/userRepository.js index ccdd960..603ff1e 100644 --- a/src/repositories/authorization/userRepository.js +++ b/src/repositories/authorization/userRepository.js @@ -78,9 +78,9 @@ const insertUserQuery = ({email, fk_role, createdBy}) => { :uuid, :username, :password, - ${emailCondition} + ${emailCondition}, ${roleCondition} - ${createdByCondition} + ${createdByCondition}, :now ); SELECT * FROM dbmaster.users WHERE uuid = :uuid; diff --git a/src/repositories/resource_types/placesRepository.js b/src/repositories/resource_types/placesRepository.js index 71c9c6e..d0c97b0 100644 --- a/src/repositories/resource_types/placesRepository.js +++ b/src/repositories/resource_types/placesRepository.js @@ -38,9 +38,9 @@ const countPlaceListQuery = rest => _placeSelectQuery()({ count: 'COUNT(DISTINCT(p.uuid)) AS count' })(rest); const insertPlaceQuery = ({ description, address, createdBy }) => { - const descriptionCondition = description ? ':description,' : null; - const addressCondition = address ? ':address,' : null; - const createdByCondition = createdBy ? 'createdBy = :createdBy,' : null; + const descriptionCondition = description ? ':description' : null; + const addressCondition = address ? ':address' : null; + const createdByCondition = createdBy ? 'createdBy = :createdBy' : null; return ` INSERT INTO dbmaster.places ( uuid, @@ -55,11 +55,11 @@ const insertPlaceQuery = ({ description, address, createdBy }) => { VALUES ( :uuid, :name, - ${descriptionCondition} - ${addressCondition} + ${descriptionCondition}, + ${addressCondition}, :longitude, :latitude, - ${createdByCondition} + ${createdByCondition}, :now ); SELECT * FROM dbmaster.places WHERE uuid = :uuid; @@ -81,7 +81,7 @@ const updatePlaceQuery = ({ name, description, address, latitude, longitude }) = ${addressCondition} ${longitudeCondition} ${latitudeCondition} - uuid = :uuid, + uuid = :uuid WHERE p.uuid = :uuid AND p.deleted IS NULL diff --git a/src/repositories/resource_types/usersHasPlacesRepository.js b/src/repositories/resource_types/usersHasPlacesRepository.js index ca49249..fe1107c 100644 --- a/src/repositories/resource_types/usersHasPlacesRepository.js +++ b/src/repositories/resource_types/usersHasPlacesRepository.js @@ -1,10 +1,10 @@ import { pagination } from "../../utils/pagination.js"; //reports repository -const _userHasPlacesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, uuidUser, uuidPlace, uuidReportType, rating }) => { +const _userHasPlacesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, user_uuid, place_uuid, report_type_uuid, rating }) => { const uuidCondition = uuid ? 'AND up.uuid = :uuid ' : ''; - const uuidUserCondition = uuidUser ? 'AND up.fk_user = (SELECT id FROM dbmaster.users WHERE uuid = :uuidUser)' : ''; - const uuidPlaceCondition = uuidPlace ? 'AND up.fk_place = (SELECT id FROM dbmaster.places WHERE uuid = :uuidPlace)' : ''; - const uuidReportTypeCondition = uuidReportType ? 'AND up.fk_report_type = (SELECT id FROM dbmaster.report_types WHERE uuid = :uuidReportType)' : ''; + const user_uuidCondition = user_uuid ? 'AND up.fk_user = (SELECT id FROM dbmaster.users WHERE uuid = :user_uuid)' : ''; + const place_uuidCondition = place_uuid ? 'AND up.fk_place = (SELECT id FROM dbmaster.places WHERE uuid = :place_uuid)' : ''; + const report_type_uuidCondition = report_type_uuid ? 'AND up.fk_report_type = (SELECT id FROM dbmaster.report_types WHERE uuid = :report_type_uuid)' : ''; const ratingCondition = rating ? 'AND up.rating = :rating' : ''; return ` SELECT ${count || @@ -26,9 +26,9 @@ const _userHasPlacesSelectQuery = (_pagination = '') => ({ count }) => ({ uuid, AND rt.deleted IS NULL AND true ${uuidCondition} - ${uuidUserCondition} - ${uuidPlaceCondition} - ${uuidReportTypeCondition} + ${user_uuidCondition} + ${place_uuidCondition} + ${report_type_uuidCondition} ${ratingCondition} ${_pagination} `; @@ -40,11 +40,11 @@ const getUserHasPlacesListQuery = ({ limit, page, ...rest }) => const countUserHasPlacesListQuery = rest => _userHasPlacesSelectQuery()({ count: 'COUNT(DISTINCT(up.uuid)) AS count' })(rest); -const insertUserHasPlacesQuery = ({ uuidUser, uuidPlace, createdBy, uuidReportType }) => { - const uuidUserCondition = uuidUser ? '(SELECT id FROM dbmaster.users WHERE uuid = :uuidUser),' : null; - const uuidPlaceCondition = uuidPlace ? '(SELECT id FROM dbmaster.places WHERE uuid = :uuidPlace),' : null; - const createdByCondition = createdBy ? 'createdBy = :createdBy,' : null; - const reportTypeCondition = uuidReportType ? '(SELECT id FROM dbmaster.report_types WHERE uuid = :reportType),' : null; +const insertUserHasPlacesQuery = ({ user_uuid, place_uuid, createdBy, report_type_uuid }) => { + const user_uuidCondition = user_uuid ? '(SELECT id FROM dbmaster.users WHERE uuid = :user_uuid)' : null; + const place_uuidCondition = place_uuid ? '(SELECT id FROM dbmaster.places WHERE uuid = :place_uuid)' : null; + const createdByCondition = createdBy ? 'createdBy = :createdBy' : null; + const reportTypeCondition = report_type_uuid ? '(SELECT id FROM dbmaster.report_types WHERE uuid = :reportType)' : null; return ` INSERT INTO dbmaster.users_has_places ( @@ -57,29 +57,29 @@ const insertUserHasPlacesQuery = ({ uuidUser, uuidPlace, createdBy, uuidReportTy created ) VALUES ( - :uuid - ${uuidUserCondition} - ${uuidPlaceCondition} - ${reportTypeCondition} + :uuid, + ${user_uuidCondition}, + ${place_uuidCondition}, + ${reportTypeCondition}, :rating, - ${createdByCondition} + ${createdByCondition}, :now ); SELECT * FROM dbmaster.users_has_places WHERE uuid = :uuid; ` } -const modifyUserHasPlacesQuery = (uuidUser, uuidPlace, uuidReportType, rating) => { - const uuidUserCondition = uuidUser ? 'fk_user = (SELECT id FROM dbmaster.users WHERE uuid = :uuidUser),' : ``; - const uuidPlaceCondition = uuidPlace ? 'fk_place = (SELECT id FROM dbmaster.places WHERE uuid = :uuidPlace),' : ``; - const uuidReportTypeCondition = uuidReportType ? 'fk_report_type = (SELECT id FROM dbmaster.report_types WHERE uuid = :uuidReportType),' : ``; +const modifyUserHasPlacesQuery = (user_uuid, place_uuid, report_type_uuid, rating) => { + const user_uuidCondition = user_uuid ? 'fk_user = (SELECT id FROM dbmaster.users WHERE uuid = :user_uuid),' : ``; + const place_uuidCondition = place_uuid ? 'fk_place = (SELECT id FROM dbmaster.places WHERE uuid = :place_uuid),' : ``; + const report_type_uuidCondition = report_type_uuid ? 'fk_report_type = (SELECT id FROM dbmaster.report_types WHERE uuid = :report_type_uuid),' : ``; const ratingCondition = rating ? 'rating = :rating,' : ``; return ` UPDATE dbmaster.users_has_places SET - ${uuidUserCondition} - ${uuidPlaceCondition} - ${uuidReportTypeCondition} + ${user_uuidCondition} + ${place_uuidCondition} + ${report_type_uuidCondition} ${ratingCondition} uuid = :uuid WHERE diff --git a/src/routes/index.js b/src/routes/index.js index 0cb6fef..576bcc3 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -1071,9 +1071,9 @@ export default(config) => { (req, res, next) => authorizePermission('/reports')(req, res, next, config), [ uuid('uuid'), - uuid('uuidPlace').optional({ nullable: false, values: 'falsy' }), - uuid('uuidUser').optional({ nullable: false, values: 'falsy' }), - uuid('uuidReportType').optional({ nullable: false, values: 'falsy' }), + uuid('place_uuid').optional({ nullable: false, values: 'falsy' }), + 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' }) ], (req, res, next) => payloadExpressValidator(req, res, next, config), @@ -1105,9 +1105,9 @@ export default(config) => { (req, res, next) => authorizePermission('/reports/:uuid')(req, res, next, config), [ uuid('uuid'), - uuid('uuidPlace').optional({ nullable: false, values: 'falsy' }), - uuid('uuidUser').optional({ nullable: false, values: 'falsy' }), - uuid('uuidReportType').optional({ nullable: false, values: 'falsy' }), + uuid('place_uuid').optional({ nullable: false, values: 'falsy' }), + 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' }) ], (req, res, next) => payloadExpressValidator(req, res, next, config), @@ -1139,9 +1139,9 @@ export default(config) => { (req, res, next) => authenticateToken(req, res, next, config), (req, res, next) => authorizePermission('/reports')(req, res, next, config), [ - uuid('uuidPlace'), - uuid('uuidUser'), - uuid('uuidReportType'), + uuid('place_uuid'), + uuid('user_uuid'), + uuid('report_type_uuid').optional({ nullable: false, values: 'falsy' }), integerRange('rating', {min: 1, max: 3}), varChar('description').optional({ nullable: true, values: 'falsy' }), ], @@ -1176,9 +1176,9 @@ export default(config) => { (req, res, next) => authorizePermission('/reports/:uuid')(req, res, next, config), [ uuid('uuid'), - uuid('uuidPlace').optional({ nullable: false, values: 'falsy' }), - uuid('uuidUser').optional({ nullable: false, values: 'falsy' }), - uuid('uuidReportType').optional({ nullable: false, values: 'falsy' }), + uuid('place_uuid').optional({ nullable: false, values: 'falsy' }), + 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' }), ],