Advertisement
djbob2000

Untitled

Jul 23rd, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { logger } from "@server/fd/fd-toolbox/logging/logger";
  2. import { HTTP_STATUS } from "@server/fd/fd-toolbox/constants/http-status";
  3.  
  4. export async function crudPut<T extends WithId>(editResource: EditResource<T>, res: NextApiResponse) {
  5.     try {
  6.         const crudUpdateConfig: CrudUpdateConfig = {
  7.             Resource: editResource.Resource,
  8.             UserId: GetUserId(),
  9.             PropertyRelationSyncInfos: editResource.PropertyRelationSyncInfos,
  10.             ThrowOnNonSuccess: false,
  11.             IsCalledFromOutside: true,
  12.         };
  13.  
  14.         const crudServiceResult: CrudResult = await crudUpdate(crudUpdateConfig);
  15.        
  16.         switch (crudServiceResult.Type) {
  17.             case CrudResultType.Success:
  18.                 return sendSuccessResponse(res, crudServiceResult);
  19.             case CrudResultType.NotValid:
  20.                 return sendValidationErrorResponse(res, crudServiceResult.ValidationResult);
  21.             default:
  22.                 return sendInternalErrorResponse(res);
  23.         }
  24.     } catch (error) {
  25.         logger.error("Error in crudPut:", error);
  26.         return sendInternalErrorResponse(res);
  27.     }
  28. }
  29.  
  30. function sendSuccessResponse(res: NextApiResponse, result: CrudResultWithGeneric<WithId>) {
  31.     const response: ApiResponse<CrudResultWithGeneric<WithId>> = { data: result };
  32.     return res.status(HTTP_STATUS.OK).json(response);
  33. }
  34.  
  35. function sendValidationErrorResponse(res: NextApiResponse, validationResult: ValidationResult) {
  36.     const response: ApiResponse<ValidationResult> = { data: validationResult };
  37.     return res.status(HTTP_STATUS.BAD_REQUEST).json(response);
  38. }
  39.  
  40. function sendInternalErrorResponse(res: NextApiResponse) {
  41.     return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).end();
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement