From bd71602b30f601bcbbeaab2091d5a7c875b6e375 Mon Sep 17 00:00:00 2001 From: Haris Shah Date: Sun, 10 Mar 2024 00:13:17 +0500 Subject: [PATCH] fix: Formatting --- .env.example | 2 +- apps/api/src/controllers/user_controller.ts | 88 ++++++++++----------- apps/api/src/routes/user_route.ts | 30 +++---- 3 files changed, 58 insertions(+), 62 deletions(-) diff --git a/.env.example b/.env.example index e835890..e18a6b5 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ NODE_ENV=development PORT=5000 -MONGO_URI=YOUR_MONGO_UR +MONGO_URI=YOUR_MONGO_URI JWT_SECRET=YOUR_JWT_SECRET \ No newline at end of file diff --git a/apps/api/src/controllers/user_controller.ts b/apps/api/src/controllers/user_controller.ts index 4dccd1c..3d908fc 100644 --- a/apps/api/src/controllers/user_controller.ts +++ b/apps/api/src/controllers/user_controller.ts @@ -1,8 +1,8 @@ -import { Request, Response } from "express"; -import { USER_SCHEMA } from "@/models/user_model"; -import { isValidObjectId } from "mongoose"; -import { log } from "console"; -import { generate_token } from "utils/generate_token"; +import { Request, Response } from 'express' +import { USER_SCHEMA } from '@/models/user_model' +import { isValidObjectId } from 'mongoose' +import { log } from 'console' +import { generate_token } from 'utils/generate_token' //* @desc Register user //* route POST /api/user @@ -12,22 +12,22 @@ export async function register_user( res: Response ): Promise { try { - const user_data = req.body; + const user_data = req.body // check if user email already exists - const { email } = req.body; - const existing_user = await USER_SCHEMA.findOne({ email }); + const { email } = req.body + const existing_user = await USER_SCHEMA.findOne({ email }) if (existing_user) { - res.status(409).json({ error: "User email already exists" }); - return; + res.status(409).json({ error: 'User email already exists' }) + return } //* post user - const user = await USER_SCHEMA.create(user_data); - generate_token(res, user._id.toString()); - res.status(201).json(user); + const user = await USER_SCHEMA.create(user_data) + generate_token(res, user._id.toString()) + res.status(201).json(user) } catch (error) { - log("Error posting user:", error); - res.status(500).json({ error: "Internal server error" }); + log('Error posting user:', error) + res.status(500).json({ error: 'Internal server error' }) } } @@ -37,19 +37,19 @@ export async function register_user( export async function login_user(req: Request, res: Response): Promise { try { //* get user by email and password - const { email, password } = req.body; - const user = await USER_SCHEMA.findOne({ email }); + const { email, password } = req.body + const user = await USER_SCHEMA.findOne({ email }) // check if user exists and password is correct if (!user || !(await user.match_password(password))) { - res.status(401).json({ error: "Incorrect email or password" }); - return; + res.status(401).json({ error: 'Incorrect email or password' }) + return } - generate_token(res, user._id.toString()); - res.status(200).json(user); + generate_token(res, user._id.toString()) + res.status(200).json(user) } catch (error) { - log("Error fetching user:", error); - res.status(500).json({ error: "Internal server error" }); + log('Error fetching user:', error) + res.status(500).json({ error: 'Internal server error' }) } } @@ -58,10 +58,10 @@ export async function login_user(req: Request, res: Response): Promise { // ? @access Public export async function logout_user(_: Request, res: Response): Promise { try { - res.clearCookie("token").status(200).json({ message: "User logged out" }); + res.clearCookie('token').status(200).json({ message: 'User logged out' }) } catch (error) { - log("Error logging out user:", error); - res.status(500).json({ error: "Internal server error" }); + log('Error logging out user:', error) + res.status(500).json({ error: 'Internal server error' }) } } @@ -70,35 +70,35 @@ export async function logout_user(_: Request, res: Response): Promise { //! @access Private export async function update_user(req: Request, res: Response): Promise { try { - const { id } = req.params; + const { id } = req.params //* check if id is valid if (!isValidObjectId(id)) { - res.status(400).json({ error: "Invalid id" }); - return; + res.status(400).json({ error: 'Invalid id' }) + return } //* check if user exists - const user = await USER_SCHEMA.findById(id); + const user = await USER_SCHEMA.findById(id) if (!user) { - res.status(404).json({ error: "user not found" }); - return; + res.status(404).json({ error: 'user not found' }) + return } //* check if user email already exists - const { email } = req.body; - const existing_user = await USER_SCHEMA.findOne({ email }); + const { email } = req.body + const existing_user = await USER_SCHEMA.findOne({ email }) if (existing_user && existing_user._id.toString() !== id) { - res.status(409).json({ error: "Email already exists" }); - return; + res.status(409).json({ error: 'Email already exists' }) + return } //* update user const updated_user = await USER_SCHEMA.findByIdAndUpdate(id, req.body, { new: true, - }); - res.status(200).json(updated_user); + }) + res.status(200).json(updated_user) } catch (error) { - log("Error updating user:", error); - res.status(500).json({ error: "Internal server error" }); + log('Error updating user:', error) + res.status(500).json({ error: 'Internal server error' }) } } @@ -111,10 +111,10 @@ export async function get_user(req: Request, res: Response): Promise { name: req.user?.name, email: req.user?.email, _id: req.user?._id, - }; - res.status(200).json(user); + } + res.status(200).json(user) } catch (error) { - log("Error fetching user:", error); - res.status(500).json({ error: "Internal server error" }); + log('Error fetching user:', error) + res.status(500).json({ error: 'Internal server error' }) } } diff --git a/apps/api/src/routes/user_route.ts b/apps/api/src/routes/user_route.ts index 6d1a848..19e718f 100644 --- a/apps/api/src/routes/user_route.ts +++ b/apps/api/src/routes/user_route.ts @@ -1,43 +1,39 @@ -import express from "express"; -const router = express.Router(); -import { validate_schema } from "@/middleware/validation_middleware"; -import { USER_VALIDATION_SCHEMA } from "@/validations/user_validation"; +import express from 'express' +const router = express.Router() +import { validate_schema } from '@/middleware/validation_middleware' +import { USER_VALIDATION_SCHEMA } from '@/validations/user_validation' import { login_user, logout_user, register_user, update_user, get_user, -} from "@/controllers/user_controller"; -import { protect_route } from "@/middleware/auth_middleware"; +} from '@/controllers/user_controller' +import { protect_route } from '@/middleware/auth_middleware' //* @desc Create user //? @access Public -router.post( - "/register", - validate_schema(USER_VALIDATION_SCHEMA), - register_user -); +router.post('/register', validate_schema(USER_VALIDATION_SCHEMA), register_user) //* @desc Login user //? @access Public -router.post("/login", validate_schema(USER_VALIDATION_SCHEMA), login_user); +router.post('/login', validate_schema(USER_VALIDATION_SCHEMA), login_user) //* @desc Logout user //? @access Public -router.post("/logout", logout_user); +router.post('/logout', logout_user) //* @desc Get User //! @access Private -router.get("/profile", protect_route, get_user); +router.get('/profile', protect_route, get_user) //* @desc Update user //! @access Private router.patch( - "/:id", + '/:id', protect_route, validate_schema(USER_VALIDATION_SCHEMA), update_user -); +) -export default router; +export default router