mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-07-17 01:47:22 +00:00
Move next js project to archive (#207)
This commit is contained in:
276
archive/next-js-web-app/src/services/posts/beer-post/index.ts
Normal file
276
archive/next-js-web-app/src/services/posts/beer-post/index.ts
Normal file
@@ -0,0 +1,276 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import {
|
||||
CreateNewBeerPost,
|
||||
EditBeerPostById,
|
||||
FindOrDeleteBeerPostById,
|
||||
GetAllBeerPosts,
|
||||
GetAllBeerPostsByBreweryId,
|
||||
GetAllBeerPostsByPostedById,
|
||||
GetAllBeerPostsByStyleId,
|
||||
GetBeerRecommendations,
|
||||
} from './types';
|
||||
|
||||
/**
|
||||
* The select object for retrieving beer posts.
|
||||
*
|
||||
* Satisfies the BeerPostQueryResult zod schema.
|
||||
*
|
||||
* @example
|
||||
* const beerPosts = await DBClient.instance.beerPost.findMany({
|
||||
* select: beerPostSelect,
|
||||
* });
|
||||
*/
|
||||
const beerPostSelect = {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
abv: true,
|
||||
ibu: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
beerImages: {
|
||||
select: {
|
||||
alt: true,
|
||||
path: true,
|
||||
caption: true,
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
},
|
||||
brewery: { select: { id: true, name: true } },
|
||||
style: { select: { id: true, name: true, description: true } },
|
||||
postedBy: { select: { id: true, username: true } },
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Creates a new beer post.
|
||||
*
|
||||
* @param params - The parameters object for creating the beer post.
|
||||
* @param params.name - The name of the beer.
|
||||
* @param params.description - The description of the beer.
|
||||
* @param params.abv - The alcohol by volume of the beer.
|
||||
* @param params.ibu - The International Bitterness Units of the beer.
|
||||
* @param params.styleId - The ID of the beer style.
|
||||
* @param params.breweryId - The ID of the brewery.
|
||||
* @param params.userId - The ID of the user who posted the beer.
|
||||
* @returns A promise that resolves to the newly created beer post.
|
||||
*/
|
||||
export const createNewBeerPost: CreateNewBeerPost = ({
|
||||
name,
|
||||
description,
|
||||
abv,
|
||||
ibu,
|
||||
styleId,
|
||||
breweryId,
|
||||
userId,
|
||||
}) => {
|
||||
return DBClient.instance.beerPost.create({
|
||||
data: {
|
||||
name,
|
||||
description,
|
||||
abv,
|
||||
ibu,
|
||||
style: { connect: { id: styleId } },
|
||||
postedBy: { connect: { id: userId } },
|
||||
brewery: { connect: { id: breweryId } },
|
||||
},
|
||||
select: beerPostSelect,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves a beer post by ID.
|
||||
*
|
||||
* @param params - The parameters object for retrieving the beer post.
|
||||
* @param params.beerPostId - The ID of the beer post to retrieve.
|
||||
* @returns A promise that resolves to the beer post.
|
||||
*/
|
||||
export const getBeerPostById: FindOrDeleteBeerPostById = async ({ beerPostId }) => {
|
||||
return DBClient.instance.beerPost.findFirst({
|
||||
where: { id: beerPostId },
|
||||
select: beerPostSelect,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves all beer posts with pagination.
|
||||
*
|
||||
* @param params - The parameters object for retrieving beer posts.
|
||||
* @param params.pageNum The page number to retrieve.
|
||||
* @param params.pageSize The number of beer posts per page.
|
||||
* @returns An object containing the beer posts and the total count.
|
||||
*/
|
||||
export const getAllBeerPostsService: GetAllBeerPosts = async ({ pageNum, pageSize }) => {
|
||||
const beerPosts = await DBClient.instance.beerPost.findMany({
|
||||
select: beerPostSelect,
|
||||
take: pageSize,
|
||||
skip: (pageNum - 1) * pageSize,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
|
||||
const count = await DBClient.instance.beerPost.count();
|
||||
|
||||
return { beerPosts, count };
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves beer posts by beer style ID.
|
||||
*
|
||||
* @param params - The parameters object for retrieving beer posts.
|
||||
* @param params.pageNum - The page number of the results.
|
||||
* @param params.pageSize - The number of results per page.
|
||||
* @param params.styleId - The ID of the beer style.
|
||||
* @returns A promise that resolves to an object containing the beer posts and the total
|
||||
* count.
|
||||
*/
|
||||
export const getBeerPostsByBeerStyleIdService: GetAllBeerPostsByStyleId = async ({
|
||||
pageNum,
|
||||
pageSize,
|
||||
styleId,
|
||||
}) => {
|
||||
const beerPosts = await DBClient.instance.beerPost.findMany({
|
||||
where: { styleId },
|
||||
take: pageSize,
|
||||
skip: (pageNum - 1) * pageSize,
|
||||
select: beerPostSelect,
|
||||
});
|
||||
|
||||
const count = await DBClient.instance.beerPost.count({
|
||||
where: { styleId },
|
||||
});
|
||||
|
||||
return { beerPosts, count };
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves beer posts by brewery ID.
|
||||
*
|
||||
* @param params - The parameters object for retrieving beer posts.
|
||||
* @param params.pageNum - The page number of the results.
|
||||
* @param params.pageSize - The number of beer posts per page.
|
||||
* @param params.breweryId - The ID of the brewery.
|
||||
* @returns A promise that resolves to an object containing the beer posts and the total
|
||||
* count.
|
||||
*/
|
||||
export const getBeerPostsByBreweryIdService: GetAllBeerPostsByBreweryId = async ({
|
||||
pageNum,
|
||||
pageSize,
|
||||
breweryId,
|
||||
}) => {
|
||||
const beerPosts = await DBClient.instance.beerPost.findMany({
|
||||
where: { breweryId },
|
||||
take: pageSize,
|
||||
skip: (pageNum - 1) * pageSize,
|
||||
select: beerPostSelect,
|
||||
});
|
||||
|
||||
const count = await DBClient.instance.beerPost.count({
|
||||
where: { breweryId },
|
||||
});
|
||||
|
||||
return { beerPosts, count };
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves beer posts by the ID of the user who posted them.
|
||||
*
|
||||
* @param params - The parameters object for retrieving beer posts.
|
||||
* @param params.pageNum The page number of the results.
|
||||
* @param params.pageSize The number of results per page.
|
||||
* @param params.postedById The ID of the user who posted the beer posts.
|
||||
* @returns A promise that resolves to an object containing the beer posts and the total
|
||||
* count.
|
||||
*/
|
||||
export const getBeerPostsByPostedByIdService: GetAllBeerPostsByPostedById = async ({
|
||||
pageNum,
|
||||
pageSize,
|
||||
postedById,
|
||||
}) => {
|
||||
const beerPosts = await DBClient.instance.beerPost.findMany({
|
||||
where: { postedById },
|
||||
take: pageSize,
|
||||
skip: (pageNum - 1) * pageSize,
|
||||
select: beerPostSelect,
|
||||
});
|
||||
|
||||
const count = await DBClient.instance.beerPost.count({
|
||||
where: { postedById },
|
||||
});
|
||||
|
||||
return { beerPosts, count };
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves beer recommendations based on the given parameters.
|
||||
*
|
||||
* @param params - The parameters object for retrieving beer recommendations.
|
||||
* @param params.beerPost - The beer post for which recommendations are requested.
|
||||
* @param params.pageNum - The page number of the recommendations.
|
||||
* @param params.pageSize - The number of recommendations per page.
|
||||
* @returns A promise that resolves to an object containing the beer recommendations and
|
||||
* the total count.
|
||||
*/
|
||||
export const getBeerRecommendationsService: GetBeerRecommendations = async ({
|
||||
beerPost,
|
||||
pageNum,
|
||||
pageSize,
|
||||
}) => {
|
||||
const beerRecommendations = await DBClient.instance.beerPost.findMany({
|
||||
where: {
|
||||
OR: [{ styleId: beerPost.style.id }, { breweryId: beerPost.brewery.id }],
|
||||
NOT: { id: beerPost.id },
|
||||
},
|
||||
take: pageSize,
|
||||
skip: (pageNum - 1) * pageSize,
|
||||
select: beerPostSelect,
|
||||
});
|
||||
|
||||
const count = await DBClient.instance.beerPost.count({
|
||||
where: {
|
||||
OR: [{ styleId: beerPost.style.id }, { breweryId: beerPost.brewery.id }],
|
||||
NOT: { id: beerPost.id },
|
||||
},
|
||||
});
|
||||
|
||||
return { beerRecommendations, count };
|
||||
};
|
||||
|
||||
/**
|
||||
* Service for editing a beer post by ID.
|
||||
*
|
||||
* @param params - The parameters object for editing the beer post.
|
||||
* @param params.beerPostId - The ID of the beer post to edit.
|
||||
* @param params.body - The updated data for the beer post.
|
||||
* @param params.body.abv - The updated ABV (Alcohol By Volume) of the beer.
|
||||
* @param params.body.description - The updated description of the beer.
|
||||
* @param params.body.ibu - The updated IBU (International Bitterness Units) of the beer.
|
||||
* @param params.body.name - The updated name of the beer.
|
||||
* @returns - A promise that resolves to the updated beer post.
|
||||
*/
|
||||
export const editBeerPostByIdService: EditBeerPostById = async ({
|
||||
body: { abv, description, ibu, name },
|
||||
beerPostId,
|
||||
}) => {
|
||||
return DBClient.instance.beerPost.update({
|
||||
where: { id: beerPostId },
|
||||
data: { abv, description, ibu, name },
|
||||
select: beerPostSelect,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Service for deleting a beer post by ID.
|
||||
*
|
||||
* @param params - The parameters object for deleting the beer post.
|
||||
* @param params.beerPostId - The ID of the beer post to delete.
|
||||
* @returns - A promise that resolves to the deleted beer post.
|
||||
*/
|
||||
export const deleteBeerPostByIdService: FindOrDeleteBeerPostById = async ({
|
||||
beerPostId,
|
||||
}) => {
|
||||
return DBClient.instance.beerPost.delete({
|
||||
where: { id: beerPostId },
|
||||
select: beerPostSelect,
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
import ImageQueryValidationSchema from '@/services/schema/ImageSchema/ImageQueryValidationSchema';
|
||||
import { z } from 'zod';
|
||||
|
||||
const BeerPostQueryResult = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
brewery: z.object({ id: z.string(), name: z.string() }),
|
||||
description: z.string(),
|
||||
beerImages: z.array(ImageQueryValidationSchema),
|
||||
ibu: z.number(),
|
||||
abv: z.number(),
|
||||
style: z.object({ id: z.string(), name: z.string(), description: z.string() }),
|
||||
postedBy: z.object({ id: z.string(), username: z.string() }),
|
||||
createdAt: z.coerce.date(),
|
||||
updatedAt: z.coerce.date().nullable(),
|
||||
});
|
||||
|
||||
export default BeerPostQueryResult;
|
||||
@@ -0,0 +1,43 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const CreateBeerPostValidationSchema = z.object({
|
||||
name: z
|
||||
.string({
|
||||
required_error: 'Beer name is required.',
|
||||
invalid_type_error: 'Beer name must be a string.',
|
||||
})
|
||||
.min(1, { message: 'Beer name is required.' })
|
||||
.max(100, { message: 'Beer name is too long.' }),
|
||||
description: z
|
||||
.string()
|
||||
.min(1, { message: 'Description is required.' })
|
||||
.max(500, { message: 'Description is too long.' }),
|
||||
abv: z
|
||||
.number({
|
||||
required_error: 'ABV is required.',
|
||||
invalid_type_error: 'ABV must be a number.',
|
||||
})
|
||||
.min(0.1, { message: 'ABV must be greater than 0.1.' })
|
||||
.max(50, { message: 'ABV must be less than 50.' }),
|
||||
ibu: z
|
||||
.number({
|
||||
required_error: 'IBU is required.',
|
||||
invalid_type_error: 'IBU must be a number.',
|
||||
})
|
||||
.min(2, { message: 'IBU must be greater than 2.' })
|
||||
.max(100, { message: 'IBU must be less than 100.' }),
|
||||
styleId: z
|
||||
.string({
|
||||
required_error: 'Style id is required.',
|
||||
invalid_type_error: 'Style id must be a string.',
|
||||
})
|
||||
.cuid({ message: 'Invalid type id.' }),
|
||||
breweryId: z
|
||||
.string({
|
||||
required_error: 'Brewery id is required.',
|
||||
invalid_type_error: 'Brewery id must be a string.',
|
||||
})
|
||||
.cuid({ message: 'Invalid brewery id.' }),
|
||||
});
|
||||
|
||||
export default CreateBeerPostValidationSchema;
|
||||
@@ -0,0 +1,9 @@
|
||||
import { z } from 'zod';
|
||||
import CreateBeerPostValidationSchema from './CreateBeerPostValidationSchema';
|
||||
|
||||
const EditBeerPostValidationSchema = CreateBeerPostValidationSchema.omit({
|
||||
breweryId: true,
|
||||
styleId: true,
|
||||
}).extend({ id: z.string().cuid() });
|
||||
|
||||
export default EditBeerPostValidationSchema;
|
||||
@@ -0,0 +1,60 @@
|
||||
import { z } from 'zod';
|
||||
import BeerPostQueryResult from '../schema/BeerPostQueryResult';
|
||||
import EditBeerPostValidationSchema from '../schema/EditBeerPostValidationSchema';
|
||||
import CreateBeerPostValidationSchema from '../schema/CreateBeerPostValidationSchema';
|
||||
|
||||
const CreateSchema = CreateBeerPostValidationSchema.extend({
|
||||
userId: z.string().cuid(),
|
||||
});
|
||||
const EditSchema = EditBeerPostValidationSchema.omit({ id: true });
|
||||
|
||||
type BeerPost = z.infer<typeof BeerPostQueryResult>;
|
||||
|
||||
export type CreateNewBeerPost = (args: z.infer<typeof CreateSchema>) => Promise<BeerPost>;
|
||||
|
||||
export type EditBeerPostById = (args: {
|
||||
beerPostId: string;
|
||||
body: z.infer<typeof EditSchema>;
|
||||
}) => Promise<BeerPost>;
|
||||
|
||||
export type FindOrDeleteBeerPostById = (args: {
|
||||
beerPostId: string;
|
||||
}) => Promise<BeerPost | null>;
|
||||
|
||||
export type GetAllBeerPosts = (args: { pageNum: number; pageSize: number }) => Promise<{
|
||||
beerPosts: BeerPost[];
|
||||
count: number;
|
||||
}>;
|
||||
|
||||
export type GetAllBeerPostsByPostedById = (args: {
|
||||
postedById: string;
|
||||
pageSize: number;
|
||||
pageNum: number;
|
||||
}) => Promise<{
|
||||
beerPosts: BeerPost[];
|
||||
count: number;
|
||||
}>;
|
||||
|
||||
export type GetAllBeerPostsByStyleId = (args: {
|
||||
styleId: string;
|
||||
pageSize: number;
|
||||
pageNum: number;
|
||||
}) => Promise<{
|
||||
beerPosts: BeerPost[];
|
||||
count: number;
|
||||
}>;
|
||||
|
||||
export type GetAllBeerPostsByBreweryId = (args: {
|
||||
breweryId: string;
|
||||
pageSize: number;
|
||||
pageNum: number;
|
||||
}) => Promise<{
|
||||
beerPosts: BeerPost[];
|
||||
count: number;
|
||||
}>;
|
||||
|
||||
export type GetBeerRecommendations = (args: {
|
||||
beerPost: BeerPost;
|
||||
pageNum: number;
|
||||
pageSize: number;
|
||||
}) => Promise<{ beerRecommendations: BeerPost[]; count: number }>;
|
||||
@@ -0,0 +1,182 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import type {
|
||||
CreateBeerStyle,
|
||||
DeleteBeerStyleById,
|
||||
EditBeerStyleById,
|
||||
GetAllBeerStyles,
|
||||
GetBeerStyleById,
|
||||
} from '@/services/posts/beer-style-post/types';
|
||||
|
||||
/**
|
||||
* The select object for retrieving beer styles.
|
||||
*
|
||||
* Satisfies the BeerStyleQueryResult zod schema.
|
||||
*
|
||||
* @remarks
|
||||
* Prisma does not support tuples, so we have to typecast the ibuRange and abvRange fields
|
||||
* to satisfy the zod schema.
|
||||
* @example
|
||||
* const beerStyles = await DBClient.instance.beerStyle.findMany({
|
||||
* select: beerStyleSelect,
|
||||
* });
|
||||
*/
|
||||
const beerStyleSelect = {
|
||||
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 } },
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Deletes a beer style by id.
|
||||
*
|
||||
* @param args - The arguments for the service.
|
||||
* @param args.beerStyleId - The id of the beer style to delete.
|
||||
* @returns The deleted beer style.
|
||||
*/
|
||||
export const deleteBeerStyleService: DeleteBeerStyleById = async ({ beerStyleId }) => {
|
||||
const deleted = await DBClient.instance.beerStyle.delete({
|
||||
where: { id: beerStyleId },
|
||||
select: beerStyleSelect,
|
||||
});
|
||||
|
||||
return deleted as Awaited<ReturnType<typeof deleteBeerStyleService>>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Edits a beer style by id.
|
||||
*
|
||||
* @param args - The arguments for the service.
|
||||
* @param args.beerStyleId - The id of the beer style to edit.
|
||||
* @param args.body - The data to update the beer style with.
|
||||
* @param args.body.abvRange - The abv range of the beer style.
|
||||
* @param args.body.description - The description of the beer style.
|
||||
* @param args.body.glasswareId - The id of the glassware to connect to the beer style.
|
||||
* @param args.body.ibuRange - The ibu range of the beer style.
|
||||
* @param args.body.name - The name of the beer style.
|
||||
* @returns The updated beer style.
|
||||
*/
|
||||
export const editBeerStyleService: EditBeerStyleById = async ({ beerStyleId, body }) => {
|
||||
const { abvRange, description, glasswareId, ibuRange, name } = body;
|
||||
|
||||
const glassware = await DBClient.instance.glassware.findUnique({
|
||||
where: { id: glasswareId },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!glassware) {
|
||||
throw new ServerError(
|
||||
'A glassware with that id does not exist and cannot be connected.',
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
||||
const updated = await DBClient.instance.beerStyle.update({
|
||||
where: { id: beerStyleId },
|
||||
data: {
|
||||
abvRange,
|
||||
description,
|
||||
ibuRange,
|
||||
name,
|
||||
glassware: { connect: { id: glasswareId } },
|
||||
},
|
||||
select: beerStyleSelect,
|
||||
});
|
||||
|
||||
return updated as Awaited<ReturnType<typeof editBeerStyleService>>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets all beer styles with pagination.
|
||||
*
|
||||
* @param args - The arguments for the service.
|
||||
* @param args.pageNum - The page number of the results.
|
||||
* @param args.pageSize - The page size of the results.
|
||||
* @returns The beer styles and the total count of beer styles.
|
||||
*/
|
||||
export const getAllBeerStylesService: GetAllBeerStyles = async ({
|
||||
pageNum,
|
||||
pageSize,
|
||||
}) => {
|
||||
const beerStyles = await DBClient.instance.beerStyle.findMany({
|
||||
take: pageSize,
|
||||
skip: (pageNum - 1) * pageSize,
|
||||
select: beerStyleSelect,
|
||||
});
|
||||
|
||||
const beerStyleCount = await DBClient.instance.beerStyle.count();
|
||||
|
||||
return {
|
||||
beerStyles: beerStyles as Awaited<
|
||||
ReturnType<typeof getAllBeerStylesService>
|
||||
>['beerStyles'],
|
||||
beerStyleCount,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets a beer style by id.
|
||||
*
|
||||
* @param args - The arguments for the service.
|
||||
* @param args.beerStyleId - The id of the beer style to get.
|
||||
* @returns The beer style.
|
||||
*/
|
||||
export const getBeerStyleByIdService: GetBeerStyleById = async ({ beerStyleId }) => {
|
||||
const beerStyle = await DBClient.instance.beerStyle.findUnique({
|
||||
where: { id: beerStyleId },
|
||||
select: beerStyleSelect,
|
||||
});
|
||||
|
||||
return beerStyle as Awaited<ReturnType<typeof getBeerStyleByIdService>>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a beer style.
|
||||
*
|
||||
* @param args - The arguments for the service.
|
||||
* @param args.body - The data to create the beer style with.
|
||||
* @param args.body.abvRange - The abv range of the beer style.
|
||||
* @param args.body.description - The description of the beer style.
|
||||
* @param args.body.glasswareId - The id of the glassware to connect to the beer style.
|
||||
* @param args.body.ibuRange - The ibu range of the beer style.
|
||||
* @param args.body.name - The name of the beer style.
|
||||
* @param args.glasswareId - The id of the glassware to connect to the beer style.
|
||||
* @param args.postedById - The id of the user who posted the beer style.
|
||||
*/
|
||||
export const createBeerStyleService: CreateBeerStyle = async ({
|
||||
body: { abvRange, description, ibuRange, name },
|
||||
glasswareId,
|
||||
postedById,
|
||||
}) => {
|
||||
const glassware = await DBClient.instance.glassware.findUnique({
|
||||
where: { id: glasswareId },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!glassware) {
|
||||
throw new ServerError(
|
||||
'A glassware with that id does not exist and cannot be connected.',
|
||||
404,
|
||||
);
|
||||
}
|
||||
|
||||
const beerStyle = await DBClient.instance.beerStyle.create({
|
||||
data: {
|
||||
name,
|
||||
description,
|
||||
abvRange,
|
||||
ibuRange,
|
||||
glassware: { connect: { id: glasswareId } },
|
||||
postedBy: { connect: { id: postedById } },
|
||||
},
|
||||
select: beerStyleSelect,
|
||||
});
|
||||
|
||||
return beerStyle as Awaited<ReturnType<typeof createBeerStyleService>>;
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const BeerStyleQueryResult = z.object({
|
||||
abvRange: z.tuple([z.number(), z.number()]),
|
||||
createdAt: z.coerce.date(),
|
||||
description: z.string(),
|
||||
glassware: z.object({ id: z.string().cuid(), name: z.string() }),
|
||||
ibuRange: z.tuple([z.number(), z.number()]),
|
||||
id: z.string().cuid(),
|
||||
name: z.string(),
|
||||
postedBy: z.object({ id: z.string().cuid(), username: z.string() }),
|
||||
updatedAt: z.coerce.date().nullable(),
|
||||
});
|
||||
|
||||
export default BeerStyleQueryResult;
|
||||
@@ -0,0 +1,46 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const CreateBeerStyleValidationSchema = z.object({
|
||||
glasswareId: z
|
||||
.string()
|
||||
.cuid({
|
||||
message: 'Glassware ID must be a valid CUID.',
|
||||
})
|
||||
.min(1, { message: 'Glassware ID is required.' }),
|
||||
description: z
|
||||
.string()
|
||||
.min(1, { message: 'Description is required.' })
|
||||
.max(500, { message: 'Description must be less than or equal to 500 characters.' }),
|
||||
ibuRange: z
|
||||
.tuple([
|
||||
z
|
||||
.number()
|
||||
.min(0, { message: 'IBU range minimum must be greater than or equal to 0.' })
|
||||
.max(100, { message: 'IBU range minimum must be less than or equal to 100.' }),
|
||||
z
|
||||
.number()
|
||||
.min(0, { message: 'IBU range maximum must be greater than or equal to 0.' })
|
||||
.max(100, { message: 'IBU range maximum must be less than or equal to 100.' }),
|
||||
])
|
||||
.refine((ibuRange) => ibuRange[0] <= ibuRange[1], {
|
||||
message: 'IBU range minimum must be less than or equal to maximum.',
|
||||
}),
|
||||
abvRange: z
|
||||
.tuple([
|
||||
z
|
||||
.number()
|
||||
.min(0, { message: 'ABV range minimum must be greater than or equal to 0.' }),
|
||||
z
|
||||
.number()
|
||||
.min(0, { message: 'ABV range maximum must be greater than or equal to 0.' }),
|
||||
])
|
||||
.refine((abvRange) => abvRange[0] <= abvRange[1], {
|
||||
message: 'ABV range minimum must be less than or equal to maximum.',
|
||||
}),
|
||||
name: z
|
||||
.string()
|
||||
.min(1, { message: 'Name is required.' })
|
||||
.max(100, { message: 'Name must be less than or equal to 100 characters.' }),
|
||||
});
|
||||
|
||||
export default CreateBeerStyleValidationSchema;
|
||||
@@ -0,0 +1,39 @@
|
||||
import { z } from 'zod';
|
||||
import BeerStyleQueryResult from '../schema/BeerStyleQueryResult';
|
||||
|
||||
type BeerStyle = z.infer<typeof BeerStyleQueryResult>;
|
||||
|
||||
export type GetBeerStyleById = (args: {
|
||||
beerStyleId: string;
|
||||
}) => Promise<BeerStyle | null>;
|
||||
|
||||
export type DeleteBeerStyleById = (args: {
|
||||
beerStyleId: string;
|
||||
}) => Promise<BeerStyle | null>;
|
||||
|
||||
export type EditBeerStyleById = (args: {
|
||||
beerStyleId: string;
|
||||
body: {
|
||||
name: string;
|
||||
description: string;
|
||||
abvRange: [number, number];
|
||||
ibuRange: [number, number];
|
||||
glasswareId: string;
|
||||
};
|
||||
}) => Promise<BeerStyle | null>;
|
||||
|
||||
export type GetAllBeerStyles = (args: { pageNum: number; pageSize: number }) => Promise<{
|
||||
beerStyles: BeerStyle[];
|
||||
beerStyleCount: number;
|
||||
}>;
|
||||
|
||||
export type CreateBeerStyle = (args: {
|
||||
body: {
|
||||
name: string;
|
||||
description: string;
|
||||
abvRange: [number, number];
|
||||
ibuRange: [number, number];
|
||||
};
|
||||
glasswareId: string;
|
||||
postedById: string;
|
||||
}) => Promise<BeerStyle>;
|
||||
241
archive/next-js-web-app/src/services/posts/brewery-post/index.ts
Normal file
241
archive/next-js-web-app/src/services/posts/brewery-post/index.ts
Normal file
@@ -0,0 +1,241 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
import {
|
||||
CreateBreweryPostLocation,
|
||||
CreateNewBreweryPost,
|
||||
GetAllBreweryPosts,
|
||||
GetAllBreweryPostsByPostedById,
|
||||
GetBreweryPostById,
|
||||
GetMapBreweryPosts,
|
||||
UpdateBreweryPost,
|
||||
} from './types';
|
||||
|
||||
/**
|
||||
* The select object to use when querying for brewery posts.
|
||||
*
|
||||
* @remarks
|
||||
* Prisma does not support tuples, so we have to typecast the coordinates field to
|
||||
* [number, number] in order to satisfy the zod schema.
|
||||
*/
|
||||
const breweryPostSelect = {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
createdAt: true,
|
||||
dateEstablished: true,
|
||||
postedBy: { select: { id: true, username: true } },
|
||||
breweryImages: {
|
||||
select: {
|
||||
path: true,
|
||||
caption: true,
|
||||
id: true,
|
||||
alt: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
},
|
||||
location: {
|
||||
select: {
|
||||
city: true,
|
||||
address: true,
|
||||
coordinates: true,
|
||||
country: true,
|
||||
stateOrProvince: true,
|
||||
},
|
||||
},
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Creates a new brewery post.
|
||||
*
|
||||
* @param args - The arguments to create a new brewery post.
|
||||
* @param args.name - The name of the brewery.
|
||||
* @param args.description - The description of the brewery.
|
||||
* @param args.dateEstablished - The date the brewery was established.
|
||||
* @param args.userId - The id of the user who created the brewery post.
|
||||
* @param args.locationId - The id of the location of the brewery.
|
||||
* @returns The newly created brewery post.
|
||||
*/
|
||||
export const createNewBreweryPostService: CreateNewBreweryPost = async ({
|
||||
dateEstablished,
|
||||
description,
|
||||
locationId,
|
||||
name,
|
||||
userId,
|
||||
}) => {
|
||||
const post = (await DBClient.instance.breweryPost.create({
|
||||
data: {
|
||||
name,
|
||||
description,
|
||||
dateEstablished,
|
||||
location: { connect: { id: locationId } },
|
||||
postedBy: { connect: { id: userId } },
|
||||
},
|
||||
select: breweryPostSelect,
|
||||
})) as Awaited<ReturnType<typeof createNewBreweryPostService>>;
|
||||
|
||||
return post;
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves all brewery posts paginated.
|
||||
*
|
||||
* @param args - The arguments to get all brewery posts.
|
||||
* @param args.pageNum - The page number of the brewery posts to get.
|
||||
* @param args.pageSize - The number of brewery posts to get per page.
|
||||
* @returns All brewery posts.
|
||||
*/
|
||||
export const getAllBreweryPostsService: GetAllBreweryPosts = async ({
|
||||
pageNum,
|
||||
pageSize,
|
||||
}) => {
|
||||
const breweryPosts = (await DBClient.instance.breweryPost.findMany({
|
||||
take: pageSize,
|
||||
skip: (pageNum - 1) * pageSize,
|
||||
select: breweryPostSelect,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
})) as Awaited<ReturnType<typeof getAllBreweryPostsService>>['breweryPosts'];
|
||||
|
||||
const count = await DBClient.instance.breweryPost.count();
|
||||
return { breweryPosts, count };
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves a brewery post by ID.
|
||||
*
|
||||
* @param args - The arguments to get a brewery post by ID.
|
||||
* @param args.breweryPostId - The ID of the brewery post to get.
|
||||
* @returns The brewery post.
|
||||
*/
|
||||
export const getBreweryPostByIdService: GetBreweryPostById = async ({
|
||||
breweryPostId,
|
||||
}) => {
|
||||
const breweryPost = await DBClient.instance.breweryPost.findFirst({
|
||||
select: breweryPostSelect,
|
||||
where: { id: breweryPostId },
|
||||
});
|
||||
|
||||
return breweryPost as Awaited<ReturnType<typeof getBreweryPostByIdService>>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves all brewery posts by posted by ID.
|
||||
*
|
||||
* @param args - The arguments to get all brewery posts by posted by ID.
|
||||
* @param args.pageNum - The page number of the brewery posts to get.
|
||||
* @param args.pageSize - The number of brewery posts to get per page.
|
||||
* @param args.postedById - The ID of the user who posted the brewery posts.
|
||||
*/
|
||||
export const getAllBreweryPostsByPostedByIdService: GetAllBreweryPostsByPostedById =
|
||||
async ({ pageNum, pageSize, postedById }) => {
|
||||
const breweryPosts = (await DBClient.instance.breweryPost.findMany({
|
||||
where: { postedBy: { id: postedById } },
|
||||
take: pageSize,
|
||||
skip: (pageNum - 1) * pageSize,
|
||||
select: breweryPostSelect,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
})) as Awaited<
|
||||
ReturnType<typeof getAllBreweryPostsByPostedByIdService>
|
||||
>['breweryPosts'];
|
||||
|
||||
const count = await DBClient.instance.breweryPost.count({
|
||||
where: { postedBy: { id: postedById } },
|
||||
});
|
||||
|
||||
return { breweryPosts, count };
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a brewery post location.
|
||||
*
|
||||
* @param args - The arguments to create a brewery post location.
|
||||
* @param args.body - The body of the request.
|
||||
* @param args.body.address - The address of the brewery.
|
||||
* @param args.body.city - The city of the brewery.
|
||||
* @param args.body.country - The country of the brewery.
|
||||
* @param args.body.stateOrProvince - The state or province of the brewery.
|
||||
* @param args.body.coordinates - The coordinates of the brewery in an array of [latitude,
|
||||
* longitude].
|
||||
* @param args.postedById - The ID of the user who posted the brewery post.
|
||||
* @returns The newly created brewery post location.
|
||||
*/
|
||||
export const createBreweryPostLocationService: CreateBreweryPostLocation = async ({
|
||||
body: { address, city, country, stateOrProvince, coordinates },
|
||||
postedById,
|
||||
}) => {
|
||||
const [latitude, longitude] = coordinates;
|
||||
|
||||
return DBClient.instance.breweryLocation.create({
|
||||
data: {
|
||||
address,
|
||||
city,
|
||||
country,
|
||||
stateOrProvince,
|
||||
coordinates: [latitude, longitude],
|
||||
postedBy: { connect: { id: postedById } },
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets all brewery posts for the post map.
|
||||
*
|
||||
* @param args - The arguments to get all brewery posts for the post map.
|
||||
* @param args.pageNum - The page number of the brewery posts to get.
|
||||
* @param args.pageSize - The number of brewery posts to get per page.
|
||||
* @returns All brewery posts for the post map.
|
||||
*/
|
||||
export const getMapBreweryPostsService: GetMapBreweryPosts = async ({
|
||||
pageNum,
|
||||
pageSize,
|
||||
}) => {
|
||||
const breweryPosts = await DBClient.instance.breweryPost.findMany({
|
||||
take: pageSize,
|
||||
skip: (pageNum - 1) * pageSize,
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
location: {
|
||||
select: { coordinates: true, city: true, country: true, stateOrProvince: true },
|
||||
},
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
|
||||
const count = await DBClient.instance.breweryPost.count();
|
||||
return { breweryPosts, count };
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates a brewery post.
|
||||
*
|
||||
* @param args - The arguments to update a brewery post.
|
||||
* @param args.breweryPostId - The ID of the brewery post to update.
|
||||
* @param args.body - The body of the request.
|
||||
* @param args.body.name - The name of the brewery.
|
||||
* @param args.body.description - The description of the brewery.
|
||||
* @param args.body.dateEstablished - The date the brewery was established.
|
||||
* @returns The updated brewery post.
|
||||
*/
|
||||
export const updateBreweryPostService: UpdateBreweryPost = async ({
|
||||
breweryPostId,
|
||||
body,
|
||||
}) => {
|
||||
const breweryPost = await DBClient.instance.breweryPost.update({
|
||||
where: { id: breweryPostId },
|
||||
data: body,
|
||||
select: breweryPostSelect,
|
||||
});
|
||||
|
||||
return breweryPost as Awaited<ReturnType<typeof updateBreweryPostService>>;
|
||||
};
|
||||
|
||||
export const deleteBreweryPostService: GetBreweryPostById = async ({ breweryPostId }) => {
|
||||
const breweryPost = await DBClient.instance.breweryPost.delete({
|
||||
where: { id: breweryPostId },
|
||||
select: breweryPostSelect,
|
||||
});
|
||||
|
||||
return breweryPost as Awaited<ReturnType<typeof deleteBreweryPostService>>;
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const BreweryPostMapQueryResult = z.object({
|
||||
location: z.object({
|
||||
coordinates: z.array(z.number()),
|
||||
city: z.string(),
|
||||
country: z.string().nullable(),
|
||||
stateOrProvince: z.string().nullable(),
|
||||
}),
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
});
|
||||
|
||||
export default BreweryPostMapQueryResult;
|
||||
@@ -0,0 +1,21 @@
|
||||
import ImageQueryValidationSchema from '@/services/schema/ImageSchema/ImageQueryValidationSchema';
|
||||
import { z } from 'zod';
|
||||
|
||||
const BreweryPostQueryResult = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
description: z.string(),
|
||||
location: z.object({
|
||||
city: z.string(),
|
||||
address: z.string(),
|
||||
coordinates: z.tuple([z.number(), z.number()]),
|
||||
country: z.string().nullable(),
|
||||
stateOrProvince: z.string().nullable(),
|
||||
}),
|
||||
postedBy: z.object({ id: z.string(), username: z.string() }),
|
||||
breweryImages: z.array(ImageQueryValidationSchema),
|
||||
createdAt: z.coerce.date(),
|
||||
dateEstablished: z.coerce.date(),
|
||||
});
|
||||
|
||||
export default BreweryPostQueryResult;
|
||||
@@ -0,0 +1,55 @@
|
||||
import { isPast } from 'date-fns';
|
||||
import { z } from 'zod';
|
||||
|
||||
const CreateBreweryPostSchema = z.object({
|
||||
name: z
|
||||
.string({
|
||||
required_error: 'Brewery name is required.',
|
||||
invalid_type_error: 'Brewery name must be a string.',
|
||||
})
|
||||
.min(1, { message: 'Brewery name is required.' })
|
||||
.max(100, { message: 'Brewery name is too long.' }),
|
||||
description: z
|
||||
.string({
|
||||
required_error: 'Description is required.',
|
||||
invalid_type_error: 'Description must be a string.',
|
||||
})
|
||||
.min(1, { message: 'Description is required.' })
|
||||
.max(1500, { message: 'Description is too long.' }),
|
||||
address: z
|
||||
.string({
|
||||
required_error: 'Address is required.',
|
||||
invalid_type_error: 'Address must be a string.',
|
||||
})
|
||||
.min(1, { message: 'Address is required.' })
|
||||
.max(300, { message: 'Address is too long.' }),
|
||||
|
||||
city: z
|
||||
.string({
|
||||
required_error: 'City is required.',
|
||||
invalid_type_error: 'City must be a string.',
|
||||
})
|
||||
.min(1, { message: 'City is required.' })
|
||||
.max(100, { message: 'City is too long.' }),
|
||||
|
||||
region: z
|
||||
.string({ invalid_type_error: 'region must be a string.' })
|
||||
.min(1, { message: 'region is required.' })
|
||||
.max(100, { message: 'region is too long.' })
|
||||
.optional(),
|
||||
|
||||
country: z
|
||||
.string({ invalid_type_error: 'Country must be a string.' })
|
||||
.max(100, { message: 'Country is too long.' })
|
||||
.optional(),
|
||||
|
||||
dateEstablished: z.coerce
|
||||
.date({
|
||||
required_error: 'Date established is required.',
|
||||
invalid_type_error: 'Date established must be a date string.',
|
||||
})
|
||||
.refine((val) => !Number.isNaN(val.toString()), { message: 'Date is invalid.' })
|
||||
.refine((val) => isPast(new Date(val)), { message: 'Date must be in the past.' }),
|
||||
});
|
||||
|
||||
export default CreateBreweryPostSchema;
|
||||
@@ -0,0 +1,14 @@
|
||||
import { z } from 'zod';
|
||||
import CreateBreweryPostSchema from './CreateBreweryPostSchema';
|
||||
|
||||
const CreateNewBreweryPostWithoutLocationSchema = CreateBreweryPostSchema.omit({
|
||||
address: true,
|
||||
city: true,
|
||||
country: true,
|
||||
stateOrProvince: true,
|
||||
}).extend({
|
||||
userId: z.string().cuid(),
|
||||
locationId: z.string().cuid(),
|
||||
});
|
||||
|
||||
export default CreateNewBreweryPostWithoutLocationSchema;
|
||||
@@ -0,0 +1,13 @@
|
||||
import { z } from 'zod';
|
||||
import CreateBreweryPostSchema from './CreateBreweryPostSchema';
|
||||
|
||||
const EditBreweryPostValidationSchema = CreateBreweryPostSchema.extend({
|
||||
id: z.string().cuid(),
|
||||
}).omit({
|
||||
address: true,
|
||||
city: true,
|
||||
region: true,
|
||||
country: true,
|
||||
});
|
||||
|
||||
export default EditBreweryPostValidationSchema;
|
||||
@@ -0,0 +1,14 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const GetMapBreweryPostsSchema = z.object({
|
||||
name: z.string(),
|
||||
id: z.string().cuid(),
|
||||
location: z.object({
|
||||
city: z.string(),
|
||||
country: z.string().nullable(),
|
||||
stateOrProvince: z.string().nullable(),
|
||||
coordinates: z.array(z.number(), z.number()),
|
||||
}),
|
||||
});
|
||||
|
||||
export default GetMapBreweryPostsSchema;
|
||||
@@ -0,0 +1,57 @@
|
||||
import { z } from 'zod';
|
||||
import BreweryPostQueryResult from '../schema/BreweryPostQueryResult';
|
||||
import CreateNewBreweryPostWithoutLocationSchema from '../schema/CreateNewBreweryPostWithoutLocationSchema';
|
||||
import BreweryPostMapQueryResult from '../schema/BreweryPostMapQueryResult';
|
||||
|
||||
export type CreateNewBreweryPost = (
|
||||
args: z.infer<typeof CreateNewBreweryPostWithoutLocationSchema>,
|
||||
) => Promise<z.infer<typeof BreweryPostQueryResult>>;
|
||||
|
||||
export type GetAllBreweryPosts = (args: {
|
||||
pageNum: number;
|
||||
pageSize: number;
|
||||
}) => Promise<{
|
||||
breweryPosts: z.infer<typeof BreweryPostQueryResult>[];
|
||||
count: number;
|
||||
}>;
|
||||
|
||||
export type GetBreweryPostById = (args: {
|
||||
breweryPostId: string;
|
||||
}) => Promise<z.infer<typeof BreweryPostQueryResult> | null>;
|
||||
|
||||
export type GetAllBreweryPostsByPostedById = (args: {
|
||||
pageNum: number;
|
||||
pageSize: number;
|
||||
postedById: string;
|
||||
}) => Promise<{
|
||||
breweryPosts: z.infer<typeof BreweryPostQueryResult>[];
|
||||
count: number;
|
||||
}>;
|
||||
|
||||
export type CreateBreweryPostLocation = (args: {
|
||||
body: {
|
||||
address: string;
|
||||
city: string;
|
||||
country?: string;
|
||||
stateOrProvince?: string;
|
||||
coordinates: [number, number];
|
||||
};
|
||||
postedById: string;
|
||||
}) => Promise<{ id: string }>;
|
||||
|
||||
export type GetMapBreweryPosts = (args: {
|
||||
pageNum: number;
|
||||
pageSize: number;
|
||||
}) => Promise<{
|
||||
breweryPosts: z.infer<typeof BreweryPostMapQueryResult>[];
|
||||
count: number;
|
||||
}>;
|
||||
|
||||
export type UpdateBreweryPost = (args: {
|
||||
breweryPostId: string;
|
||||
body: {
|
||||
name: string;
|
||||
description: string;
|
||||
dateEstablished: Date;
|
||||
};
|
||||
}) => Promise<z.infer<typeof BreweryPostQueryResult>>;
|
||||
Reference in New Issue
Block a user