mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
Refactor: move service logic out of api routes and into separate files
This commit is contained in:
22
src/services/BeerComment/editBeerCommentById.ts
Normal file
22
src/services/BeerComment/editBeerCommentById.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
interface EditBeerCommentByIdArgs {
|
||||
id: string;
|
||||
content: string;
|
||||
rating: number;
|
||||
}
|
||||
|
||||
const editBeerCommentById = async ({ id, content, rating }: EditBeerCommentByIdArgs) => {
|
||||
const updated = await DBClient.instance.beerComment.update({
|
||||
where: { id },
|
||||
data: {
|
||||
content,
|
||||
rating,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return updated;
|
||||
};
|
||||
|
||||
export default editBeerCommentById;
|
||||
11
src/services/BeerComment/findBeerCommentById.ts
Normal file
11
src/services/BeerComment/findBeerCommentById.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
const findBeerCommentById = async (id: string) => {
|
||||
const comment = await DBClient.instance.beerComment.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return comment;
|
||||
};
|
||||
|
||||
export default findBeerCommentById;
|
||||
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;
|
||||
@@ -1,6 +1,14 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
const findBeerPostLikeById = async (beerPostId: string, likedById: string) =>
|
||||
interface FindBeerPostLikeByIdArgs {
|
||||
beerPostId: string;
|
||||
likedById: string;
|
||||
}
|
||||
|
||||
const findBeerPostLikeById = async ({
|
||||
beerPostId,
|
||||
likedById,
|
||||
}: FindBeerPostLikeByIdArgs) =>
|
||||
DBClient.instance.beerPostLike.findFirst({ where: { beerPostId, likedById } });
|
||||
|
||||
export default findBeerPostLikeById;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const ImageMetadataValidationSchema = z.object({
|
||||
caption: z.string().min(1, { message: 'Caption is required.' }),
|
||||
alt: z.string().min(1, { message: 'Alt text is required.' }),
|
||||
});
|
||||
|
||||
export default ImageMetadataValidationSchema;
|
||||
Reference in New Issue
Block a user