mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Refactor: further extract controller logic from routers
This commit is contained in:
34
src/controllers/images/beerImages/index.ts
Normal file
34
src/controllers/images/beerImages/index.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import addBeerImageToDB from '@/services/BeerImage/addBeerImageToDB';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { z } from 'zod';
|
||||
import { UploadBeerPostImagesRequest } from './types';
|
||||
|
||||
// eslint-disable-next-line import/prefer-default-export
|
||||
export const processBeerImageData = async (
|
||||
req: UploadBeerPostImagesRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const { files, user, body } = req;
|
||||
|
||||
if (!files || !files.length) {
|
||||
throw new ServerError('No images uploaded', 400);
|
||||
}
|
||||
|
||||
const beerImages = await addBeerImageToDB({
|
||||
alt: body.alt,
|
||||
caption: body.caption,
|
||||
beerPostId: req.query.id,
|
||||
userId: user!.id,
|
||||
files,
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: `Successfully uploaded ${beerImages.length} image${
|
||||
beerImages.length > 1 ? 's' : ''
|
||||
}`,
|
||||
statusCode: 200,
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user