Skip to content

Commit

Permalink
fix: update api
Browse files Browse the repository at this point in the history
  • Loading branch information
BharathSanjeeviT committed Feb 26, 2024
1 parent 6876324 commit 2f388f3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/controllers/adminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const GetUsersFromEvent = async (req: Request, res: Response) => {
const client = await pool.connect();
const { event_id } = EventIdValidator.parse(req.params);
const data = await client.query(getUsersforEvent, [event_id]);

client.release()
return res.status(200).json({
statusCode: 200,
body: { message: "Sucessfull", data: data.rows },
Expand All @@ -182,6 +182,7 @@ const UpdateUserCart = async (req: Request, res: Response) => {
await client.query(insertMissingOnes, [user_email, events_id]);
await client.query(deleteFromCart, [user_email, events_id]);
await client.query(commit);
client.release()

return res.status(200).json({
statusCode: 200,
Expand Down
30 changes: 30 additions & 0 deletions src/controllers/eventController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { Response, Request } from "express";
import { pool } from "../../config/db.js";
import {
createEvent,
editEvent
} from "../queries/eventQueries.js";

const CreateEvent = async (req: Request, res: Response) => {
if(req.body.admin.is_super_admin){
const { event_id, name, fee, pass_id} = req.body
const client = await pool.connect()
await client.query(createEvent, [event_id, name, fee, pass_id])
client.release()
return res
.status(200)
.json({ status: 200, body: { message: "Sucessfull" } });
Expand All @@ -18,6 +20,34 @@ const CreateEvent = async (req: Request, res: Response) => {
.json({ status: 403, body: { message: "Not Authorized" } });
};

const UpdateEvent = async (req: Request, res: Response) => {
if(req.body.admin.is_super_admin || (req.body.admin.events_id.includes(req.body.event_id))){
try{
const { event_id, name, fee, pass_id} = req.body
const client = await pool.connect()
const data = await client.query(editEvent, [event_id, name, fee, pass_id])
client.release()
if(data.rows.length == 1)
return res
.status(200)
.json({ status: 200, body: { message: "Updated Sucessfull" } });
else
return res
.status(500)
.json({ status: 500, body: { message: "Event doesnot exist" } });
} catch(err){
console.log(err)
return res
.status(500)
.json({ status: 500, body: { message: "Event not updated" } });
}
}else
return res
.status(403)
.json({ status: 403, body: { message: "Not Authorized" } });
};

export {
CreateEvent,
UpdateEvent
};
3 changes: 3 additions & 0 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const GetUserDetails = async (req: Request, res: Response) => {
const { email } = emailValidator.parse(req.body.user);
const client = await pool.connect();
const result = await client.query(getUserDetails, [email]);
client.release()
return res.status(200).json({
statusCode: 200,
body: { message: "Request Sucessfull", data: result.rows },
Expand Down Expand Up @@ -53,6 +54,7 @@ const GetUserCart = async (req: Request, res: Response) => {
const { email } = emailValidator.parse(req.body.user);
const client = await pool.connect();
const data = await client.query(getCart, [email]);
client.release()
let cartItems: Record<string, Array<cartType>> = {};
data.rows.forEach((event: cartType) => {
if (!cartItems[event.pass_id]) cartItems[event.pass_id] = [];
Expand All @@ -78,6 +80,7 @@ const UpdateUserCart = async (
await client.query(insertMissingOnes, [email, events_id]);
await client.query(deleteFromCart, [email, events_id]);
await client.query(commit);
client.release()

return res.status(200).json({
statusCode: 200,
Expand Down
1 change: 1 addition & 0 deletions src/middlewares/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function errorHandler(
},
});
}
console.log(err)
return res.status(500).json({
statusCode: 500,
body: {
Expand Down
5 changes: 5 additions & 0 deletions src/queries/eventQueries.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
export const createEvent: string = `INSERT INTO
events (id, name, fee, pass_id) VALUES ($1, $2, $3, $4)`;
export const editEvent: string = `UPDATE events SET
name = $2,
fee = $3,
pass_id = $4
WHERE id = $1 returning *`;
3 changes: 2 additions & 1 deletion src/routes/eventsRoute.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import express from "express";
import { Module } from "../../libs/utils/types/module.js";
import { CreateEvent } from "../controllers/eventController.js";
import { CreateEvent, UpdateEvent } from "../controllers/eventController.js";
import asyncMiddleware from "../middlewares/asyncMiddleware.js";

const router = express.Router();
const BASE_ROUTE = "/events";

router.post("/", asyncMiddleware(CreateEvent));
router.put("/", asyncMiddleware(UpdateEvent));

const MODULE: Module = {
router,
Expand Down

0 comments on commit 2f388f3

Please sign in to comment.