-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3073b0
commit 0414ee7
Showing
7 changed files
with
369 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { FolderModel, type folderType } from "../models/folder.model"; | ||
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors"; | ||
import { checkDuplicateItemName } from "../utils/checkDuplicates"; | ||
|
||
export const createFolder = async (foldersFields: folderType) => { | ||
try { | ||
if(await checkDuplicateItemName(foldersFields.itemName)){ | ||
throw new HttpError( | ||
HttpStatus.BAD_REQUEST, | ||
"Duplicate folder name", | ||
) | ||
} | ||
|
||
const newFolders = new FolderModel(foldersFields); | ||
await newFolders.save(); | ||
return newFolders; | ||
} catch (err: unknown) { | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
|
||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Folder creation failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const getAllFolders = async (user: string) => { | ||
try { | ||
const folders = await FolderModel.find({ user: user }); | ||
return folders; | ||
} catch (err: unknown) { | ||
//rethrow any errors as HttpErrors | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
//checks if mongoose threw and will rethrow with appropriate status code and message | ||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Folders retrieval failed", | ||
{ cause: err }, | ||
); | ||
} | ||
} | ||
|
||
export const getFolderById = async (user: string, folderId: string) => { | ||
try { | ||
const folder = await FolderModel.findOne({ | ||
user: user, | ||
_id: folderId, | ||
}); | ||
return folder; | ||
} catch (err: unknown) { | ||
//rethrow any errors as HttpErrors | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
//checks if mongoose threw and will rethrow with appropriate status code and message | ||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Folder retrieval failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const updateFolder = async ( | ||
user: string, | ||
folderId: string, | ||
foldersFields: folderType, | ||
) => { | ||
try { | ||
if (!folderId) { | ||
throw new HttpError( | ||
HttpStatus.BAD_REQUEST, | ||
"Missing folder ID for update", | ||
); | ||
} | ||
|
||
if (await checkDuplicateItemName(foldersFields.itemName, folderId)) { | ||
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); | ||
} | ||
|
||
const updatedFolder = await FolderModel.findOneAndUpdate( | ||
{ _id: folderId, user: user }, // Query to match the document by _id and user | ||
{ $set: foldersFields }, // Update operation | ||
{ new: true, runValidators: true }, // Options: return the updated document and run schema validators | ||
); | ||
return updatedFolder; | ||
} catch (err: unknown) { | ||
//rethrow any errors as HttpErrors | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
//checks if mongoose threw and will rethrow with appropriate status code and message | ||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Folder update failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const deleteFolder = async (user: string, folderId: string) => { | ||
try { | ||
const deletedFolder = await FolderModel.findOneAndDelete({ | ||
_id: folderId, | ||
user: user, | ||
}); | ||
if (!deletedFolder) { | ||
throw new HttpError( | ||
HttpStatus.NOT_FOUND, | ||
"Folder not found or already deleted", | ||
); | ||
} | ||
return { message: "Folder deleted successfully" }; | ||
} catch (err: unknown) { | ||
//rethrow any errors as HttpErrors | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
//checks if mongoose threw and will rethrow with appropriate status code and message | ||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Folder deletion failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; |
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
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,118 @@ | ||
import { Router, type Request, type Response } from "express"; | ||
import { | ||
createFolder, | ||
getAllFolders, | ||
getFolderById, | ||
updateFolder, | ||
deleteFolder, | ||
} from "../controllers/folder.controller"; | ||
import { HttpError, HttpStatus } from "../utils/errors"; | ||
import { type folderType } from "../models/folder.model"; | ||
|
||
export const folderRouter = Router(); | ||
|
||
//Add an folder | ||
//Note that the user field (which is part of foldersType) in body is automatically populated by verifyToken middleware | ||
folderRouter.post( | ||
"/", | ||
async (req: Request<any, any, folderType>, res: Response) => { | ||
try { | ||
const folder = await createFolder(req.body); | ||
res.status(HttpStatus.OK).json(folder); | ||
} catch (err: unknown) { | ||
if (err instanceof HttpError) { | ||
res.status(err.errorCode).json({ error: err.message }); | ||
} else { | ||
res | ||
.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.json({ error: "An unknown error occurred" }); | ||
} | ||
} | ||
}, | ||
); | ||
|
||
//Get all folders | ||
folderRouter.get( | ||
"/", | ||
async (req: Request<any, any, folderType>, res: Response) => { | ||
try { | ||
const folder = await getAllFolders(req.body.user); | ||
res.status(HttpStatus.OK).json(folder); | ||
} catch (err: unknown) { | ||
if (err instanceof HttpError) { | ||
res.status(err.errorCode).json({ error: err.message }); | ||
} else { | ||
res | ||
.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.json({ error: "An unknown error occurred" }); | ||
} | ||
} | ||
}, | ||
); | ||
|
||
//Get a single folder by id | ||
folderRouter.get( | ||
"/:folderId", | ||
async (req: Request<any, any, folderType>, res: Response) => { | ||
try { | ||
const folder = await getFolderById( | ||
req.body.user, | ||
req.params.folderId, | ||
); | ||
res.status(HttpStatus.OK).json(folder); | ||
} catch (err: unknown) { | ||
if (err instanceof HttpError) { | ||
res.status(err.errorCode).json({ error: err.message }); | ||
} else { | ||
res | ||
.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.json({ error: "An unknown error occurred" }); | ||
} | ||
} | ||
}, | ||
); | ||
|
||
//Update an folder | ||
folderRouter.put( | ||
"/:folderId", | ||
async (req: Request<any, any, folderType>, res: Response) => { | ||
try { | ||
const folder = await updateFolder( | ||
req.body.user, | ||
req.params.folderId, | ||
req.body, | ||
); | ||
res.status(HttpStatus.OK).json(folder); | ||
} catch (err: unknown) { | ||
if (err instanceof HttpError) { | ||
res.status(err.errorCode).json({ error: err.message }); | ||
} else { | ||
res | ||
.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.json({ error: "An unknown error occurred" }); | ||
} | ||
} | ||
}, | ||
); | ||
|
||
//Delete an folder | ||
folderRouter.delete( | ||
"/:folderId", | ||
async (req: Request<any, any, folderType>, res: Response) => { | ||
try { | ||
const folder = await deleteFolder( | ||
req.body.user, | ||
req.params.folderId, | ||
); | ||
res.status(HttpStatus.OK).json(folder); | ||
} catch (err: unknown) { | ||
if (err instanceof HttpError) { | ||
res.status(err.errorCode).json({ error: err.message }); | ||
} else { | ||
res | ||
.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.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
Oops, something went wrong.