Skip to content

Commit

Permalink
refactor: user routes and controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
hariscs committed Mar 2, 2024
1 parent ca247d0 commit 6e9d219
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
32 changes: 9 additions & 23 deletions apps/api/src/controllers/user_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { generate_token } from 'utils/generate_token'
//* @desc Post user
//* route POST /api/user
//? @access Public
export async function post_user(req: Request, res: Response): Promise<void> {
export async function create_user(req: Request, res: Response): Promise<void> {
try {
const user_data = req.body
// check if user email already exists
Expand All @@ -28,10 +28,10 @@ export async function post_user(req: Request, res: Response): Promise<void> {
}
}

//* @desc Get user
//* @desc Login user
//* route GET /api/user
//! @access Public
export async function get_user(req: Request, res: Response): Promise<void> {
export async function login_user(req: Request, res: Response): Promise<void> {
try {
//* get user by email and password
const { email, password } = req.body
Expand All @@ -50,28 +50,14 @@ export async function get_user(req: Request, res: Response): Promise<void> {
}
}

//* @desc Delete user
//* route DELETE /api/user/:id
//! @access Private
export async function delete_user(req: Request, res: Response): Promise<void> {
//* @desc Logout user
//* route POST /api/user/logout
// ? @access Public
export async function logout_user(_: Request, res: Response): Promise<void> {
try {
const { id } = req.params
//* check if id is valid
if (!isValidObjectId(id)) {
res.status(400).json({ error: 'Invalid id' })
return
}
//* check if user exists
const user = await USER_SCHEMA.findById(id)
if (!user) {
res.status(404).json({ error: 'user not found' })
return
}
//* delete user
await USER_SCHEMA.findByIdAndDelete(id)
res.status(200).json({ message: 'user deleted successfully' })
res.clearCookie('token').status(200).json({ message: 'User logged out' })
} catch (error) {
log('Error deleting user:', error)
log('Error logging out user:', error)
res.status(500).json({ error: 'Internal server error' })
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/models/user_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import mongoose, { Document, Schema } from 'mongoose'
import bcrypt from 'bcrypt'

interface IUserSchema extends Document {
name?: string
email: string
password: string
name?: string
match_password: (password: string) => Promise<boolean>
}

Expand Down
18 changes: 9 additions & 9 deletions apps/api/src/routes/user_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ const router = express.Router()
import { validate_schema } from '@/middleware/validation_middleware'
import { USER_VALIDATION_SCHEMA } from '@/validations/user_validation'
import {
delete_user,
get_user,
post_user,
login_user,
logout_user,
create_user,
update_user,
} from '@/controllers/user_controller'

//* @desc Post user
//? @access Public
router.post('/', validate_schema(USER_VALIDATION_SCHEMA), post_user)
router.post('/', validate_schema(USER_VALIDATION_SCHEMA), create_user)

//* @desc Get user
//* @desc Login user
//? @access Public
router.get('/', validate_schema(USER_VALIDATION_SCHEMA), get_user)
router.get('/', validate_schema(USER_VALIDATION_SCHEMA), login_user)

//* @desc Delete user
//! @access Private
router.delete('/:id', delete_user)
//* @desc Logout user
//? @access Public
router.post('/logout', logout_user)

//* @desc Update user
//! @access Private
Expand Down

0 comments on commit 6e9d219

Please sign in to comment.