-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Refactor backend and client code
- Loading branch information
1 parent
3e13189
commit 0ff3442
Showing
30 changed files
with
710 additions
and
495 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,109 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { | ||
addNewBook, | ||
deleteBook, | ||
getAllBooksService, | ||
getBookById, | ||
getRecentlyAddedBooks, | ||
searchBooksService, | ||
updateBook, | ||
updateCategories, | ||
} from '../services/book'; | ||
|
||
export const getAllBooksController = async (req: Request, res: Response) => { | ||
try { | ||
const books = await getAllBooksService(req); | ||
res.json(books); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
res.status(500).json({ error: err.message }); | ||
} else { | ||
res.status(500).json({ error: 'An unknown error occurred' }); | ||
} | ||
} | ||
}; | ||
|
||
export const searchBooksController = async (req: Request, res: Response) => { | ||
try { | ||
const books = await searchBooksService(req); | ||
res.json(books); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
res.status(500).json({ error: err.message }); | ||
} else { | ||
res.status(500).json({ error: 'An unknown error occurred' }); | ||
} | ||
} | ||
}; | ||
export const addBookController = async (req: Request, res: Response) => { | ||
try { | ||
const books = await addNewBook(req); | ||
|
||
res.status(201).json(books); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
res.status(500).json({ error: err.message }); | ||
} else { | ||
res.status(500).json({ error: 'An unknown error occurred' }); | ||
} | ||
} | ||
}; | ||
|
||
export const recentlyAddedBooksController = async ( | ||
req: Request, | ||
res: Response | ||
) => { | ||
try { | ||
const books = await getRecentlyAddedBooks(); | ||
res.json(books); | ||
} catch (err) { | ||
console.log(err); | ||
res.status(500).json({ error: 'An unknown error occurred' }); | ||
} | ||
}; | ||
|
||
export const deleteBookController = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
try { | ||
const book = await deleteBook(req); | ||
res.status(201).json(book); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
export const getBookByIdController = async (req: Request, res: Response) => { | ||
try { | ||
const book = await getBookById(req); | ||
res.status(201).json(book); | ||
} catch (err) { | ||
console.log(err); | ||
res.status(500).json({ error: 'An unknown error occurred' }); | ||
} | ||
}; | ||
|
||
export const updateCategoriesController = async ( | ||
req: Request, | ||
res: Response | ||
) => { | ||
try { | ||
await updateCategories(); | ||
res.status(200).send('Categories updated'); | ||
} catch (err) { | ||
console.log(err); | ||
res.status(500).json({ error: 'An unknown error occurred' }); | ||
} | ||
}; | ||
|
||
export const updateBookController = async (req: Request, res: Response) => { | ||
try { | ||
const book = await updateBook(req); | ||
res.status(201).json(book); | ||
} catch (err) { | ||
console.log(err); | ||
res.status(500).json({ error: 'An unknown error occurred' }); | ||
} | ||
}; |
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,33 @@ | ||
import { Request, Response } from 'express'; | ||
import { | ||
createUserMessage, | ||
deleteMessage, | ||
getUserMessages, | ||
} from '../services/message/userMessages.service'; | ||
|
||
export const getUserMessagesController = async ( | ||
req: Request, | ||
res: Response | ||
) => { | ||
const userMessages = await getUserMessages(); | ||
res.json(userMessages); | ||
}; | ||
|
||
export const createUserMessageController = async ( | ||
req: Request, | ||
res: Response | ||
) => { | ||
const { text, sender } = req.body; | ||
try { | ||
const userMessages = await createUserMessage(text, sender); | ||
res.status(201).json(userMessages); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
||
export const deleteMessageController = async (req: Request, res: Response) => { | ||
const { id } = req.body; | ||
const result = await deleteMessage(id, req.body.user.isAdmin); | ||
res.status(201).json(result); | ||
}; |
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,38 @@ | ||
import { Request, Response } from 'express'; | ||
import { | ||
authenticateUser, | ||
loginUser, | ||
logoutUser, | ||
registerUser, | ||
} from '../services/user'; | ||
|
||
export const loginController = async (req: Request, res: Response) => { | ||
const { username, password } = req.body; | ||
const result = await loginUser(username, password); | ||
res.status(201).json(result); | ||
}; | ||
|
||
export const registerController = async (req: Request, res: Response) => { | ||
const { username, email, password, isAdmin = false } = req.body; | ||
const result = await registerUser(username, email, password, isAdmin); | ||
res.status(201).json(result); | ||
}; | ||
|
||
export const authController = async (req: Request, res: Response) => { | ||
try { | ||
const token = req.header('Authorization')?.split(' ')[1] || ''; | ||
const result = await authenticateUser(req.body.user.id, token); | ||
res.json(result); | ||
} catch (err: any) { | ||
res.status(500).json({ error: err.message }); | ||
} | ||
}; | ||
|
||
export const logoutController = async (req: Request, res: Response) => { | ||
try { | ||
await logoutUser(req.body.user.id); | ||
res.status(201).json({}); | ||
} catch (err: any) { | ||
res.status(500).json({ message: 'User logged out successfully' }); | ||
} | ||
}; |
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 |
---|---|---|
|
@@ -27,6 +27,4 @@ app.get( | |
} | ||
); | ||
|
||
module.exports = { | ||
updateMetrics, | ||
}; | ||
export { app, updateMetrics }; |
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
Oops, something went wrong.