Skip to content

Commit

Permalink
implements education controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jackgarritano committed Feb 28, 2024
1 parent 6db55ac commit 52513e3
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/controllers/activities.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export const getAllActivities = async (user: string) => {

export const getActivityById = async (user: string, activityId: string) => {
try {
const activities = await ActivitiesModel.findOne({
const activity = await ActivitiesModel.findOne({
user: user,
_id: activityId,
});
return activities;
return activity;
} catch (err: unknown) {
//rethrow any errors as HttpErrors
if (err instanceof HttpError) {
Expand Down
138 changes: 138 additions & 0 deletions src/controllers/education.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { EducationModel, type EducationType } from "../models/education.model";
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors";
import { checkDuplicateItemName } from "../utils/checkDuplicates";

export const createEducation = async (educationFields: EducationType) => {
try {
if (await checkDuplicateItemName(educationFields.itemName)) {
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name");
}

const newEducation = new EducationModel(educationFields);
await newEducation.save();
return newEducation;
} catch (err: unknown) {
if (err instanceof HttpError) {
throw err;
}

checkMongooseErrors(err);

throw new HttpError(
HttpStatus.INTERNAL_SERVER_ERROR,
"Education creation failed",
{ cause: err },
);
}
};

export const getAllEducation = async (user: string) => {
try {
const education = await EducationModel.find({ user: user });
return education;
} 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,
"Education retrieval failed",
{ cause: err },
);
}
};

export const getActivityById = async (user: string, educationId: string) => {
try {
const education = await EducationModel.findOne({
user: user,
_id: educationId,
});
return education;
} 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,
"Education retrieval failed",
{ cause: err },
);
}
};

export const updateEducation = async (
user: string,
educationId: string,
educationFields: EducationType,
) => {
try {
if (!educationId) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Missing education ID for update",
);
}

if (await checkDuplicateItemName(educationFields.itemName)) {
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name");
}

const updatedActivity = await EducationModel.findOneAndUpdate(
{ _id: educationId, user: user }, // Query to match the document by _id and user
{ $set: educationFields }, // Update operation
{ new: true, runValidators: true }, // Options: return the updated document and run schema validators
);
return updatedActivity;
} 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,
"Education update failed",
{ cause: err },
);
}
};

export const deleteEducation = async (user: string, educationId: string) => {
try {
const deletedEducation = await EducationModel.findOneAndDelete({
_id: educationId,
user: user,
});
if (!deletedEducation) {
throw new HttpError(
HttpStatus.NOT_FOUND,
"Education not found or already deleted",
);
}
return { message: "Education 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,
"Education deletion failed",
{ cause: err },
);
}
};

0 comments on commit 52513e3

Please sign in to comment.