Skip to content

Commit

Permalink
Added routers, controllers and tests for the rest of the resume items
Browse files Browse the repository at this point in the history
  • Loading branch information
seancrich11 committed Mar 1, 2024
1 parent aa800dc commit 838ebbb
Show file tree
Hide file tree
Showing 18 changed files with 1,299 additions and 11 deletions.
142 changes: 142 additions & 0 deletions src/controllers/experience.controller.ts
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 },
);
}
};

142 changes: 142 additions & 0 deletions src/controllers/heading.controller.ts
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 },
);
}
};

Loading

0 comments on commit 838ebbb

Please sign in to comment.