mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
refactor: update comments services
This commit is contained in:
85
src/services/comments/beer-style-comment/index.ts
Normal file
85
src/services/comments/beer-style-comment/index.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import {
|
||||
CreateNewBeerStyleComment,
|
||||
GetAllBeerStyleComments,
|
||||
GetBeerStyleCommentCount,
|
||||
UpdateBeerStyleCommentById,
|
||||
FindOrDeleteBeerStyleCommentById,
|
||||
} from './types';
|
||||
|
||||
const beerStyleCommentSelect = {
|
||||
id: true,
|
||||
content: true,
|
||||
rating: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
postedBy: {
|
||||
select: { id: true, username: true, createdAt: true, userAvatar: true },
|
||||
},
|
||||
} as const;
|
||||
|
||||
export const createNewBeerStyleComment: CreateNewBeerStyleComment = ({
|
||||
body,
|
||||
userId,
|
||||
beerStyleId,
|
||||
}) => {
|
||||
const { content, rating } = body;
|
||||
return DBClient.instance.beerStyleComment.create({
|
||||
data: {
|
||||
content,
|
||||
rating,
|
||||
beerStyle: { connect: { id: beerStyleId } },
|
||||
postedBy: { connect: { id: userId } },
|
||||
},
|
||||
select: beerStyleCommentSelect,
|
||||
});
|
||||
};
|
||||
|
||||
export const getAllBeerStyleComments: GetAllBeerStyleComments = ({
|
||||
beerStyleId,
|
||||
pageNum,
|
||||
pageSize,
|
||||
}) => {
|
||||
return DBClient.instance.beerStyleComment.findMany({
|
||||
skip: (pageNum - 1) * pageSize,
|
||||
take: pageSize,
|
||||
where: { beerStyleId },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
select: beerStyleCommentSelect,
|
||||
});
|
||||
};
|
||||
|
||||
export const getBeerStyleCommentCount: GetBeerStyleCommentCount = ({ beerStyleId }) => {
|
||||
return DBClient.instance.beerStyleComment.count({ where: { beerStyleId } });
|
||||
};
|
||||
|
||||
export const updateBeerStyleCommentById: UpdateBeerStyleCommentById = ({
|
||||
body,
|
||||
beerStyleCommentId,
|
||||
}) => {
|
||||
const { content, rating } = body;
|
||||
|
||||
return DBClient.instance.beerStyleComment.update({
|
||||
where: { id: beerStyleCommentId },
|
||||
data: { content, rating, updatedAt: new Date() },
|
||||
select: beerStyleCommentSelect,
|
||||
});
|
||||
};
|
||||
|
||||
export const findBeerStyleCommentById: FindOrDeleteBeerStyleCommentById = ({
|
||||
beerStyleCommentId,
|
||||
}) => {
|
||||
return DBClient.instance.beerStyleComment.findUnique({
|
||||
where: { id: beerStyleCommentId },
|
||||
select: beerStyleCommentSelect,
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteBeerStyleCommentById: FindOrDeleteBeerStyleCommentById = ({
|
||||
beerStyleCommentId,
|
||||
}) => {
|
||||
return DBClient.instance.beerStyleComment.delete({
|
||||
where: { id: beerStyleCommentId },
|
||||
select: beerStyleCommentSelect,
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user