Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change return error code HTTP for all route #58

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 39 additions & 30 deletions src/modules/details/details.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
Delete,
NotFoundException,
BadRequestException,
InternalServerErrorException,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { DetailsService } from './details.service';

Expand All @@ -31,21 +32,22 @@ export class DetailsController {
* @param {number} idRestaurant - The unique identifier of the restaurant.
* @returns {Promise<any>} - A promise that resolves to an array of details.
* @throws {NotFoundException} - Throws if no details are found for the restaurant.
* @throws {InternalServerErrorException} - Throws if there is an error during retrieval.
* @throws {HttpException} - Throws if there is an error during retrieval.
* @async
*/
@Get()
async getAllDetail(@Param('idRestaurant') idRestaurant: number) {
try {
const details = await this.detailsService.findAll(Number(idRestaurant));
if (!details || details.length === 0) {
throw new NotFoundException('No details found');
throw new NotFoundException();
}
return details.details;
} catch (error) {
throw new InternalServerErrorException(
`Error fetching details: ${error}`,
);
if (error instanceof HttpException) {
throw error;
}
throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -56,7 +58,7 @@ export class DetailsController {
* @param {number} id - The unique identifier of the detail.
* @returns {Promise<any>} - A promise that resolves to the requested detail.
* @throws {NotFoundException} - Throws if the detail is not found.
* @throws {InternalServerErrorException} - Throws if there is an error during retrieval.
* @throws {HttpException} - Throws if there is an error during retrieval.
* @async
*/
@Get(':id')
Expand All @@ -70,13 +72,14 @@ export class DetailsController {
Number(id),
);
if (!detail) {
throw new NotFoundException(`Detail with id ${id} not found`);
throw new NotFoundException();
}
return detail.details[0];
} catch (error) {
throw new InternalServerErrorException(
`Error fetching detail with id ${id}: ${error}`,
);
if (error instanceof HttpException) {
throw error;
}
throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -87,7 +90,7 @@ export class DetailsController {
* @param {Request} request - The request object containing detail data.
* @returns {Promise<any>} - A promise that resolves to the created detail.
* @throws {BadRequestException} - Throws if there is an error during creation.
* @throws {InternalServerErrorException} - Throws if there is an error during creation.
* @throws {HttpException} - Throws if there is an error during creation.
* @async
*/
@Post()
Expand All @@ -100,12 +103,18 @@ export class DetailsController {
Number(idRestaurant),
request.body,
);
if (!createdDetail) {
throw new BadRequestException('Error creating detail');
if (createdDetail.modifiedCount === 0) {
throw new NotFoundException();
}
if (createdDetail.matchedCount === 0) {
throw new NotFoundException();
}
return createdDetail;
} catch (error) {
throw new InternalServerErrorException(`Error creating detail: ${error}`);
if (error instanceof HttpException) {
throw error;
}
throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -118,7 +127,7 @@ export class DetailsController {
* @returns {Promise<any>} - A promise that resolves to a success message.
* @throws {NotFoundException} - Throws if the detail is not found.
* @throws {BadRequestException} - Throws if no changes are made.
* @throws {InternalServerErrorException} - Throws if there is an error during the update.
* @throws {HttpException} - Throws if there is an error during the update.
* @async
*/
@Put(':id')
Expand All @@ -134,18 +143,17 @@ export class DetailsController {
request.body,
);
if (result.matchedCount === 0) {
throw new NotFoundException(`Detail with id ${id} not found`);
throw new NotFoundException();
}
if (result.modifiedCount === 0) {
throw new BadRequestException(
`No changes made to the detail with id ${id}`,
);
throw new BadRequestException();
}
return { message: `Detail with id ${id} updated successfully` };
return;
} catch (error) {
throw new InternalServerErrorException(
`Error updating detail with id ${id}: ${error}`,
);
if (error instanceof HttpException) {
throw error;
}
throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -156,7 +164,7 @@ export class DetailsController {
* @param {number} id - The unique identifier of the detail to be deleted.
* @returns {Promise<any>} - A promise that resolves to a success message.
* @throws {NotFoundException} - Throws if the detail is not found.
* @throws {InternalServerErrorException} - Throws if there is an error during deletion.
* @throws {HttpException} - Throws if there is an error during deletion.
* @async
*/
@Delete(':id')
Expand All @@ -170,13 +178,14 @@ export class DetailsController {
Number(id),
);
if (result.modifiedCount === 0) {
throw new NotFoundException(`Detail with id ${id} not found`);
throw new NotFoundException();
}
return { message: `Detail with id ${id} deleted successfully` };
return;
} catch (error) {
throw new InternalServerErrorException(
`Error deleting detail with id ${id}: ${error}`,
);
if (error instanceof HttpException) {
throw error;
}
throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
67 changes: 39 additions & 28 deletions src/modules/food/food.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
Delete,
NotFoundException,
BadRequestException,
InternalServerErrorException,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { FoodService } from './food.service';

Expand All @@ -31,19 +32,22 @@ export class FoodController {
* @param {number} idRestaurant - The unique identifier for the restaurant.
* @returns {Promise<any>} An array of food items.
* @throws {NotFoundException} if no food items are found.
* @throws {InternalServerErrorException} if there is an error during the operation.
* @throws {HttpException} if there is an error during the operation.
* @async
*/
@Get()
async getAllFood(@Param('idRestaurant') idRestaurant: number): Promise<any> {
try {
const food = await this.foodService.findAll(Number(idRestaurant));
if (!food || food.length === 0) {
throw new NotFoundException('No food found');
throw new NotFoundException();
}
return food.foods; // Assuming food.foods is of type any[]
} catch (error) {
throw new InternalServerErrorException(`Error fetching food: ${error}`);
if (error instanceof HttpException) {
throw error;
}
throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -54,7 +58,7 @@ export class FoodController {
* @param {number} id - The unique identifier for the food item.
* @returns {Promise<any>} The food item if found.
* @throws {NotFoundException} if the food item is not found.
* @throws {InternalServerErrorException} if there is an error during the operation.
* @throws {HttpException} if there is an error during the operation.
* @async
*/
@Get(':id')
Expand All @@ -68,13 +72,14 @@ export class FoodController {
Number(id),
);
if (!food) {
throw new NotFoundException(`Food with id ${id} not found`);
throw new NotFoundException();
}
return food.foods[0]; // Assuming food.foods is of type any[]
} catch (error) {
throw new InternalServerErrorException(
`Error fetching food with id ${id}: ${error}`,
);
if (error instanceof HttpException) {
throw error;
}
throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -85,7 +90,7 @@ export class FoodController {
* @param {Request} request - The request object containing the food item data.
* @returns {Promise<any>} The created food item.
* @throws {BadRequestException} if there is an error during creation.
* @throws {InternalServerErrorException} if there is an error during the operation.
* @throws {HttpException} if there is an error during the operation.
* @async
*/
@Post()
Expand All @@ -98,12 +103,18 @@ export class FoodController {
Number(idRestaurant),
request.body,
);
if (!createdFood) {
throw new BadRequestException('Error creating food');
if (createdFood.modifiedCount === 0) {
throw new NotFoundException();
}
if (createdFood.matchedCount === 0) {
throw new NotFoundException();
}
return createdFood; // Assuming createdFood is of type any
} catch (error) {
throw new InternalServerErrorException(`Error creating food: ${error}`);
if (error instanceof HttpException) {
throw error;
}
throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -116,7 +127,7 @@ export class FoodController {
* @returns {Promise<any>} A success message if the food item is updated successfully.
* @throws {NotFoundException} if the food item is not found.
* @throws {BadRequestException} if no changes are made.
* @throws {InternalServerErrorException} if there is an error during the operation.
* @throws {HttpException} if there is an error during the operation.
* @async
*/
@Put(':id')
Expand All @@ -132,18 +143,17 @@ export class FoodController {
request.body,
);
if (result.matchedCount === 0) {
throw new NotFoundException(`Food with id ${id} not found`);
throw new NotFoundException();
}
if (result.modifiedCount === 0) {
throw new BadRequestException(
`No changes made to the food with id ${id}`,
);
throw new NotFoundException();
}
return { message: `Food with id ${id} updated successfully` };
return;
} catch (error) {
throw new InternalServerErrorException(
`Error updating food with id ${id}: ${error}`,
);
if (error instanceof HttpException) {
throw error;
}
throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -154,7 +164,7 @@ export class FoodController {
* @param {number} id - The unique identifier for the food item.
* @returns {Promise<any>} A success message if the food item is deleted successfully.
* @throws {NotFoundException} if the food item is not found.
* @throws {InternalServerErrorException} if there is an error during the operation.
* @throws {HttpException} if there is an error during the operation.
* @async
*/
@Delete(':id')
Expand All @@ -168,13 +178,14 @@ export class FoodController {
Number(id),
);
if (result.modifiedCount === 0) {
throw new NotFoundException(`Food with id ${id} not found`);
throw new NotFoundException();
}
return { message: `Food with id ${id} deleted successfully` };
return;
} catch (error) {
throw new InternalServerErrorException(
`Error deleting food with id ${id}: ${error}`,
);
if (error instanceof HttpException) {
throw error;
}
throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
Loading
Loading