-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from kentibs/setup/monitoring
feat: add post event api
- Loading branch information
Showing
8 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"cSpell.words": ["sendgrid", "signin", "superadmin", "uuidv"] | ||
"cSpell.words": ["fieldname", "sendgrid", "signin", "superadmin", "uuidv"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
-- CreateEnum | ||
CREATE TYPE "EventCategory" AS ENUM ('exhibition', 'conference'); | ||
|
||
-- CreateTable | ||
CREATE TABLE "_events" ( | ||
"eventId" TEXT NOT NULL, | ||
"postByUserId" TEXT NOT NULL, | ||
"category" "EventCategory" NOT NULL DEFAULT 'exhibition', | ||
"title" TEXT NOT NULL, | ||
"description" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3), | ||
|
||
CONSTRAINT "_events_pkey" PRIMARY KEY ("eventId") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "_event_images" ( | ||
"eventImageId" TEXT NOT NULL, | ||
"eventId" TEXT NOT NULL, | ||
"imageUrl" TEXT, | ||
"imagePath" TEXT, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3), | ||
|
||
CONSTRAINT "_event_images_pkey" PRIMARY KEY ("eventImageId") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_events_eventId_idx" ON "_events"("eventId"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_events_postByUserId_idx" ON "_events"("postByUserId"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_events_category_idx" ON "_events"("category"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_event_images_eventImageId_idx" ON "_event_images"("eventImageId"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_event_images_eventId_idx" ON "_event_images"("eventId"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_events" ADD CONSTRAINT "_events_postByUserId_fkey" FOREIGN KEY ("postByUserId") REFERENCES "_users"("userId") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_event_images" ADD CONSTRAINT "_event_images_eventId_fkey" FOREIGN KEY ("eventId") REFERENCES "_events"("eventId") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { AppError } from "../utils/error"; | ||
import { asyncHandler } from "../utils/asyncHandler"; | ||
import { PrismaClient } from "@prisma/client"; | ||
import { Upload } from "../utils/upload"; | ||
import { EventCategory } from "../types/eventCategory"; | ||
import { TFile } from "../types/file"; | ||
|
||
const prisma = new PrismaClient(); | ||
const Event = prisma.event; | ||
const EventImage = prisma.eventImage; | ||
|
||
const validateEventCategory = (event: string): boolean => { | ||
const isExhibition = event === EventCategory.EXHIBITION; | ||
const isConference = event === EventCategory.CONFERENCE; | ||
|
||
if (isExhibition || isConference) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
export const postEvent = asyncHandler( | ||
async (req: Request, res: Response, next: NextFunction) => { | ||
const title = req.body.title as string; | ||
const category = req.body.category as EventCategory; | ||
const description = req.body.description as string; | ||
const userId = res.locals.user.userId; | ||
const files = req.files as TFile[]; | ||
|
||
if (!userId) { | ||
return next(new AppError("Please provide the userId", 400)); | ||
} | ||
if (!title || !category || !description) { | ||
return next(new AppError("Please fill out all fields", 400)); | ||
} | ||
if (!validateEventCategory(category)) { | ||
return next(new AppError("Please provide a valid event category", 400)); | ||
} | ||
if (files == undefined) { | ||
return next(new AppError("Please Provide at least one event photo", 400)); | ||
} | ||
|
||
const newEvent = await Event.create({ | ||
data: { | ||
title: title, | ||
category: category, | ||
description: description, | ||
postByUserId: userId, | ||
}, | ||
select: { | ||
eventId: true, | ||
title: true, | ||
category: true, | ||
description: true, | ||
createdAt: true, | ||
updatedAt: true, | ||
}, | ||
}); | ||
res.locals.event = newEvent; | ||
next(); | ||
} | ||
); | ||
|
||
export const uploadEventImages = asyncHandler( | ||
async (req: Request, res: Response, next: NextFunction) => { | ||
const files = req.files as TFile[]; | ||
|
||
const event = res.locals.event; | ||
const eventId = event.eventId as string; | ||
|
||
for (let i = 0; i < files.length; i++) { | ||
const imagePath = `events/${Date.now()}_${files[i].originalname}`; | ||
const upload = await new Upload(imagePath, next).add(files[i]); | ||
const url = upload?.url as string; | ||
|
||
console.log("url from firebase"); | ||
console.log(url); | ||
|
||
await EventImage.create({ | ||
data: { eventId: eventId, imageUrl: url, imagePath: imagePath }, | ||
}); | ||
} | ||
|
||
const eventImages = await EventImage.findMany({ | ||
where: { eventId: { equals: eventId } }, | ||
select: { imageUrl: true }, | ||
}); | ||
event.eventImages = eventImages; | ||
|
||
res.status(201).json({ | ||
status: "success", | ||
message: "Event created successfully", | ||
data: { event: event }, | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import express from "express"; | ||
import { protectSuperAdmin } from "../../controllers/userController"; | ||
import { uploadFiles } from "../../utils/upload"; | ||
import { | ||
postEvent, | ||
uploadEventImages, | ||
} from "../../controllers/eventsController"; | ||
|
||
const router = express.Router(); | ||
|
||
router.post( | ||
"/post-event", | ||
uploadFiles, | ||
protectSuperAdmin, | ||
postEvent, | ||
uploadEventImages | ||
); | ||
|
||
export { router as eventRoutes }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum EventCategory { | ||
EXHIBITION = "exhibition", | ||
CONFERENCE = "conference", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export type TFile = { | ||
fieldname: string; | ||
originalname: string; | ||
encoding: string; | ||
mimetype: string; | ||
buffer: Buffer; | ||
size: number; | ||
}; |