refactor image services

This commit is contained in:
Aaron William Po
2023-12-13 09:49:31 -05:00
parent 685c86e0c1
commit fdbadb63dc
17 changed files with 285 additions and 99 deletions

View File

@@ -1,40 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { BeerImage } from '@prisma/client';
import { z } from 'zod';
import ImageMetadataValidationSchema from '../../schema/ImageSchema/ImageMetadataValidationSchema';
interface ProcessImageDataArgs {
files: Express.Multer.File[];
alt: z.infer<typeof ImageMetadataValidationSchema>['alt'];
caption: z.infer<typeof ImageMetadataValidationSchema>['caption'];
beerPostId: string;
userId: string;
}
const addBeerImageToDB = ({
alt,
caption,
files,
beerPostId,
userId,
}: ProcessImageDataArgs) => {
const beerImagePromises: Promise<BeerImage>[] = [];
files.forEach((file) => {
beerImagePromises.push(
DBClient.instance.beerImage.create({
data: {
alt,
caption,
postedBy: { connect: { id: userId } },
beerPost: { connect: { id: beerPostId } },
path: file.path,
},
}),
);
});
return Promise.all(beerImagePromises);
};
export default addBeerImageToDB;

View File

@@ -0,0 +1,87 @@
import DBClient from '@/prisma/DBClient';
import { BeerImage } from '@prisma/client';
import { cloudinary } from '@/config/cloudinary';
import {
AddBeerImagesToDB,
DeleteBeerImageFromDBAndStorage,
UpdateBeerImageMetadata,
} from './types';
/**
* Adds beer images to the database.
*
* @param options - The options for adding beer images.
* @param options.body - The body of the request.
* @param options.body.alt - The alt text for the beer image.
* @param options.body.caption - The caption for the beer image.
* @param options.files - The array of files to be uploaded as beer images.
* @param options.beerPostId - The ID of the beer post.
* @param options.userId - The ID of the user.
* @returns A promise that resolves to an array of created beer images.
*/
export const addBeerImagesToDB: AddBeerImagesToDB = ({
body,
files,
beerPostId,
userId,
}) => {
const beerImagePromises: Promise<BeerImage>[] = [];
const { alt, caption } = body;
files.forEach((file) => {
beerImagePromises.push(
DBClient.instance.beerImage.create({
data: {
alt,
caption,
postedBy: { connect: { id: userId } },
beerPost: { connect: { id: beerPostId } },
path: file.path,
},
}),
);
});
return Promise.all(beerImagePromises);
};
/**
* Deletes a beer image from the database and storage.
*
* @param options - The options for deleting a beer image.
* @param options.beerImageId - The ID of the beer image.
*/
export const deleteBeerImageFromDBAndStorage: DeleteBeerImageFromDBAndStorage = async ({
beerImageId,
}) => {
const deleted = await DBClient.instance.beerImage.delete({
where: { id: beerImageId },
select: { path: true, id: true },
});
const { path } = deleted;
await cloudinary.uploader.destroy(path);
};
/**
* Updates the beer image metadata in the database.
*
* @param options - The options for updating the beer image metadata.
* @param options.beerImageId - The ID of the beer image.
* @param options.body - The body of the request containing the alt and caption.
* @param options.body.alt - The alt text for the beer image.
* @param options.body.caption - The caption for the beer image.
* @returns A promise that resolves to the updated beer image.
*/
export const updateBeerImageMetadata: UpdateBeerImageMetadata = async ({
beerImageId,
body,
}) => {
const { alt, caption } = body;
const updated = await DBClient.instance.beerImage.update({
where: { id: beerImageId },
data: { alt, caption },
});
return updated;
};

View File

@@ -0,0 +1,19 @@
import ImageMetadataValidationSchema from '@/services/schema/ImageSchema/ImageMetadataValidationSchema';
import { BeerImage } from '@prisma/client';
import { z } from 'zod';
export type AddBeerImagesToDB = (args: {
files: Express.Multer.File[];
body: z.infer<typeof ImageMetadataValidationSchema>;
beerPostId: string;
userId: string;
}) => Promise<BeerImage[]>;
export type DeleteBeerImageFromDBAndStorage = (args: {
beerImageId: string;
}) => Promise<void>;
export type UpdateBeerImageMetadata = (args: {
beerImageId: string;
body: z.infer<typeof ImageMetadataValidationSchema>;
}) => Promise<BeerImage>;

