Refactor: update beer style, brewery post services

This commit is contained in:
Aaron William Po
2023-12-14 22:18:59 -05:00
parent 0de4697e77
commit 70a168df92
26 changed files with 671 additions and 633 deletions

View File

@@ -1,6 +1,5 @@
import ServerError from '@/config/util/ServerError'; import ServerError from '@/config/util/ServerError';
import getBeerStyleById from '@/services/posts/beer-style-post/getBeerStyleById';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema'; import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse, NextApiRequest } from 'next'; import { NextApiResponse, NextApiRequest } from 'next';
import { z } from 'zod'; import { z } from 'zod';
@@ -11,6 +10,7 @@ import {
getBeerStyleLikeCountService, getBeerStyleLikeCountService,
removeBeerStyleLikeService, removeBeerStyleLikeService,
} from '@/services/likes/beer-style-like'; } from '@/services/likes/beer-style-like';
import { getBeerStyleByIdService } from '@/services/posts/beer-style-post';
import { LikeRequest } from '../types'; import { LikeRequest } from '../types';
export const sendBeerStyleLikeRequest = async ( export const sendBeerStyleLikeRequest = async (
@@ -20,7 +20,7 @@ export const sendBeerStyleLikeRequest = async (
const user = req.user!; const user = req.user!;
const { id } = req.query; const { id } = req.query;
const beerStyle = await getBeerStyleById(id); const beerStyle = await getBeerStyleByIdService({ beerStyleId: id });
if (!beerStyle) { if (!beerStyle) {
throw new ServerError('Could not find a beer style with that id.', 404); throw new ServerError('Could not find a beer style with that id.', 404);
} }

View File

@@ -1,5 +1,17 @@
import ServerError from '@/config/util/ServerError'; import ServerError from '@/config/util/ServerError';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse } from 'next';
import { NextHandler } from 'next-connect';
import { z } from 'zod';
import { GetAllPostsByConnectedPostId } from '@/controllers/posts/types';
import {
BeerPostRequest,
CreateBeerPostRequest,
EditBeerPostRequest,
GetAllBeerPostsRequest,
GetBeerRecommendationsRequest,
} from '@/controllers/posts/beer-posts/types';
import { import {
getBeerPostById, getBeerPostById,
editBeerPostByIdService, editBeerPostByIdService,
@@ -9,18 +21,6 @@ import {
createNewBeerPost, createNewBeerPost,
getBeerPostsByPostedByIdService, getBeerPostsByPostedByIdService,
} from '@/services/posts/beer-post'; } from '@/services/posts/beer-post';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse } from 'next';
import { NextHandler } from 'next-connect';
import { z } from 'zod';
import {
BeerPostRequest,
CreateBeerPostRequest,
EditBeerPostRequest,
GetAllBeerPostsRequest,
GetBeerRecommendationsRequest,
} from './types';
import { GetAllPostsByConnectedPostId } from '../types';
export const checkIfBeerPostOwner = async <BeerPostRequestType extends BeerPostRequest>( export const checkIfBeerPostOwner = async <BeerPostRequestType extends BeerPostRequest>(
req: BeerPostRequestType, req: BeerPostRequestType,

View File

@@ -1,16 +1,16 @@
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { z } from 'zod'; import { z } from 'zod';
import DBClient from '@/prisma/DBClient';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema'; import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import getBeerStyleById from '@/services/posts/beer-style-post/getBeerStyleById';
import getAllBeerStyles from '@/services/posts/beer-style-post/getAllBeerStyles';
import ServerError from '@/config/util/ServerError';
import { getBeerPostsByBeerStyleIdService } from '@/services/posts/beer-post'; import { getBeerPostsByBeerStyleIdService } from '@/services/posts/beer-post';
import {
createBeerStyleService,
getAllBeerStylesService,
getBeerStyleByIdService,
} from '@/services/posts/beer-style-post';
import { CreateBeerStyleRequest, GetBeerStyleByIdRequest } from './types'; import { CreateBeerStyleRequest, GetBeerStyleByIdRequest } from './types';
import { GetAllPostsByConnectedPostId, GetAllPostsRequest } from '../types'; import { GetAllPostsByConnectedPostId, GetAllPostsRequest } from '../types';
@@ -20,7 +20,9 @@ export const getBeerStyle = async (
) => { ) => {
const { id } = req.query; const { id } = req.query;
const beerStyle = await getBeerStyleById(id); const beerStyle = await getBeerStyleByIdService({
beerStyleId: id,
});
res.status(200).json({ res.status(200).json({
message: 'Beer style retrieved successfully.', message: 'Beer style retrieved successfully.',
@@ -37,20 +39,18 @@ export const getAllBeersByBeerStyle = async (
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
const { page_size, page_num, id } = req.query; const { page_size, page_num, id } = req.query;
const beers = await getBeerPostsByBeerStyleIdService({ const { beerPosts, count } = await getBeerPostsByBeerStyleIdService({
pageNum: parseInt(page_num, 10), pageNum: parseInt(page_num, 10),
pageSize: parseInt(page_size, 10), pageSize: parseInt(page_size, 10),
styleId: id, styleId: id,
}); });
const count = await DBClient.instance.beerPost.count({ where: { styleId: id } });
res.setHeader('X-Total-Count', count); res.setHeader('X-Total-Count', count);
res.status(200).json({ res.status(200).json({
message: `Beers with style id ${id} retrieved successfully.`, message: `Beers with style id ${id} retrieved successfully.`,
statusCode: 200, statusCode: 200,
payload: beers, payload: beerPosts,
success: true, success: true,
}); });
}; };
@@ -62,8 +62,10 @@ export const getBeerStyles = async (
const pageNum = parseInt(req.query.page_num, 10); const pageNum = parseInt(req.query.page_num, 10);
const pageSize = parseInt(req.query.page_size, 10); const pageSize = parseInt(req.query.page_size, 10);
const beerStyles = await getAllBeerStyles({ pageNum, pageSize }); const { beerStyles, beerStyleCount } = await getAllBeerStylesService({
const beerStyleCount = await DBClient.instance.beerStyle.count(); pageNum,
pageSize,
});
res.setHeader('X-Total-Count', beerStyleCount); res.setHeader('X-Total-Count', beerStyleCount);
@@ -83,23 +85,14 @@ export const createBeerStyle = async (
const user = req.user!; const user = req.user!;
const glassware = await DBClient.instance.glassware.findUnique({ const beerStyle = await createBeerStyleService({
where: { id: glasswareId }, glasswareId,
select: { id: true }, postedById: user.id,
}); body: {
if (!glassware) {
throw new ServerError('Glassware not found.', 404);
}
const beerStyle = await DBClient.instance.beerStyle.create({
data: {
abvRange, abvRange,
description, description,
glassware: { connect: { id: glasswareId } },
ibuRange, ibuRange,
name, name,
postedBy: { connect: { id: user.id } },
}, },
}); });

View File

@@ -1,15 +1,27 @@
import DBClient from '@/prisma/DBClient';
import getAllBreweryPostsByPostedById from '@/services/posts/brewery-post/getAllBreweryPostsByPostedById';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema'; import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { z } from 'zod'; import { z } from 'zod';
import getAllBreweryPosts from '@/services/posts/brewery-post/getAllBreweryPosts';
import createNewBreweryPost from '@/services/posts/brewery-post/createNewBreweryPost';
import geocode from '@/config/mapbox/geocoder'; import geocode from '@/config/mapbox/geocoder';
import ServerError from '@/config/util/ServerError'; import ServerError from '@/config/util/ServerError';
import BreweryPostMapQueryResult from '@/services/posts/brewery-post/schema/BreweryPostMapQueryResult';
import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult'; import {
import { CreateBreweryPostRequest, GetBreweryPostsRequest } from './types'; getAllBreweryPostsByPostedByIdService,
getAllBreweryPostsService,
createNewBreweryPostService,
createBreweryPostLocationService,
getMapBreweryPostsService,
getBreweryPostByIdService,
} from '@/services/posts/brewery-post';
import { getBeerPostsByBreweryIdService } from '@/services/posts/beer-post';
import { NextHandler } from 'next-connect';
import DBClient from '@/prisma/DBClient';
import {
BreweryPostRequest,
CreateBreweryPostRequest,
EditBreweryPostRequest,
GetBreweryPostsRequest,
} from './types';
import { GetAllPostsByConnectedPostId } from '../types'; import { GetAllPostsByConnectedPostId } from '../types';
export const getBreweryPostsByUserId = async ( export const getBreweryPostsByUserId = async (
@@ -21,17 +33,13 @@ export const getBreweryPostsByUserId = async (
const { id } = req.query; const { id } = req.query;
const breweryPosts = await getAllBreweryPostsByPostedById({ const { breweryPosts, count } = await getAllBreweryPostsByPostedByIdService({
pageNum, pageNum,
pageSize, pageSize,
postedById: id, postedById: id,
}); });
const breweryPostCount = await DBClient.instance.breweryPost.count({ res.setHeader('X-Total-Count', count);
where: { postedBy: { id } },
});
res.setHeader('X-Total-Count', breweryPostCount);
res.status(200).json({ res.status(200).json({
message: `Brewery posts by user ${id} fetched successfully`, message: `Brewery posts by user ${id} fetched successfully`,
@@ -48,10 +56,9 @@ export const getBreweryPosts = async (
const pageNum = parseInt(req.query.page_num, 10); const pageNum = parseInt(req.query.page_num, 10);
const pageSize = parseInt(req.query.page_size, 10); const pageSize = parseInt(req.query.page_size, 10);
const breweryPosts = await getAllBreweryPosts({ pageNum, pageSize }); const { breweryPosts, count } = await getAllBreweryPostsService({ pageNum, pageSize });
const breweryPostCount = await DBClient.instance.breweryPost.count();
res.setHeader('X-Total-Count', breweryPostCount); res.setHeader('X-Total-Count', count);
res.status(200).json({ res.status(200).json({
message: 'Brewery posts retrieved successfully', message: 'Brewery posts retrieved successfully',
statusCode: 200, statusCode: 200,
@@ -77,19 +84,18 @@ export const createBreweryPost = async (
const [latitude, longitude] = geocoded.center; const [latitude, longitude] = geocoded.center;
const location = await DBClient.instance.breweryLocation.create({ const location = await createBreweryPostLocationService({
data: { body: {
address, address,
city, city,
country, country,
stateOrProvince: region, stateOrProvince: region,
coordinates: [latitude, longitude], coordinates: [latitude, longitude],
postedBy: { connect: { id: userId } },
}, },
select: { id: true }, postedById: userId,
}); });
const newBreweryPost = await createNewBreweryPost({ const newBreweryPost = await createNewBreweryPostService({
name, name,
description, description,
locationId: location.id, locationId: location.id,
@@ -112,24 +118,12 @@ export const getMapBreweryPosts = async (
const pageNum = parseInt(req.query.page_num, 10); const pageNum = parseInt(req.query.page_num, 10);
const pageSize = parseInt(req.query.page_size, 10); const pageSize = parseInt(req.query.page_size, 10);
const skip = (pageNum - 1) * pageSize; const { breweryPosts, count } = await getMapBreweryPostsService({
const take = pageSize; pageNum,
pageSize,
const breweryPosts: z.infer<typeof BreweryPostMapQueryResult>[] =
await DBClient.instance.breweryPost.findMany({
select: {
location: {
select: { coordinates: true, city: true, country: true, stateOrProvince: true },
},
id: true,
name: true,
},
skip,
take,
}); });
const breweryPostCount = await DBClient.instance.breweryPost.count();
res.setHeader('X-Total-Count', breweryPostCount); res.setHeader('X-Total-Count', count);
res.status(200).json({ res.status(200).json({
message: 'Brewery posts retrieved successfully', message: 'Brewery posts retrieved successfully',
@@ -149,37 +143,10 @@ export const getAllBeersByBrewery = async (
const pageNum = parseInt(page_num, 10); const pageNum = parseInt(page_num, 10);
const pageSize = parseInt(page_size, 10); const pageSize = parseInt(page_size, 10);
const beers: z.infer<typeof BeerPostQueryResult>[] = const { beerPosts, count } = await getBeerPostsByBreweryIdService({
await DBClient.instance.beerPost.findMany({ pageNum,
where: { breweryId: id }, pageSize,
skip: (pageNum - 1) * pageSize, breweryId: id,
take: pageSize,
select: {
id: true,
name: true,
ibu: true,
abv: true,
createdAt: true,
updatedAt: true,
description: true,
postedBy: { select: { username: true, id: true } },
brewery: { select: { name: true, id: true } },
style: { select: { name: true, id: true, description: true } },
beerImages: {
select: {
alt: true,
path: true,
caption: true,
id: true,
createdAt: true,
updatedAt: true,
},
},
},
});
const count = await DBClient.instance.beerPost.count({
where: { breweryId: id },
}); });
res.setHeader('X-Total-Count', count); res.setHeader('X-Total-Count', count);
@@ -187,7 +154,69 @@ export const getAllBeersByBrewery = async (
res.status(200).json({ res.status(200).json({
message: 'Beers fetched successfully', message: 'Beers fetched successfully',
statusCode: 200, statusCode: 200,
payload: beers, payload: beerPosts,
success: true, success: true,
}); });
}; };
export const checkIfBreweryPostOwner = async (
req: BreweryPostRequest,
res: NextApiResponse,
next: NextHandler,
) => {
const user = req.user!;
const { id } = req.query;
const breweryPost = await getBreweryPostByIdService({ breweryPostId: id });
if (!breweryPost) {
throw new ServerError('Brewery post not found', 404);
}
if (breweryPost.postedBy.id !== user.id) {
throw new ServerError('You are not the owner of this brewery post', 403);
}
return next();
};
export const editBreweryPost = async (
req: EditBreweryPostRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const {
body,
query: { id },
} = req;
await DBClient.instance.breweryPost.update({
where: { id },
data: body,
});
res.status(200).json({
message: 'Brewery post updated successfully',
success: true,
statusCode: 200,
});
};
export const deleteBreweryPost = async (
req: BreweryPostRequest,
res: NextApiResponse,
) => {
const {
query: { id },
} = req;
const deleted = await DBClient.instance.breweryPost.delete({ where: { id } });
if (!deleted) {
throw new ServerError('Brewery post not found', 404);
}
res.status(200).json({
message: 'Brewery post deleted successfully',
success: true,
statusCode: 200,
});
};

View File

@@ -1,5 +1,6 @@
import { UserExtendedNextApiRequest } from '@/config/auth/types'; import { UserExtendedNextApiRequest } from '@/config/auth/types';
import CreateBreweryPostSchema from '@/services/posts/brewery-post/schema/CreateBreweryPostSchema'; import CreateBreweryPostSchema from '@/services/posts/brewery-post/schema/CreateBreweryPostSchema';
import EditBreweryPostValidationSchema from '@/services/posts/brewery-post/schema/EditBreweryPostValidationSchema';
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema'; import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
import { NextApiRequest } from 'next'; import { NextApiRequest } from 'next';
import { z } from 'zod'; import { z } from 'zod';
@@ -11,3 +12,11 @@ export interface GetBreweryPostsRequest extends NextApiRequest {
export interface CreateBreweryPostRequest extends UserExtendedNextApiRequest { export interface CreateBreweryPostRequest extends UserExtendedNextApiRequest {
body: z.infer<typeof CreateBreweryPostSchema>; body: z.infer<typeof CreateBreweryPostSchema>;
} }
export interface BreweryPostRequest extends UserExtendedNextApiRequest {
query: { id: string };
}
export interface EditBreweryPostRequest extends BreweryPostRequest {
body: z.infer<typeof EditBreweryPostValidationSchema>;
}

View File

@@ -1,81 +1,17 @@
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser'; import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
import { UserExtendedNextApiRequest } from '@/config/auth/types';
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions'; import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema'; import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { createRouter, NextHandler } from 'next-connect'; import { createRouter } from 'next-connect';
import { z } from 'zod'; import { z } from 'zod';
import ServerError from '@/config/util/ServerError';
import DBClient from '@/prisma/DBClient';
import getBreweryPostById from '@/services/posts/brewery-post/getBreweryPostById';
import EditBreweryPostValidationSchema from '@/services/posts/brewery-post/schema/EditBreweryPostValidationSchema';
interface BreweryPostRequest extends UserExtendedNextApiRequest { import {
query: { id: string }; checkIfBreweryPostOwner,
} editBreweryPost,
deleteBreweryPost,
} from '@/controllers/posts/breweries';
import { EditBreweryPostRequest } from '@/controllers/posts/breweries/types';
interface EditBreweryPostRequest extends BreweryPostRequest {
body: z.infer<typeof EditBreweryPostValidationSchema>;
}
const checkIfBreweryPostOwner = async (
req: BreweryPostRequest,
res: NextApiResponse,
next: NextHandler,
) => {
const user = req.user!;
const { id } = req.query;
const breweryPost = await getBreweryPostById(id);
if (!breweryPost) {
throw new ServerError('Brewery post not found', 404);
}
if (breweryPost.postedBy.id !== user.id) {
throw new ServerError('You are not the owner of this brewery post', 403);
}
return next();
};
const editBreweryPost = async (
req: EditBreweryPostRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const {
body,
query: { id },
} = req;
await DBClient.instance.breweryPost.update({
where: { id },
data: body,
});
res.status(200).json({
message: 'Brewery post updated successfully',
success: true,
statusCode: 200,
});
};
const deleteBreweryPost = async (req: BreweryPostRequest, res: NextApiResponse) => {
const {
query: { id },
} = req;
const deleted = await DBClient.instance.breweryPost.delete({ where: { id } });
if (!deleted) {
throw new ServerError('Brewery post not found', 404);
}
res.status(200).json({
message: 'Brewery post deleted successfully',
success: true,
statusCode: 200,
});
};
const router = createRouter< const router = createRouter<
EditBreweryPostRequest, EditBreweryPostRequest,
NextApiResponse<z.infer<typeof APIResponseValidationSchema>> NextApiResponse<z.infer<typeof APIResponseValidationSchema>>

View File

@@ -6,56 +6,9 @@ import { z } from 'zod';
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions'; import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser'; import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
import CreateBreweryPostSchema from '@/services/posts/brewery-post/schema/CreateBreweryPostSchema'; import CreateBreweryPostSchema from '@/services/posts/brewery-post/schema/CreateBreweryPostSchema';
import createNewBreweryPost from '@/services/posts/brewery-post/createNewBreweryPost';
import geocode from '@/config/mapbox/geocoder';
import ServerError from '@/config/util/ServerError';
import DBClient from '@/prisma/DBClient';
import { CreateBreweryPostRequest } from '@/controllers/posts/breweries/types'; import { CreateBreweryPostRequest } from '@/controllers/posts/breweries/types';
import { createBreweryPost } from '@/controllers/posts/breweries';
const createBreweryPost = async (
req: CreateBreweryPostRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const { name, description, dateEstablished, address, city, country, region } = req.body;
const userId = req.user!.id;
const fullAddress = `${address}, ${city}, ${region}, ${country}`;
const geocoded = await geocode(fullAddress);
if (!geocoded) {
throw new ServerError('Address is not valid', 400);
}
const [latitude, longitude] = geocoded.center;
const location = await DBClient.instance.breweryLocation.create({
data: {
address,
city,
country,
stateOrProvince: region,
coordinates: [latitude, longitude],
postedBy: { connect: { id: userId } },
},
select: { id: true },
});
const newBreweryPost = await createNewBreweryPost({
name,
description,
locationId: location.id,
dateEstablished,
userId,
});
res.status(201).json({
message: 'Brewery post created successfully',
statusCode: 201,
payload: newBreweryPost,
success: true,
});
};
const router = createRouter< const router = createRouter<
CreateBreweryPostRequest, CreateBreweryPostRequest,

View File

@@ -1,51 +1,15 @@
import validateRequest from '@/config/nextConnect/middleware/validateRequest'; import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import DBClient from '@/prisma/DBClient'; import { getMapBreweryPosts } from '@/controllers/posts/breweries';
import BreweryPostMapQueryResult from '@/services/posts/brewery-post/schema/BreweryPostMapQueryResult'; import { GetBreweryPostsRequest } from '@/controllers/posts/breweries/types';
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema'; import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema'; import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { NextApiRequest, NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { createRouter } from 'next-connect'; import { createRouter } from 'next-connect';
import { z } from 'zod'; import { z } from 'zod';
interface GetBreweryPostsRequest extends NextApiRequest {
query: z.infer<typeof PaginatedQueryResponseSchema>;
}
const getMapBreweryPosts = async (
req: GetBreweryPostsRequest,
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
) => {
const pageNum = parseInt(req.query.page_num, 10);
const pageSize = parseInt(req.query.page_size, 10);
const skip = (pageNum - 1) * pageSize;
const take = pageSize;
const breweryPosts: z.infer<typeof BreweryPostMapQueryResult>[] =
await DBClient.instance.breweryPost.findMany({
select: {
location: {
select: { coordinates: true, city: true, country: true, stateOrProvince: true },
},
id: true,
name: true,
},
skip,
take,
});
const breweryPostCount = await DBClient.instance.breweryPost.count();
res.setHeader('X-Total-Count', breweryPostCount);
res.status(200).json({
message: 'Brewery posts retrieved successfully',
statusCode: 200,
payload: breweryPosts,
success: true,
});
};
const router = createRouter< const router = createRouter<
GetBreweryPostsRequest, GetBreweryPostsRequest,
NextApiResponse<z.infer<typeof APIResponseValidationSchema>> NextApiResponse<z.infer<typeof APIResponseValidationSchema>>

View File

@@ -5,11 +5,12 @@ import { z } from 'zod';
import useMediaQuery from '@/hooks/utilities/useMediaQuery'; import useMediaQuery from '@/hooks/utilities/useMediaQuery';
import { Tab } from '@headlessui/react'; import { Tab } from '@headlessui/react';
import getBeerStyleById from '@/services/posts/beer-style-post/getBeerStyleById';
import BeerStyleHeader from '@/components/BeerStyleById/BeerStyleHeader'; import BeerStyleHeader from '@/components/BeerStyleById/BeerStyleHeader';
import BeerStyleQueryResult from '@/services/posts/beer-style-post/schema/BeerStyleQueryResult'; import BeerStyleQueryResult from '@/services/posts/beer-style-post/schema/BeerStyleQueryResult';
import BeerStyleCommentSection from '@/components/BeerStyleById/BeerStyleCommentSection'; import BeerStyleCommentSection from '@/components/BeerStyleById/BeerStyleCommentSection';
import BeerStyleBeerSection from '@/components/BeerStyleById/BeerStyleBeerSection'; import BeerStyleBeerSection from '@/components/BeerStyleById/BeerStyleBeerSection';
import { getBeerStyleByIdService } from '@/services/posts/beer-style-post';
interface BeerStylePageProps { interface BeerStylePageProps {
beerStyle: z.infer<typeof BeerStyleQueryResult>; beerStyle: z.infer<typeof BeerStyleQueryResult>;
@@ -69,7 +70,7 @@ export default BeerStyleByIdPage;
export const getServerSideProps: GetServerSideProps = async ({ params }) => { export const getServerSideProps: GetServerSideProps = async ({ params }) => {
const id = params!.id as string; const id = params!.id as string;
const beerStyle = await getBeerStyleById(id); const beerStyle = await getBeerStyleByIdService({ beerStyleId: id });
if (!beerStyle) { if (!beerStyle) {
return { notFound: true }; return { notFound: true };
} }

View File

@@ -8,7 +8,7 @@ import { BeerStyle } from '@prisma/client';
import { NextPage } from 'next'; import { NextPage } from 'next';
import { BiBeer } from 'react-icons/bi'; import { BiBeer } from 'react-icons/bi';
import { z } from 'zod'; import { z } from 'zod';
import getBreweryPostById from '@/services/posts/brewery-post/getBreweryPostById'; import { getBreweryPostByIdService } from '@/services/posts/brewery-post';
interface CreateBeerPageProps { interface CreateBeerPageProps {
brewery: z.infer<typeof BreweryPostQueryResult>; brewery: z.infer<typeof BreweryPostQueryResult>;
@@ -32,7 +32,7 @@ export const getServerSideProps = withPageAuthRequired<CreateBeerPageProps>(
async (context) => { async (context) => {
const id = context.params?.id as string; const id = context.params?.id as string;
const breweryPost = await getBreweryPostById(id); const breweryPost = await getBreweryPostByIdService({ breweryPostId: id });
const beerStyles = await DBClient.instance.beerStyle.findMany(); const beerStyles = await DBClient.instance.beerStyle.findMany();
return { return {

View File

@@ -5,7 +5,8 @@ import FormPageLayout from '@/components/ui/forms/FormPageLayout';
import FormSegment from '@/components/ui/forms/FormSegment'; import FormSegment from '@/components/ui/forms/FormSegment';
import FormTextArea from '@/components/ui/forms/FormTextArea'; import FormTextArea from '@/components/ui/forms/FormTextArea';
import FormTextInput from '@/components/ui/forms/FormTextInput'; import FormTextInput from '@/components/ui/forms/FormTextInput';
import getBreweryPostById from '@/services/posts/brewery-post/getBreweryPostById'; import { getBreweryPostByIdService } from '@/services/posts/brewery-post';
import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult'; import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult';
import EditBreweryPostValidationSchema from '@/services/posts/brewery-post/schema/EditBreweryPostValidationSchema'; import EditBreweryPostValidationSchema from '@/services/posts/brewery-post/schema/EditBreweryPostValidationSchema';
import withPageAuthRequired from '@/util/withPageAuthRequired'; import withPageAuthRequired from '@/util/withPageAuthRequired';
@@ -143,7 +144,7 @@ export default EditBreweryPostPage;
export const getServerSideProps = withPageAuthRequired<EditPageProps>( export const getServerSideProps = withPageAuthRequired<EditPageProps>(
async (context, session) => { async (context, session) => {
const breweryPostId = context.params?.id as string; const breweryPostId = context.params?.id as string;
const breweryPost = await getBreweryPostById(breweryPostId); const breweryPost = await getBreweryPostByIdService({ breweryPostId });
const { id: userId } = session; const { id: userId } = session;

View File

@@ -1,4 +1,3 @@
import getBreweryPostById from '@/services/posts/brewery-post/getBreweryPostById';
import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult'; import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult';
import { GetServerSideProps, NextPage } from 'next'; import { GetServerSideProps, NextPage } from 'next';
@@ -13,6 +12,7 @@ import { Tab } from '@headlessui/react';
import dynamic from 'next/dynamic'; import dynamic from 'next/dynamic';
import { MAPBOX_ACCESS_TOKEN } from '@/config/env'; import { MAPBOX_ACCESS_TOKEN } from '@/config/env';
import { CldImage } from 'next-cloudinary'; import { CldImage } from 'next-cloudinary';
import { getBreweryPostByIdService } from '@/services/posts/brewery-post';
const [BreweryInfoHeader, BreweryBeersSection, BreweryCommentsSection, BreweryPostMap] = [ const [BreweryInfoHeader, BreweryBeersSection, BreweryCommentsSection, BreweryPostMap] = [
dynamic(() => import('@/components/BreweryById/BreweryInfoHeader')), dynamic(() => import('@/components/BreweryById/BreweryInfoHeader')),
@@ -114,7 +114,9 @@ const BreweryByIdPage: NextPage<BreweryPageProps> = ({ breweryPost, mapboxToken
export const getServerSideProps: GetServerSideProps<BreweryPageProps> = async ( export const getServerSideProps: GetServerSideProps<BreweryPageProps> = async (
context, context,
) => { ) => {
const breweryPost = await getBreweryPostById(context.params!.id! as string); const breweryPost = await getBreweryPostByIdService({
breweryPostId: context.params?.id as string,
});
const mapboxToken = MAPBOX_ACCESS_TOKEN; const mapboxToken = MAPBOX_ACCESS_TOKEN;
return !breweryPost return !breweryPost

View File

@@ -1,34 +0,0 @@
import { z } from 'zod';
import DBClient from '@/prisma/DBClient';
import BeerStyleQueryResult from './schema/BeerStyleQueryResult';
interface DeleteBeerStyleByIdArgs {
beerStyleId: string;
}
const deleteBeerStyleById = async ({
beerStyleId,
}: DeleteBeerStyleByIdArgs): Promise<z.infer<typeof BeerStyleQueryResult> | null> => {
const deleted = await DBClient.instance.beerStyle.delete({
where: { id: beerStyleId },
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 } },
},
});
/**
* Prisma does not support tuples, so we have to typecast the ibuRange and abvRange
* fields to [number, number] in order to satisfy the zod schema.
*/
return deleted as Awaited<ReturnType<typeof deleteBeerStyleById>>;
};
export default deleteBeerStyleById;

View File

@@ -1,30 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerStyleQueryResult from './schema/BeerStyleQueryResult';
const editBeerStyleById = async (
id: string,
): Promise<z.infer<typeof BeerStyleQueryResult> | null> => {
const beerStyle = await DBClient.instance.beerStyle.findUnique({
where: { id },
select: {
id: true,
name: true,
postedBy: { select: { id: true, username: true } },
createdAt: true,
updatedAt: true,
abvRange: true,
ibuRange: true,
description: true,
glassware: { select: { id: true, name: true } },
},
});
/**
* Prisma does not support tuples, so we have to typecast the ibuRange and abvRange
* fields to [number, number] in order to satisfy the zod schema.
*/
return beerStyle as Awaited<ReturnType<typeof editBeerStyleById>>;
};
export default editBeerStyleById;

View File

@@ -1,37 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerStyleQueryResult from './schema/BeerStyleQueryResult';
interface GetAllBeerStylesArgs {
pageNum: number;
pageSize: number;
}
const getAllBeerStyles = async ({
pageNum,
pageSize,
}: GetAllBeerStylesArgs): Promise<z.infer<typeof BeerStyleQueryResult>[]> => {
const beerStyles = await DBClient.instance.beerStyle.findMany({
take: pageSize,
skip: (pageNum - 1) * pageSize,
select: {
id: true,
name: true,
postedBy: { select: { id: true, username: true } },
createdAt: true,
updatedAt: true,
abvRange: true,
ibuRange: true,
description: true,
glassware: { select: { id: true, name: true } },
},
});
/**
* Prisma does not support tuples, so we have to typecast the ibuRange and abvRange
* fields to [number, number] in order to satisfy the zod schema.
*/
return beerStyles as Awaited<ReturnType<typeof getAllBeerStyles>>;
};
export default getAllBeerStyles;

View File

@@ -1,30 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerStyleQueryResult from './schema/BeerStyleQueryResult';
const getBeerStyleById = async (
id: string,
): Promise<z.infer<typeof BeerStyleQueryResult> | null> => {
const beerStyle = await DBClient.instance.beerStyle.findUnique({
where: { id },
select: {
id: true,
name: true,
postedBy: { select: { id: true, username: true } },
createdAt: true,
updatedAt: true,
abvRange: true,
ibuRange: true,
description: true,
glassware: { select: { id: true, name: true } },
},
});
/**
* Prisma does not support tuples, so we have to typecast the ibuRange and abvRange
* fields to [number, number] in order to satisfy the zod schema.
*/
return beerStyle as Awaited<ReturnType<typeof getBeerStyleById>>;
};
export default getBeerStyleById;

View File

@@ -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>>;
};

View File

@@ -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>;

View File

@@ -1,64 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import CreateBreweryPostSchema from './schema/CreateBreweryPostSchema';
import BreweryPostQueryResult from './schema/BreweryPostQueryResult';
const CreateNewBreweryPostWithUserAndLocationSchema = CreateBreweryPostSchema.omit({
address: true,
city: true,
country: true,
stateOrProvince: true,
}).extend({
userId: z.string().cuid(),
locationId: z.string().cuid(),
});
const createNewBreweryPost = async ({
dateEstablished,
description,
locationId,
name,
userId,
}: z.infer<typeof CreateNewBreweryPostWithUserAndLocationSchema>): Promise<
z.infer<typeof BreweryPostQueryResult>
> => {
const post = (await DBClient.instance.breweryPost.create({
data: {
name,
description,
dateEstablished,
location: { connect: { id: locationId } },
postedBy: { connect: { id: userId } },
},
select: {
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 Awaited<ReturnType<typeof createNewBreweryPost>>;
return post;
};
export default createNewBreweryPost;

View File

@@ -1,55 +0,0 @@
import DBClient from '@/prisma/DBClient';
import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult';
import { z } from 'zod';
const prisma = DBClient.instance;
const getAllBreweryPosts = async ({
pageNum,
pageSize,
}: {
pageNum: number;
pageSize: number;
}): Promise<z.infer<typeof BreweryPostQueryResult>[]> => {
const breweryPosts = await prisma.breweryPost.findMany({
take: pageSize,
skip: (pageNum - 1) * pageSize,
select: {
id: true,
location: {
select: {
city: true,
address: true,
coordinates: true,
country: true,
stateOrProvince: true,
},
},
description: true,
name: true,
postedBy: { select: { username: true, id: true } },
breweryImages: {
select: {
path: true,
caption: true,
id: true,
alt: true,
createdAt: true,
updatedAt: true,
},
},
createdAt: true,
dateEstablished: true,
},
orderBy: { createdAt: 'desc' },
});
/**
* Prisma does not support tuples, so we have to typecast the coordinates field to
* [number, number] in order to satisfy the zod schema.
*/
return breweryPosts as Awaited<ReturnType<typeof getAllBreweryPosts>>;
};
export default getAllBreweryPosts;

View File

@@ -1,58 +0,0 @@
import DBClient from '@/prisma/DBClient';
import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult';
import { z } from 'zod';
const prisma = DBClient.instance;
const getAllBreweryPostsByPostedById = async ({
pageNum,
pageSize,
postedById,
}: {
pageNum: number;
pageSize: number;
postedById: string;
}): Promise<z.infer<typeof BreweryPostQueryResult>[]> => {
const breweryPosts = await prisma.breweryPost.findMany({
where: { postedBy: { id: postedById } },
take: pageSize,
skip: (pageNum - 1) * pageSize,
select: {
id: true,
location: {
select: {
city: true,
address: true,
coordinates: true,
country: true,
stateOrProvince: true,
},
},
description: true,
name: true,
postedBy: { select: { username: true, id: true } },
breweryImages: {
select: {
path: true,
caption: true,
id: true,
alt: true,
createdAt: true,
updatedAt: true,
},
},
createdAt: true,
dateEstablished: true,
},
orderBy: { createdAt: 'desc' },
});
/**
* Prisma does not support tuples, so we have to typecast the coordinates field to
* [number, number] in order to satisfy the zod schema.
*/
return breweryPosts as Awaited<ReturnType<typeof getAllBreweryPostsByPostedById>>;
};
export default getAllBreweryPostsByPostedById;

View File

@@ -1,46 +0,0 @@
import DBClient from '@/prisma/DBClient';
import BreweryPostQueryResult from '@/services/posts/brewery-post/schema/BreweryPostQueryResult';
import { z } from 'zod';
const prisma = DBClient.instance;
const getBreweryPostById = async (id: string) => {
const breweryPost = await prisma.breweryPost.findFirst({
select: {
id: true,
location: {
select: {
city: true,
address: true,
coordinates: true,
country: true,
stateOrProvince: true,
},
},
description: true,
name: true,
breweryImages: {
select: {
path: true,
caption: true,
id: true,
alt: true,
createdAt: true,
updatedAt: true,
},
},
postedBy: { select: { username: true, id: true } },
createdAt: true,
dateEstablished: true,
},
where: { id },
});
/**
* Prisma does not support tuples, so we have to typecast the coordinates field to
* [number, number] in order to satisfy the zod schema.
*/
return breweryPost as z.infer<typeof BreweryPostQueryResult> | null;
};
export default getBreweryPostById;

View File

@@ -0,0 +1,207 @@
import DBClient from '@/prisma/DBClient';
import {
CreateBreweryPostLocation,
CreateNewBreweryPost,
GetAllBreweryPosts,
GetAllBreweryPostsByPostedById,
GetBreweryPostById,
GetMapBreweryPosts,
} 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 };
};

View File

@@ -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;

View File

@@ -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;

View File

@@ -0,0 +1,48 @@
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;
}>;