-
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.
Added routers, controllers and tests for the rest of the resume items
- Loading branch information
1 parent
aa800dc
commit 838ebbb
Showing
18 changed files
with
1,299 additions
and
11 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,142 @@ | ||
import { | ||
ExperienceModel, | ||
type ExperienceType, | ||
} from "../models/experience.model"; | ||
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors"; | ||
import { checkDuplicateItemName } from "../utils/checkDuplicates"; | ||
|
||
export const createExperience = async (experienceFields: ExperienceType) => { | ||
try { | ||
if (await checkDuplicateItemName(experienceFields.itemName)) { | ||
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); | ||
} | ||
|
||
const newExperience = new ExperienceModel(experienceFields); | ||
await newExperience.save(); | ||
return newExperience; | ||
} catch (err: unknown) { | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
|
||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Experience creation failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const getAllExperiences = async (user: string) => { | ||
try { | ||
const experience = await ExperienceModel.find({ user: user }); | ||
return experience; | ||
} 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, | ||
"Experience retrieval failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const getExperienceById = async (user: string, experienceId: string) => { | ||
try { | ||
const experience = await ExperienceModel.findOne({ | ||
user: user, | ||
_id: experienceId, | ||
}); | ||
return experience; | ||
} 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, | ||
"Experience retrieval failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const updateExperience = async ( | ||
user: string, | ||
experienceId: string, | ||
experienceFields: ExperienceType, | ||
) => { | ||
try { | ||
if (!experienceId) { | ||
throw new HttpError( | ||
HttpStatus.BAD_REQUEST, | ||
"Missing experience ID for update", | ||
); | ||
} | ||
|
||
if (await checkDuplicateItemName(experienceFields.itemName, experienceId)) { | ||
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); | ||
} | ||
|
||
const updatedExperience = await ExperienceModel.findOneAndUpdate( | ||
{ _id: experienceId, user: user }, // Query to match the document by _id and user | ||
{ $set: experienceFields }, // Update operation | ||
{ new: true, runValidators: true }, // Options: return the updated document and run schema validators | ||
); | ||
return updatedExperience; | ||
} 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, | ||
"Experience update failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const deleteExperience = async (user: string, experienceId: string) => { | ||
try { | ||
const deletedExperience = await ExperienceModel.findOneAndDelete({ | ||
_id: experienceId, | ||
user: user, | ||
}); | ||
if (!deletedExperience) { | ||
throw new HttpError( | ||
HttpStatus.NOT_FOUND, | ||
"Experience not found or already deleted", | ||
); | ||
} | ||
return { message: "Experience 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, | ||
"Experience 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { | ||
HeadingModel, | ||
type HeadingType, | ||
} from "../models/heading.model"; | ||
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors"; | ||
import { checkDuplicateItemName } from "../utils/checkDuplicates"; | ||
|
||
export const createHeading = async (headingFields: HeadingType) => { | ||
try { | ||
if (await checkDuplicateItemName(headingFields.itemName)) { | ||
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); | ||
} | ||
|
||
const newHeading = new HeadingModel(headingFields); | ||
await newHeading.save(); | ||
return newHeading; | ||
} catch (err: unknown) { | ||
if (err instanceof HttpError) { | ||
throw err; | ||
} | ||
|
||
checkMongooseErrors(err); | ||
|
||
throw new HttpError( | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Heading creation failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const getAllHeadings = async (user: string) => { | ||
try { | ||
const heading = await HeadingModel.find({ user: user }); | ||
return heading; | ||
} 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, | ||
"Heading retrieval failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const getHeadingById = async (user: string, headingId: string) => { | ||
try { | ||
const heading = await HeadingModel.findOne({ | ||
user: user, | ||
_id: headingId, | ||
}); | ||
return heading; | ||
} 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, | ||
"Heading retrieval failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const updateHeading = async ( | ||
user: string, | ||
headingId: string, | ||
headingFields: HeadingType, | ||
) => { | ||
try { | ||
if (!headingId) { | ||
throw new HttpError( | ||
HttpStatus.BAD_REQUEST, | ||
"Missing heading ID for update", | ||
); | ||
} | ||
|
||
if (await checkDuplicateItemName(headingFields.itemName, headingId)) { | ||
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); | ||
} | ||
|
||
const updatedHeading = await HeadingModel.findOneAndUpdate( | ||
{ _id: headingId, user: user }, // Query to match the document by _id and user | ||
{ $set: headingFields }, // Update operation | ||
{ new: true, runValidators: true }, // Options: return the updated document and run schema validators | ||
); | ||
return updatedHeading; | ||
} 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, | ||
"Heading update failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
||
export const deleteHeading = async (user: string, headingId: string) => { | ||
try { | ||
const deletedHeading = await HeadingModel.findOneAndDelete({ | ||
_id: headingId, | ||
user: user, | ||
}); | ||
if (!deletedHeading) { | ||
throw new HttpError( | ||
HttpStatus.NOT_FOUND, | ||
"Heading not found or already deleted", | ||
); | ||
} | ||
return { message: "Heading 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, | ||
"Heading deletion failed", | ||
{ cause: err }, | ||
); | ||
} | ||
}; | ||
|
Oops, something went wrong.