View File

@@ -1,39 +0,0 @@
import DBClient from '@/prisma/DBClient';
import { BreweryImage } from '@prisma/client';
import { z } from 'zod';
import ImageMetadataValidationSchema from '../../schema/ImageSchema/ImageMetadataValidationSchema';
interface ProcessImageDataArgs {
files: Express.Multer.File[];
alt: z.infer<typeof ImageMetadataValidationSchema>['alt'];
caption: z.infer<typeof ImageMetadataValidationSchema>['caption'];
breweryPostId: string;
userId: string;
}
const addBreweryImageToDB = ({
alt,
caption,
files,
breweryPostId,
userId,
}: ProcessImageDataArgs) => {
const breweryImagePromises: Promise<BreweryImage>[] = [];
files.forEach((file) => {
breweryImagePromises.push(
DBClient.instance.breweryImage.create({
data: {
alt,
caption,
postedBy: { connect: { id: userId } },
breweryPost: { connect: { id: breweryPostId } },
path: file.path,
},
}),
);
});
return Promise.all(breweryImagePromises);
};
export default addBreweryImageToDB;

View File

@@ -0,0 +1,86 @@
import DBClient from '@/prisma/DBClient';
import { BreweryImage } from '@prisma/client';
import { cloudinary } from '@/config/cloudinary';
import {
AddBreweryImagesToDB,
DeleteBreweryImageFromDBAndStorage,
UpdateBreweryImageMetadata,
} from './types';
/**
* Adds brewery images to the database.
*
* @param options - The options for adding brewery images.
* @param options.body - The body of the request containing the alt and caption.
* @param options.body.alt - The alt text for the brewery image.
* @param options.body.caption - The caption for the brewery image.
* @param options.files - The array of files to be uploaded as brewery images.
* @param options.breweryPostId - The ID of the brewery post.
* @param options.userId - The ID of the user adding the images.
* @returns A promise that resolves to an array of created brewery images.
*/
export const addBreweryImagesToDB: AddBreweryImagesToDB = ({
body,
files,
breweryPostId,
userId,
}) => {
const breweryImagePromises: Promise<BreweryImage>[] = [];
const { alt, caption } = body;
files.forEach((file) => {
breweryImagePromises.push(
DBClient.instance.breweryImage.create({
data: {
alt,
caption,
postedBy: { connect: { id: userId } },
breweryPost: { connect: { id: breweryPostId } },
path: file.path,
},
}),
);
});
return Promise.all(breweryImagePromises);
};
/**
* Deletes a brewery image from the database and storage.
*
* @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);
};
/**
* Updates the brewery image metadata in the database.
*
* @param options - The options for updating the brewery image metadata.
* @param options.breweryImageId - The ID of the brewery image.
* @param options.body - The body of the request containing the alt and caption.
* @param options.body.alt - The alt text for the brewery image.
* @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 ({
breweryImageId,
body,
}) => {
const { alt, caption } = body;
const updated = await DBClient.instance.breweryImage.update({
where: { id: breweryImageId },
data: { alt, caption },
});
return updated;
};

View File

@@ -0,0 +1,19 @@
import ImageMetadataValidationSchema from '@/services/schema/ImageSchema/ImageMetadataValidationSchema';
import { BreweryImage } from '@prisma/client';
import { z } from 'zod';
export type AddBreweryImagesToDB = (args: {
files: Express.Multer.File[];
body: z.infer<typeof ImageMetadataValidationSchema>;
breweryPostId: string;
userId: string;
}) => Promise<BreweryImage[]>;
export type DeleteBreweryImageFromDBAndStorage = (args: {
breweryImageId: string;
}) => Promise<void>;
export type UpdateBreweryImageMetadata = (args: {
breweryImageId: string;
body: z.infer<typeof ImageMetadataValidationSchema>;
}) => Promise<BreweryImage>;