mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
feat: begin work on delete beer style
This commit is contained in:
@@ -8,9 +8,9 @@ import { NextApiResponse } from 'next';
|
|||||||
import { createRouter, NextHandler } from 'next-connect';
|
import { createRouter, NextHandler } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import ServerError from '@/config/util/ServerError';
|
import ServerError from '@/config/util/ServerError';
|
||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||||
|
import deleteBeerPostById from '@/services/BeerPost/deleteBeerPostById';
|
||||||
|
|
||||||
interface BeerPostRequest extends UserExtendedNextApiRequest {
|
interface BeerPostRequest extends UserExtendedNextApiRequest {
|
||||||
query: { id: string };
|
query: { id: string };
|
||||||
@@ -60,13 +60,9 @@ const editBeerPost = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => {
|
const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => {
|
||||||
const {
|
const { id } = req.query;
|
||||||
query: { id },
|
|
||||||
} = req;
|
|
||||||
|
|
||||||
const deleted = await DBClient.instance.beerPost.delete({
|
const deleted = deleteBeerPostById(id);
|
||||||
where: { id },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!deleted) {
|
if (!deleted) {
|
||||||
throw new ServerError('Beer post not found', 404);
|
throw new ServerError('Beer post not found', 404);
|
||||||
|
|||||||
28
src/services/BeerPost/deleteBeerPostById.ts
Normal file
28
src/services/BeerPost/deleteBeerPostById.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import DBClient from '@/prisma/DBClient';
|
||||||
|
import { z } from 'zod';
|
||||||
|
import BeerPostQueryResult from './schema/BeerPostQueryResult';
|
||||||
|
|
||||||
|
const deleteBeerPostById = async (
|
||||||
|
id: string,
|
||||||
|
): Promise<z.infer<typeof BeerPostQueryResult> | null> => {
|
||||||
|
const deleted = await DBClient.instance.beerPost.delete({
|
||||||
|
where: { id },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
name: true,
|
||||||
|
brewery: { select: { id: true, name: true } },
|
||||||
|
description: true,
|
||||||
|
beerImages: { select: { id: true, path: true, caption: true, alt: true } },
|
||||||
|
ibu: true,
|
||||||
|
abv: true,
|
||||||
|
style: { select: { id: true, name: true, description: true } },
|
||||||
|
postedBy: { select: { id: true, username: true } },
|
||||||
|
createdAt: true,
|
||||||
|
updatedAt: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return deleted;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default deleteBeerPostById;
|
||||||
24
src/services/BeerStyles/deleteBeerStyleById.ts
Normal file
24
src/services/BeerStyles/deleteBeerStyleById.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import DBClient from '@/prisma/DBClient';
|
||||||
|
import BeerStyleQueryResult from './schema/BeerStyleQueryResult';
|
||||||
|
|
||||||
|
const deleteBeerStyleById = async (id: string) => {
|
||||||
|
const deleted = await DBClient.instance.beerStyle.delete({
|
||||||
|
where: { id },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
name: true,
|
||||||
|
createdAt: true,
|
||||||
|
updatedAt: true,
|
||||||
|
abvRange: true,
|
||||||
|
ibuRange: true,
|
||||||
|
description: true,
|
||||||
|
postedBy: { select: { id: true, username: true } },
|
||||||
|
glassware: { select: { id: true, name: true } },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return deleted as z.infer<typeof BeerStyleQueryResult> | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default deleteBeerStyleById;
|
||||||
@@ -6,7 +6,7 @@ const getAllBeerStyles = async (
|
|||||||
pageNum: number,
|
pageNum: number,
|
||||||
pageSize: number,
|
pageSize: number,
|
||||||
): Promise<z.infer<typeof BeerStyleQueryResult>[]> => {
|
): Promise<z.infer<typeof BeerStyleQueryResult>[]> => {
|
||||||
const styles = (await DBClient.instance.beerStyle.findMany({
|
const styles = await DBClient.instance.beerStyle.findMany({
|
||||||
take: pageSize,
|
take: pageSize,
|
||||||
skip: (pageNum - 1) * pageSize,
|
skip: (pageNum - 1) * pageSize,
|
||||||
select: {
|
select: {
|
||||||
@@ -20,9 +20,9 @@ const getAllBeerStyles = async (
|
|||||||
description: true,
|
description: true,
|
||||||
glassware: { select: { id: true, name: true } },
|
glassware: { select: { id: true, name: true } },
|
||||||
},
|
},
|
||||||
})) as z.infer<typeof BeerStyleQueryResult>[];
|
});
|
||||||
|
|
||||||
return styles;
|
return styles as z.infer<typeof BeerStyleQueryResult>[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export default getAllBeerStyles;
|
export default getAllBeerStyles;
|
||||||
|
|||||||
Reference in New Issue
Block a user