mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Feat: add create brewery post, brewery image upload
Add address autocomplete, using MapBox
This commit is contained in:
@@ -11,7 +11,7 @@ interface ProcessImageDataArgs {
|
||||
userId: string;
|
||||
}
|
||||
|
||||
const processImageDataIntoDB = ({
|
||||
const addBeerImageToDB = ({
|
||||
alt,
|
||||
caption,
|
||||
files,
|
||||
@@ -36,4 +36,4 @@ const processImageDataIntoDB = ({
|
||||
return Promise.all(beerImagePromises);
|
||||
};
|
||||
|
||||
export default processImageDataIntoDB;
|
||||
export default addBeerImageToDB;
|
||||
39
src/services/BreweryImage/addBreweryImageToDB.ts
Normal file
39
src/services/BreweryImage/addBreweryImageToDB.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { BreweryImage } from '@prisma/client';
|
||||
import { z } from 'zod';
|
||||
import ImageMetadataValidationSchema from '../types/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;
|
||||
55
src/services/BreweryPost/createNewBreweryPost.ts
Normal file
55
src/services/BreweryPost/createNewBreweryPost.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import CreateBreweryPostSchema from './types/CreateBreweryPostSchema';
|
||||
import BreweryPostQueryResult from './types/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>) => {
|
||||
const breweryPost: z.infer<typeof BreweryPostQueryResult> =
|
||||
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 } },
|
||||
location: {
|
||||
select: {
|
||||
city: true,
|
||||
address: true,
|
||||
coordinates: true,
|
||||
country: true,
|
||||
stateOrProvince: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return breweryPost;
|
||||
};
|
||||
|
||||
export default createNewBreweryPost;
|
||||
55
src/services/BreweryPost/types/CreateBreweryPostSchema.ts
Normal file
55
src/services/BreweryPost/types/CreateBreweryPostSchema.ts
Normal file
@@ -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(500, { 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(100, { 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;
|
||||
@@ -37,6 +37,9 @@ export const BaseCreateUserSchema = z.object({
|
||||
}),
|
||||
dateOfBirth: z
|
||||
.string()
|
||||
.refine((val) => !Number.isNaN(Date.parse(val)), {
|
||||
message: 'Date is invalid.',
|
||||
})
|
||||
.refine((dateOfBirth) => new Date(dateOfBirth) <= MINIMUM_DATE_OF_BIRTH, {
|
||||
message: 'You must be at least 19 years old to register.',
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user