mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Refactor: move service logic out of api routes and into separate files
This commit is contained in:
39
src/services/BeerImage/processImageDataIntoDB.ts
Normal file
39
src/services/BeerImage/processImageDataIntoDB.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { BeerImage } from '@prisma/client';
|
||||
import { z } from 'zod';
|
||||
import ImageMetadataValidationSchema from '../types/ImageSchema/ImageMetadataValidationSchema';
|
||||
|
||||
interface ProcessImageDataArgs {
|
||||
files: Express.Multer.File[];
|
||||
alt: z.infer<typeof ImageMetadataValidationSchema>['alt'];
|
||||
caption: z.infer<typeof ImageMetadataValidationSchema>['caption'];
|
||||
beerPostId: string;
|
||||
userId: string;
|
||||
}
|
||||
|
||||
const processImageDataIntoDB = ({
|
||||
alt,
|
||||
caption,
|
||||
files,
|
||||
beerPostId,
|
||||
userId,
|
||||
}: ProcessImageDataArgs) => {
|
||||
const beerImagePromises: Promise<BeerImage>[] = [];
|
||||
files.forEach((file) => {
|
||||
beerImagePromises.push(
|
||||
DBClient.instance.beerImage.create({
|
||||
data: {
|
||||
alt,
|
||||
caption,
|
||||
postedBy: { connect: { id: userId } },
|
||||
beerPost: { connect: { id: beerPostId } },
|
||||
path: file.path,
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
return Promise.all(beerImagePromises);
|
||||
};
|
||||
|
||||
export default processImageDataIntoDB;
|
||||
Reference in New Issue
Block a user