-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add user routes, controllers and model
- Loading branch information
Showing
12 changed files
with
185 additions
and
43 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
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,115 @@ | ||
import { Request, Response } from 'express' | ||
import { USER_SCHEMA } from '@/models/user_model' | ||
import { isValidObjectId } from 'mongoose' | ||
import { log } from 'console' | ||
|
||
//* @desc Post user | ||
//* route POST /api/user | ||
//? @access Public | ||
export async function post_user(req: Request, res: Response): Promise<void> { | ||
try { | ||
const user_data = req.body | ||
|
||
// check if user email already exists | ||
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 | ||
} | ||
|
||
//* post user | ||
const new_job = await USER_SCHEMA.create(user_data) | ||
res.status(201).json(new_job) | ||
} catch (error) { | ||
log('Error posting user:', error) | ||
res.status(500).json({ error: 'Internal server error' }) | ||
} | ||
} | ||
|
||
//* @desc Get user | ||
//* route GET /api/user/:id | ||
//! @access Private | ||
export async function get_user(req: 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 | ||
} | ||
res.status(200).json(user) | ||
} catch (error) { | ||
log('Error fetching user:', error) | ||
res.status(500).json({ error: 'Internal server error' }) | ||
} | ||
} | ||
|
||
//* @desc Delete job | ||
//* route DELETE /api/user/:id | ||
//! @access Private | ||
export async function delete_user(req: 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' }) | ||
} catch (error) { | ||
log('Error deleting user:', error) | ||
res.status(500).json({ error: 'Internal server error' }) | ||
} | ||
} | ||
|
||
//* @desc Update user | ||
//* route PATCH /api/user/:id | ||
//! @access Private | ||
export async function update_user(req: 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 | ||
} | ||
|
||
// check if user email already exists | ||
const { email } = req.body | ||
const existing_user = await USER_SCHEMA.findOne({ email }) | ||
if (existing_user && existing_user._id.toString() !== id) { | ||
res.status(400).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) | ||
} catch (error) { | ||
log('Error updating user:', error) | ||
res.status(500).json({ error: 'Internal server error' }) | ||
} | ||
} |
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,12 +1,20 @@ | ||
import { log } from '@repo/logger' | ||
import { createServer } from './server' | ||
import job_route from '@/routes/job_route' | ||
import { job_route, user_route } from '@/routes' | ||
import { connect_db } from 'config/db' | ||
|
||
const port = process.env.PORT || 5001 | ||
const server = createServer() | ||
|
||
server.use('/api/v1/jobs', job_route) | ||
server.use('/api/v1/user', user_route) | ||
|
||
server.listen(port, () => { | ||
log(`api running on ${port}`) | ||
}) | ||
connect_db() | ||
.then(() => { | ||
server.listen(port, () => { | ||
log(`db connected & api running on ${port}`) | ||
}) | ||
}) | ||
.catch((error) => { | ||
log('Error connecting to db:', error) | ||
}) |
2 changes: 1 addition & 1 deletion
2
...c/middleware/job_validation_middleware.ts → ...i/src/middleware/validation_middleware.ts
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,14 @@ | ||
import mongoose, { Schema } from 'mongoose' | ||
|
||
const user_schema = new Schema( | ||
{ | ||
name: { type: String, required: true }, | ||
email: { type: String, required: true, unique: true }, | ||
password: { type: String, required: true }, | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
) | ||
|
||
export const USER_SCHEMA = mongoose.model('User', user_schema) |
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,2 @@ | ||
export { default as job_route } from './job_route' | ||
export { default as user_route } from './user_route' |
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,28 @@ | ||
import express from 'express' | ||
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, | ||
update_user, | ||
} from '@/controllers/user_controller' | ||
|
||
//* @desc Post user | ||
//? @access Public | ||
router.post('/', validate_schema(USER_VALIDATION_SCHEMA), post_user) | ||
|
||
//* @desc Get user | ||
//! @access Private | ||
router.get('/:id', get_user) | ||
|
||
//* @desc Delete user | ||
//! @access Private | ||
router.delete('/:id', delete_user) | ||
|
||
//* @desc Update user | ||
//! @access Private | ||
router.patch('/:id', validate_schema(USER_VALIDATION_SCHEMA), update_user) | ||
|
||
export default router |
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,7 @@ | ||
import { z } from 'zod' | ||
|
||
export const USER_VALIDATION_SCHEMA = z.object({ | ||
name: z.string(), | ||
email: z.string().email(), | ||
password: z.string().min(6), | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.