mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
Refactor: update like services
This commit is contained in:
@@ -19,7 +19,7 @@ import {
|
||||
* @param options.userId - The ID of the user.
|
||||
* @returns A promise that resolves to an array of created beer images.
|
||||
*/
|
||||
export const addBeerImagesToDB: AddBeerImagesToDB = ({
|
||||
export const addBeerImagesService: AddBeerImagesToDB = ({
|
||||
body,
|
||||
files,
|
||||
beerPostId,
|
||||
@@ -51,7 +51,7 @@ export const addBeerImagesToDB: AddBeerImagesToDB = ({
|
||||
* @param options - The options for deleting a beer image.
|
||||
* @param options.beerImageId - The ID of the beer image.
|
||||
*/
|
||||
export const deleteBeerImageFromDBAndStorage: DeleteBeerImageFromDBAndStorage = async ({
|
||||
export const deleteBeerImageService: DeleteBeerImageFromDBAndStorage = async ({
|
||||
beerImageId,
|
||||
}) => {
|
||||
const deleted = await DBClient.instance.beerImage.delete({
|
||||
@@ -73,7 +73,7 @@ export const deleteBeerImageFromDBAndStorage: DeleteBeerImageFromDBAndStorage =
|
||||
* @returns A promise that resolves to the updated beer image.
|
||||
*/
|
||||
|
||||
export const updateBeerImageMetadata: UpdateBeerImageMetadata = async ({
|
||||
export const updateBeerImageService: UpdateBeerImageMetadata = async ({
|
||||
beerImageId,
|
||||
body,
|
||||
}) => {
|
||||
|
||||
@@ -20,7 +20,7 @@ import {
|
||||
* @returns A promise that resolves to an array of created brewery images.
|
||||
*/
|
||||
|
||||
export const addBreweryImagesToDB: AddBreweryImagesToDB = ({
|
||||
export const addBreweryImagesService: AddBreweryImagesToDB = ({
|
||||
body,
|
||||
files,
|
||||
breweryPostId,
|
||||
@@ -52,15 +52,16 @@ export const addBreweryImagesToDB: AddBreweryImagesToDB = ({
|
||||
* @param options - The options for deleting a brewery image.
|
||||
* @param options.breweryImageId - The ID of the brewery image.
|
||||
*/
|
||||
export const deleteBreweryImageFromDBAndStorage: DeleteBreweryImageFromDBAndStorage =
|
||||
async ({ breweryImageId }) => {
|
||||
const deleted = await DBClient.instance.breweryImage.delete({
|
||||
where: { id: breweryImageId },
|
||||
select: { path: true, id: true },
|
||||
});
|
||||
const { path } = deleted;
|
||||
await cloudinary.uploader.destroy(path);
|
||||
};
|
||||
export const deleteBreweryImageService: DeleteBreweryImageFromDBAndStorage = async ({
|
||||
breweryImageId,
|
||||
}) => {
|
||||
const deleted = await DBClient.instance.breweryImage.delete({
|
||||
where: { id: breweryImageId },
|
||||
select: { path: true, id: true },
|
||||
});
|
||||
const { path } = deleted;
|
||||
await cloudinary.uploader.destroy(path);
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates the brewery image metadata in the database.
|
||||
@@ -72,7 +73,7 @@ export const deleteBreweryImageFromDBAndStorage: DeleteBreweryImageFromDBAndStor
|
||||
* @param options.body.caption - The caption for the brewery image.
|
||||
* @returns A promise that resolves to the updated brewery image.
|
||||
*/
|
||||
export const updateBreweryImageMetadata: UpdateBreweryImageMetadata = async ({
|
||||
export const updateBreweryImageService: UpdateBreweryImageMetadata = async ({
|
||||
breweryImageId,
|
||||
body,
|
||||
}) => {
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import GetUserSchema from '../../users/auth/schema/GetUserSchema';
|
||||
|
||||
interface CreateBeerPostLikeArgs {
|
||||
id: string;
|
||||
user: z.infer<typeof GetUserSchema>;
|
||||
}
|
||||
|
||||
const createBeerPostLike = async ({ id, user }: CreateBeerPostLikeArgs) =>
|
||||
DBClient.instance.beerPostLike.create({
|
||||
data: { beerPost: { connect: { id } }, likedBy: { connect: { id: user.id } } },
|
||||
});
|
||||
|
||||
export default createBeerPostLike;
|
||||
@@ -1,14 +0,0 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
interface FindBeerPostLikeByIdArgs {
|
||||
beerPostId: string;
|
||||
likedById: string;
|
||||
}
|
||||
|
||||
const findBeerPostLikeById = async ({
|
||||
beerPostId,
|
||||
likedById,
|
||||
}: FindBeerPostLikeByIdArgs) =>
|
||||
DBClient.instance.beerPostLike.findFirst({ where: { beerPostId, likedById } });
|
||||
|
||||
export default findBeerPostLikeById;
|
||||
@@ -1,6 +0,0 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
const getBeerPostLikeCountByBeerPostId = async ({ beerPostId }: { beerPostId: string }) =>
|
||||
DBClient.instance.beerPostLike.count({ where: { beerPostId } });
|
||||
|
||||
export default getBeerPostLikeCountByBeerPostId;
|
||||
60
src/services/likes/beer-post-like/index.ts
Normal file
60
src/services/likes/beer-post-like/index.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import {
|
||||
CreateBeerPostLike,
|
||||
FindBeerPostLikeById,
|
||||
GetBeerPostLikeCount,
|
||||
RemoveBeerPostLike,
|
||||
} from './types';
|
||||
|
||||
/**
|
||||
* Creates a new beer post like.
|
||||
*
|
||||
* @param params - The parameters object for creating the beer post like.
|
||||
* @param params.beerPostId - The ID of the beer post.
|
||||
* @param params.likedById - The ID of the user who will like the beer post.
|
||||
* @returns A promise that resolves to the newly created beer post like.
|
||||
*/
|
||||
export const createBeerPostLikeService: CreateBeerPostLike = async ({
|
||||
beerPostId,
|
||||
likedById,
|
||||
}) =>
|
||||
DBClient.instance.beerPostLike.create({
|
||||
data: {
|
||||
beerPost: { connect: { id: beerPostId } },
|
||||
likedBy: { connect: { id: likedById } },
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Retrieves a beer post like by ID.
|
||||
*
|
||||
* @param params - The parameters object for retrieving the beer post like.
|
||||
* @param params.beerPostId - The ID of the beer post.
|
||||
* @param params.likedById - The ID of the user who liked the beer post.
|
||||
* @returns A promise that resolves to the beer post like.
|
||||
*/
|
||||
export const findBeerPostLikeByIdService: FindBeerPostLikeById = async ({
|
||||
beerPostId,
|
||||
likedById,
|
||||
}) => DBClient.instance.beerPostLike.findFirst({ where: { beerPostId, likedById } });
|
||||
|
||||
/**
|
||||
* Removes a beer post like.
|
||||
*
|
||||
* @param params - The parameters object for removing the beer post like.
|
||||
* @param params.beerPostLikeId - The ID of the beer post like to remove.
|
||||
* @returns A promise that resolves to the removed beer post like.
|
||||
*/
|
||||
export const removeBeerPostLikeService: RemoveBeerPostLike = async ({ beerPostLikeId }) =>
|
||||
DBClient.instance.beerPostLike.delete({ where: { id: beerPostLikeId } });
|
||||
|
||||
/**
|
||||
* Retrieves the number of likes for a beer post.
|
||||
*
|
||||
* @param params - The parameters object for retrieving the number of likes for a beer
|
||||
* post.
|
||||
* @param params.beerPostId - The ID of the beer post.
|
||||
* @returns A promise that resolves to the number of likes for a beer post.
|
||||
*/
|
||||
export const getBeerPostLikeCountService: GetBeerPostLikeCount = async ({ beerPostId }) =>
|
||||
DBClient.instance.beerPostLike.count({ where: { beerPostId } });
|
||||
@@ -1,10 +0,0 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
interface RemoveBeerPostLikeArgs {
|
||||
beerLikeId: string;
|
||||
}
|
||||
|
||||
const removeBeerPostLikeById = async ({ beerLikeId }: RemoveBeerPostLikeArgs) =>
|
||||
DBClient.instance.beerPostLike.delete({ where: { id: beerLikeId } });
|
||||
|
||||
export default removeBeerPostLikeById;
|
||||
@@ -0,0 +1,11 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const BeerPostLikeSchema = z.object({
|
||||
id: z.string().cuid(),
|
||||
beerPostId: z.string().cuid(),
|
||||
likedById: z.string().cuid(),
|
||||
createdAt: z.coerce.date(),
|
||||
updatedAt: z.coerce.date().nullable(),
|
||||
});
|
||||
|
||||
export default BeerPostLikeSchema;
|
||||
23
src/services/likes/beer-post-like/types/index.ts
Normal file
23
src/services/likes/beer-post-like/types/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import GetUserSchema from '@/services/users/auth/schema/GetUserSchema';
|
||||
|
||||
import { z } from 'zod';
|
||||
import BeerPostLikeSchema from '../schema/BeerPostLikeSchema';
|
||||
|
||||
type User = z.infer<typeof GetUserSchema>;
|
||||
type ReturnSchema = z.infer<typeof BeerPostLikeSchema>;
|
||||
|
||||
export type CreateBeerPostLike = (args: {
|
||||
beerPostId: string;
|
||||
likedById: User['id'];
|
||||
}) => Promise<ReturnSchema>;
|
||||
|
||||
export type FindBeerPostLikeById = (args: {
|
||||
beerPostId: string;
|
||||
likedById: User['id'];
|
||||
}) => Promise<ReturnSchema | null>;
|
||||
|
||||
export type RemoveBeerPostLike = (args: {
|
||||
beerPostLikeId: string;
|
||||
}) => Promise<ReturnSchema>;
|
||||
|
||||
export type GetBeerPostLikeCount = (args: { beerPostId: string }) => Promise<number>;
|
||||
@@ -1,18 +0,0 @@
|
||||
import { z } from 'zod';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import GetUserSchema from '@/services/users/auth/schema/GetUserSchema';
|
||||
|
||||
interface CreateBeerStyleLikeArgs {
|
||||
beerStyleId: string;
|
||||
user: z.infer<typeof GetUserSchema>;
|
||||
}
|
||||
const createBeerStyleLike = async ({ beerStyleId, user }: CreateBeerStyleLikeArgs) => {
|
||||
return DBClient.instance.beerStyleLike.create({
|
||||
data: {
|
||||
beerStyleId,
|
||||
likedById: user.id,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export default createBeerStyleLike;
|
||||
@@ -1,16 +0,0 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
interface FindBeerStyleLikeByIdArgs {
|
||||
beerStyleId: string;
|
||||
likedById: string;
|
||||
}
|
||||
const findBeerStyleLikeById = async ({
|
||||
beerStyleId,
|
||||
likedById,
|
||||
}: FindBeerStyleLikeByIdArgs) => {
|
||||
return DBClient.instance.beerStyleLike.findFirst({
|
||||
where: { beerStyleId, likedById },
|
||||
});
|
||||
};
|
||||
|
||||
export default findBeerStyleLikeById;
|
||||
@@ -1,10 +0,0 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
interface GetBeerStyleLikeCountArgs {
|
||||
beerStyleId: string;
|
||||
}
|
||||
const getBeerStyleLikeCount = async ({ beerStyleId }: GetBeerStyleLikeCountArgs) => {
|
||||
return DBClient.instance.beerStyleLike.count({ where: { beerStyleId } });
|
||||
};
|
||||
|
||||
export default getBeerStyleLikeCount;
|
||||
63
src/services/likes/beer-style-like/index.ts
Normal file
63
src/services/likes/beer-style-like/index.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
import {
|
||||
CreateBeerStyleLike,
|
||||
FindBeerStyleLike,
|
||||
GetBeerStyleLikeCount,
|
||||
RemoveBeerStyleLike,
|
||||
} from './types';
|
||||
|
||||
/**
|
||||
* Creates a new beer style like.
|
||||
*
|
||||
* @param params - The parameters object for creating the beer style like.
|
||||
* @param params.beerStyleId - The ID of the beer style.
|
||||
* @param params.likedById - The ID of the user who will like the beer style.
|
||||
* @returns A promise that resolves to the newly created beer style like.
|
||||
*/
|
||||
export const createBeerStyleLikeService: CreateBeerStyleLike = async ({
|
||||
beerStyleId,
|
||||
likedById,
|
||||
}) =>
|
||||
DBClient.instance.beerStyleLike.create({
|
||||
data: {
|
||||
beerStyle: { connect: { id: beerStyleId } },
|
||||
likedBy: { connect: { id: likedById } },
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Retrieves a beer style like by ID.
|
||||
*
|
||||
* @param params - The parameters object for retrieving the beer style like.
|
||||
* @param params.beerStyleId - The ID of the beer style.
|
||||
* @param params.likedById - The ID of the user who liked the beer style.
|
||||
* @returns A promise that resolves to the beer style like.
|
||||
*/
|
||||
export const findBeerStyleLikeService: FindBeerStyleLike = async ({
|
||||
beerStyleId,
|
||||
likedById,
|
||||
}) => DBClient.instance.beerStyleLike.findFirst({ where: { beerStyleId, likedById } });
|
||||
|
||||
/**
|
||||
* Removes a beer style like.
|
||||
*
|
||||
* @param params - The parameters object for removing the beer style like.
|
||||
* @param params.beerStyleLikeId - The ID of the beer style like to remove.
|
||||
* @returns A promise that resolves to the removed beer style like.
|
||||
*/
|
||||
export const removeBeerStyleLikeService: RemoveBeerStyleLike = async ({
|
||||
beerStyleLikeId,
|
||||
}) => DBClient.instance.beerStyleLike.delete({ where: { id: beerStyleLikeId } });
|
||||
|
||||
/**
|
||||
* Retrieves the number of likes for a beer style.
|
||||
*
|
||||
* @param params - The parameters object for retrieving the number of likes for a beer
|
||||
* style.
|
||||
* @param params.beerStyleId - The ID of the beer style.
|
||||
* @returns A promise that resolves to the number of likes for a beer style.
|
||||
*/
|
||||
export const getBeerStyleLikeCountService: GetBeerStyleLikeCount = async ({
|
||||
beerStyleId,
|
||||
}) => DBClient.instance.beerStyleLike.count({ where: { beerStyleId } });
|
||||
@@ -1,12 +0,0 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
interface RemoveBeerStyleLikeByIdArgs {
|
||||
beerStyleLikeId: string;
|
||||
}
|
||||
const removeBeerStyleLikeById = async ({
|
||||
beerStyleLikeId,
|
||||
}: RemoveBeerStyleLikeByIdArgs) => {
|
||||
return DBClient.instance.beerStyleLike.delete({ where: { id: beerStyleLikeId } });
|
||||
};
|
||||
|
||||
export default removeBeerStyleLikeById;
|
||||
@@ -0,0 +1,11 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const BeerStyleLikeSchema = z.object({
|
||||
id: z.string().cuid(),
|
||||
beerStyleId: z.string().cuid(),
|
||||
likedById: z.string().cuid(),
|
||||
createdAt: z.coerce.date(),
|
||||
updatedAt: z.coerce.date().nullable(),
|
||||
});
|
||||
|
||||
export default BeerStyleLikeSchema;
|
||||
23
src/services/likes/beer-style-like/types/index.ts
Normal file
23
src/services/likes/beer-style-like/types/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import GetUserSchema from '@/services/users/auth/schema/GetUserSchema';
|
||||
import { z } from 'zod';
|
||||
|
||||
import BeerStyleLikeSchema from '../schema/BeerStyleLikeSchema';
|
||||
|
||||
type User = z.infer<typeof GetUserSchema>;
|
||||
type ReturnSchema = z.infer<typeof BeerStyleLikeSchema>;
|
||||
|
||||
export type CreateBeerStyleLike = (args: {
|
||||
beerStyleId: string;
|
||||
likedById: User['id'];
|
||||
}) => Promise<ReturnSchema>;
|
||||
|
||||
export type FindBeerStyleLike = (args: {
|
||||
beerStyleId: string;
|
||||
likedById: User['id'];
|
||||
}) => Promise<ReturnSchema | null>;
|
||||
|
||||
export type RemoveBeerStyleLike = (args: {
|
||||
beerStyleLikeId: string;
|
||||
}) => Promise<ReturnSchema>;
|
||||
|
||||
export type GetBeerStyleLikeCount = (args: { beerStyleId: string }) => Promise<number>;
|
||||
63
src/services/likes/brewery-post-like/index.ts
Normal file
63
src/services/likes/brewery-post-like/index.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
import {
|
||||
CreateBreweryPostLike,
|
||||
FindBreweryPostLike,
|
||||
GetBreweryPostLikeCount,
|
||||
RemoveBreweryPostLike,
|
||||
} from './types';
|
||||
|
||||
/**
|
||||
* Creates a new brewery post like.
|
||||
*
|
||||
* @param params - The parameters object for creating the brewery post like.
|
||||
* @param params.breweryPostId - The ID of the brewery post.
|
||||
* @param params.likedById - The ID of the user who will like the brewery post.
|
||||
* @returns A promise that resolves to the newly created brewery post like.
|
||||
*/
|
||||
export const createBreweryPostLikeService: CreateBreweryPostLike = async ({
|
||||
breweryPostId,
|
||||
likedById,
|
||||
}) =>
|
||||
DBClient.instance.breweryPostLike.create({
|
||||
data: {
|
||||
breweryPost: { connect: { id: breweryPostId } },
|
||||
likedBy: { connect: { id: likedById } },
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Retrieves a brewery post like by ID.
|
||||
*
|
||||
* @param params - The parameters object for retrieving the brewery post like.
|
||||
* @param params.breweryPostId - The ID of the brewery post.
|
||||
* @param params.likedById - The ID of the user who liked the brewery post.
|
||||
* @returns A promise that resolves to the brewery post like.
|
||||
*/
|
||||
export const findBreweryPostLikeService: FindBreweryPostLike = async ({
|
||||
breweryPostId,
|
||||
likedById,
|
||||
}) =>
|
||||
DBClient.instance.breweryPostLike.findFirst({ where: { breweryPostId, likedById } });
|
||||
|
||||
/**
|
||||
* Removes a brewery post like.
|
||||
*
|
||||
* @param params - The parameters object for removing the brewery post like.
|
||||
* @param params.breweryPostLikeId - The ID of the brewery post like to remove.
|
||||
* @returns A promise that resolves to the removed brewery post like.
|
||||
*/
|
||||
export const removeBreweryPostLikeService: RemoveBreweryPostLike = async ({
|
||||
breweryPostLikeId,
|
||||
}) => DBClient.instance.breweryPostLike.delete({ where: { id: breweryPostLikeId } });
|
||||
|
||||
/**
|
||||
* Retrieves the number of likes for a brewery post.
|
||||
*
|
||||
* @param params - The parameters object for retrieving the number of likes for a brewery
|
||||
* @param params.breweryPostId - The ID of the brewery post.
|
||||
* @returns A promise that resolves to the number of likes for a brewery post.
|
||||
*/
|
||||
export const getBreweryPostLikeCountService: GetBreweryPostLikeCount = async ({
|
||||
breweryPostId,
|
||||
}) => DBClient.instance.breweryPostLike.count({ where: { breweryPostId } });
|
||||
@@ -0,0 +1,11 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const BreweryPostLikeSchema = z.object({
|
||||
id: z.string().cuid(),
|
||||
breweryPostId: z.string().cuid(),
|
||||
likedById: z.string().cuid(),
|
||||
createdAt: z.coerce.date(),
|
||||
updatedAt: z.coerce.date().nullable(),
|
||||
});
|
||||
|
||||
export default BreweryPostLikeSchema;
|
||||
24
src/services/likes/brewery-post-like/types/index.ts
Normal file
24
src/services/likes/brewery-post-like/types/index.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import GetUserSchema from '@/services/users/auth/schema/GetUserSchema';
|
||||
import { z } from 'zod';
|
||||
import BreweryPostLikeSchema from '../schema/BreweryPostLikeSchema';
|
||||
|
||||
type User = z.infer<typeof GetUserSchema>;
|
||||
type ReturnSchema = z.infer<typeof BreweryPostLikeSchema>;
|
||||
|
||||
export type CreateBreweryPostLike = (args: {
|
||||
breweryPostId: string;
|
||||
likedById: User['id'];
|
||||
}) => Promise<ReturnSchema>;
|
||||
|
||||
export type FindBreweryPostLike = (args: {
|
||||
breweryPostId: string;
|
||||
likedById: User['id'];
|
||||
}) => Promise<ReturnSchema | null>;
|
||||
|
||||
export type RemoveBreweryPostLike = (args: {
|
||||
breweryPostLikeId: string;
|
||||
}) => Promise<ReturnSchema>;
|
||||
|
||||
export type GetBreweryPostLikeCount = (args: {
|
||||
breweryPostId: string;
|
||||
}) => Promise<number>;
|
||||
Reference in New Issue
Block a user