refactor image services

This commit is contained in:
Aaron William Po
2023-12-13 09:49:31 -05:00
parent 685c86e0c1
commit fdbadb63dc
17 changed files with 285 additions and 99 deletions

View File

@@ -1,9 +1,12 @@
import ServerError from '@/config/util/ServerError';
import addBeerImageToDB from '@/services/images/beer-image/addBeerImageToDB';
import {
addBeerImagesToDB,
deleteBeerImageFromDBAndStorage,
} from '@/services/images/beer-image';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse } from 'next';
import { z } from 'zod';
import { UploadImagesRequest } from '../types';
import { DeleteImageRequest, UploadImagesRequest } from '../types';
// eslint-disable-next-line import/prefer-default-export
export const processBeerImageData = async (
@@ -16,11 +19,10 @@ export const processBeerImageData = async (
throw new ServerError('No images uploaded', 400);
}
const beerImages = await addBeerImageToDB({
alt: body.alt,
caption: body.caption,
const beerImages = await addBeerImagesToDB({
beerPostId: req.query.id,
userId: user!.id,
body,
files,
});
@@ -32,3 +34,18 @@ export const processBeerImageData = async (
statusCode: 200,
});
};
export const deleteBeerImageData = async (
req: DeleteImageRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const { id } = req.query;
await deleteBeerImageFromDBAndStorage({ beerImageId: id });
res.status(200).json({
success: true,
message: `Successfully deleted image with id ${id}`,
statusCode: 200,
});
};