Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { logger } from "@server/fd/fd-toolbox/logging/logger";
- import { HTTP_STATUS } from "@server/fd/fd-toolbox/constants/http-status";
- export async function crudPut<T extends WithId>(editResource: EditResource<T>, res: NextApiResponse) {
- try {
- const crudUpdateConfig: CrudUpdateConfig = {
- Resource: editResource.Resource,
- UserId: GetUserId(),
- PropertyRelationSyncInfos: editResource.PropertyRelationSyncInfos,
- ThrowOnNonSuccess: false,
- IsCalledFromOutside: true,
- };
- const crudServiceResult: CrudResult = await crudUpdate(crudUpdateConfig);
- switch (crudServiceResult.Type) {
- case CrudResultType.Success:
- return sendSuccessResponse(res, crudServiceResult);
- case CrudResultType.NotValid:
- return sendValidationErrorResponse(res, crudServiceResult.ValidationResult);
- default:
- return sendInternalErrorResponse(res);
- }
- } catch (error) {
- logger.error("Error in crudPut:", error);
- return sendInternalErrorResponse(res);
- }
- }
- function sendSuccessResponse(res: NextApiResponse, result: CrudResultWithGeneric<WithId>) {
- const response: ApiResponse<CrudResultWithGeneric<WithId>> = { data: result };
- return res.status(HTTP_STATUS.OK).json(response);
- }
- function sendValidationErrorResponse(res: NextApiResponse, validationResult: ValidationResult) {
- const response: ApiResponse<ValidationResult> = { data: validationResult };
- return res.status(HTTP_STATUS.BAD_REQUEST).json(response);
- }
- function sendInternalErrorResponse(res: NextApiResponse) {
- return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).end();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement