From 9156318f2b4614ea6ec4df204f3ef1fd72e173e7 Mon Sep 17 00:00:00 2001 From: Jerry Cheng Date: Tue, 8 Oct 2024 23:30:01 -0400 Subject: [PATCH 1/2] changed activity to activity type --- ...alidators.ts => activityTypeValidators.ts} | 2 +- ....rename-activity-table-to-activity-type.ts | 54 ++++++++ backend/typescript/models/activity.model.ts | 57 ++++++++- .../typescript/models/activityType.model.ts | 7 + backend/typescript/models/userPetActivity.ts | 57 --------- backend/typescript/rest/activityRoutes.ts | 106 --------------- backend/typescript/rest/activityTypeRoutes.ts | 114 +++++++++++++++++ backend/typescript/server.ts | 4 +- .../implementations/activityService.ts | 119 ----------------- .../implementations/activityTypeService.ts | 121 ++++++++++++++++++ .../services/interfaces/activityService.ts | 54 -------- .../interfaces/activityTypeService.ts | 55 ++++++++ 12 files changed, 407 insertions(+), 343 deletions(-) rename backend/typescript/middlewares/validators/{activityValidators.ts => activityTypeValidators.ts} (90%) create mode 100644 backend/typescript/migrations/2024.10.09T02.40.17.rename-activity-table-to-activity-type.ts create mode 100644 backend/typescript/models/activityType.model.ts delete mode 100644 backend/typescript/models/userPetActivity.ts delete mode 100644 backend/typescript/rest/activityRoutes.ts create mode 100644 backend/typescript/rest/activityTypeRoutes.ts delete mode 100644 backend/typescript/services/implementations/activityService.ts create mode 100644 backend/typescript/services/implementations/activityTypeService.ts delete mode 100644 backend/typescript/services/interfaces/activityService.ts create mode 100644 backend/typescript/services/interfaces/activityTypeService.ts diff --git a/backend/typescript/middlewares/validators/activityValidators.ts b/backend/typescript/middlewares/validators/activityTypeValidators.ts similarity index 90% rename from backend/typescript/middlewares/validators/activityValidators.ts rename to backend/typescript/middlewares/validators/activityTypeValidators.ts index e06b7f9..d1b5e11 100644 --- a/backend/typescript/middlewares/validators/activityValidators.ts +++ b/backend/typescript/middlewares/validators/activityTypeValidators.ts @@ -3,7 +3,7 @@ import { getApiValidationError, validatePrimitive } from "./util"; /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ /* eslint-disable-next-line import/prefer-default-export */ -export const activityRequestDtoValidator = async ( +export const activityTypeRequestDtoValidator = async ( req: Request, res: Response, next: NextFunction, diff --git a/backend/typescript/migrations/2024.10.09T02.40.17.rename-activity-table-to-activity-type.ts b/backend/typescript/migrations/2024.10.09T02.40.17.rename-activity-table-to-activity-type.ts new file mode 100644 index 0000000..1f5632e --- /dev/null +++ b/backend/typescript/migrations/2024.10.09T02.40.17.rename-activity-table-to-activity-type.ts @@ -0,0 +1,54 @@ +import { DataType } from "sequelize-typescript"; +import { Migration } from "../umzug"; + +const OLD_TABLE_NAME = "activities"; +const NEW_TABLE_NAME = "activity_types"; +const USER_PET_ACTIVITIES_TABLE = "user_pet_activities"; + +export const up: Migration = async ({ context: sequelize }) => { + // Rename the activities table to activity_types + await sequelize + .getQueryInterface() + .renameTable(OLD_TABLE_NAME, NEW_TABLE_NAME); + + // Change the activity_id column in user_pet_activities to activity_type_id + await sequelize + .getQueryInterface() + .renameColumn(USER_PET_ACTIVITIES_TABLE, "activity_id", "activity_type_id"); + + // Update the references for activity_type_id to point to the new activity_types table + await sequelize + .getQueryInterface() + .changeColumn(USER_PET_ACTIVITIES_TABLE, "activity_type_id", { + type: DataType.INTEGER, + allowNull: false, + references: { + model: NEW_TABLE_NAME, // Reference the new table name + key: "id", + }, + }); +}; + +export const down: Migration = async ({ context: sequelize }) => { + // Rename the activity_types table back to activities + await sequelize + .getQueryInterface() + .renameTable(NEW_TABLE_NAME, OLD_TABLE_NAME); + + // Revert the activity_type_id column back to activity_id + await sequelize + .getQueryInterface() + .renameColumn(USER_PET_ACTIVITIES_TABLE, "activity_type_id", "activity_id"); + + // Revert the activity_id column to reference the old activities table + await sequelize + .getQueryInterface() + .changeColumn(USER_PET_ACTIVITIES_TABLE, "activity_id", { + type: DataType.INTEGER, + allowNull: false, + references: { + model: OLD_TABLE_NAME, // Revert back to the old table name + key: "id", + }, + }); +}; diff --git a/backend/typescript/models/activity.model.ts b/backend/typescript/models/activity.model.ts index 3db7cc9..bd7058e 100644 --- a/backend/typescript/models/activity.model.ts +++ b/backend/typescript/models/activity.model.ts @@ -1,7 +1,56 @@ -import { Column, Model, Table } from "sequelize-typescript"; +import { + Column, + Model, + Table, + DataType, + ForeignKey, + BelongsTo, +} from "sequelize-typescript"; +import User from "./user.model"; +import Pet from "./pet.model"; +import ActivityType from "./activityType.model"; -@Table({ timestamps: false, tableName: "activities" }) +@Table({ timestamps: false, tableName: "user_pet_activities" }) export default class Activity extends Model { - @Column - activity_name!: string; + @Column({}) + user_pet_activity_id!: number; + + @ForeignKey(() => User) // in case of null, task has not been assigned + @Column({}) + user_id?: number; + + @BelongsTo(() => User) + user?: User; + + @ForeignKey(() => Pet) + @Column({}) + pet_id!: number; + + @BelongsTo(() => Pet) + pet!: Pet; + + @ForeignKey(() => ActivityType) + @Column({}) + activity_type_id!: number; + + @BelongsTo(() => ActivityType) + activity_type!: ActivityType; + + @Column({}) + scheduled_start_time?: Date; + + @Column({}) + start_time?: Date; + + @Column({}) + end_time?: Date; + + @Column({ type: DataType.TEXT }) + notes?: string; + + @Column({}) + created_at!: Date; + + @Column({}) + updated_at?: Date; } diff --git a/backend/typescript/models/activityType.model.ts b/backend/typescript/models/activityType.model.ts new file mode 100644 index 0000000..68a6d01 --- /dev/null +++ b/backend/typescript/models/activityType.model.ts @@ -0,0 +1,7 @@ +import { Column, Model, Table } from "sequelize-typescript"; + +@Table({ timestamps: false, tableName: "activity_types" }) +export default class ActivityType extends Model { + @Column + activity_name!: string; +} diff --git a/backend/typescript/models/userPetActivity.ts b/backend/typescript/models/userPetActivity.ts deleted file mode 100644 index a50b3e4..0000000 --- a/backend/typescript/models/userPetActivity.ts +++ /dev/null @@ -1,57 +0,0 @@ -// not started -import { - Column, - Model, - Table, - DataType, - ForeignKey, - BelongsTo, -} from "sequelize-typescript"; -import User from "./user.model"; -import Pet from "./pet.model"; -import Activity from "./activity.model"; - -@Table({ timestamps: false, tableName: "user_pet_activities" }) -export default class UserPetActivity extends Model { - @Column({}) - user_pet_activity_id!: number; - - @ForeignKey(() => User) // in case of null, task has not been assigned - @Column({}) - user_id?: number; - - @BelongsTo(() => User) - user?: User; - - @ForeignKey(() => Pet) - @Column({}) - pet_id!: number; - - @BelongsTo(() => Pet) - pet!: Pet; - - @ForeignKey(() => Activity) - @Column({}) - activity_id!: number; - - @BelongsTo(() => Activity) - activity!: Activity; - - @Column({}) - scheduled_start_time?: Date; - - @Column({}) - start_time?: Date; - - @Column({}) - end_time?: Date; - - @Column({ type: DataType.TEXT }) - notes?: string; - - @Column({}) - created_at!: Date; - - @Column({}) - updated_at?: Date; -} diff --git a/backend/typescript/rest/activityRoutes.ts b/backend/typescript/rest/activityRoutes.ts deleted file mode 100644 index 6227e0f..0000000 --- a/backend/typescript/rest/activityRoutes.ts +++ /dev/null @@ -1,106 +0,0 @@ -import { Router } from "express"; -import { activityRequestDtoValidator } from "../middlewares/validators/activityValidators"; -import ActivityService from "../services/implementations/activityService"; -import { - ActivityResponseDTO, - IActivityService, -} from "../services/interfaces/activityService"; -import { - getErrorMessage, - NotFoundError, - INTERNAL_SERVER_ERROR_MESSAGE, -} from "../utilities/errorUtils"; -import { sendResponseByMimeType } from "../utilities/responseUtil"; - -const activityRouter: Router = Router(); - -const activityService: IActivityService = new ActivityService(); - -/* Create Activity */ -activityRouter.post("/", activityRequestDtoValidator, async (req, res) => { - try { - const { body } = req; - const newActivity = await activityService.createActivity({ - activityName: body.activityName, - }); - res.status(201).json(newActivity); - } catch (e: unknown) { - if (e instanceof NotFoundError) { - res.status(404).send(getErrorMessage(e)); - } else { - res.status(500).send(INTERNAL_SERVER_ERROR_MESSAGE); - } - } -}); - -/* Get all Activities */ -activityRouter.get("/", async (req, res) => { - const contentType = req.headers["content-type"]; - try { - const activities = await activityService.getActivities(); - await sendResponseByMimeType( - res, - 200, - contentType, - activities, - ); - } catch (e: unknown) { - await sendResponseByMimeType(res, 500, contentType, [ - { - error: INTERNAL_SERVER_ERROR_MESSAGE, - }, - ]); - } -}); - -/* Get Activity by id */ -activityRouter.get("/:id", async (req, res) => { - const { id } = req.params; - - try { - const activity = await activityService.getActivity(id); - res.status(200).json(activity); - } catch (e: unknown) { - if (e instanceof NotFoundError) { - res.status(404).send(getErrorMessage(e)); - } else { - res.status(500).send(INTERNAL_SERVER_ERROR_MESSAGE); - } - } -}); - -/* Update Activity by id */ -activityRouter.put("/:id", activityRequestDtoValidator, async (req, res) => { - const { id } = req.params; - try { - const { body } = req; - const activity = await activityService.updateActivity(id, { - activityName: body.activityName, - }); - res.status(200).json(activity); - } catch (e: unknown) { - if (e instanceof NotFoundError) { - res.status(404).send(getErrorMessage(e)); - } else { - res.status(500).send(INTERNAL_SERVER_ERROR_MESSAGE); - } - } -}); - -/* Delete Activity by id */ -activityRouter.delete("/:id", async (req, res) => { - const { id } = req.params; - - try { - const deletedId = await activityService.deleteActivity(id); - res.status(200).json({ id: deletedId }); - } catch (e: unknown) { - if (e instanceof NotFoundError) { - res.status(404).send(getErrorMessage(e)); - } else { - res.status(500).send(INTERNAL_SERVER_ERROR_MESSAGE); - } - } -}); - -export default activityRouter; diff --git a/backend/typescript/rest/activityTypeRoutes.ts b/backend/typescript/rest/activityTypeRoutes.ts new file mode 100644 index 0000000..7e37bb9 --- /dev/null +++ b/backend/typescript/rest/activityTypeRoutes.ts @@ -0,0 +1,114 @@ +import { Router } from "express"; +import { activityTypeRequestDtoValidator } from "../middlewares/validators/activityTypeValidators"; +import ActivityTypeService from "../services/implementations/activityTypeService"; +import { + ActivityTypeResponseDTO, + IActivityTypeService, +} from "../services/interfaces/activityTypeService"; +import { + getErrorMessage, + NotFoundError, + INTERNAL_SERVER_ERROR_MESSAGE, +} from "../utilities/errorUtils"; +import { sendResponseByMimeType } from "../utilities/responseUtil"; + +const activityTypeRouter: Router = Router(); + +const activityTypeService: IActivityTypeService = new ActivityTypeService(); + +/* Create ActivityType */ +activityTypeRouter.post( + "/", + activityTypeRequestDtoValidator, + async (req, res) => { + try { + const { body } = req; + const newActivityType = await activityTypeService.createActivityType({ + activityName: body.activityName, + }); + res.status(201).json(newActivityType); + } catch (e: unknown) { + if (e instanceof NotFoundError) { + res.status(404).send(getErrorMessage(e)); + } else { + res.status(500).send(INTERNAL_SERVER_ERROR_MESSAGE); + } + } + }, +); + +/* Get all ActivityTypes */ +activityTypeRouter.get("/", async (req, res) => { + const contentType = req.headers["content-type"]; + try { + const activityTypes = await activityTypeService.getActivityTypes(); + await sendResponseByMimeType( + res, + 200, + contentType, + activityTypes, + ); + } catch (e: unknown) { + await sendResponseByMimeType(res, 500, contentType, [ + { + error: INTERNAL_SERVER_ERROR_MESSAGE, + }, + ]); + } +}); + +/* Get ActivityType by id */ +activityTypeRouter.get("/:id", async (req, res) => { + const { id } = req.params; + + try { + const activityType = await activityTypeService.getActivityType(id); + res.status(200).json(activityType); + } catch (e: unknown) { + if (e instanceof NotFoundError) { + res.status(404).send(getErrorMessage(e)); + } else { + res.status(500).send(INTERNAL_SERVER_ERROR_MESSAGE); + } + } +}); + +/* Update ActivityType by id */ +activityTypeRouter.put( + "/:id", + activityTypeRequestDtoValidator, + async (req, res) => { + const { id } = req.params; + try { + const { body } = req; + const activityType = await activityTypeService.updateActivityType(id, { + activityName: body.activityName, + }); + res.status(200).json(activityType); + } catch (e: unknown) { + if (e instanceof NotFoundError) { + res.status(404).send(getErrorMessage(e)); + } else { + res.status(500).send(INTERNAL_SERVER_ERROR_MESSAGE); + } + } + }, +); + +/* Delete ActivityType by id */ +activityTypeRouter.delete("/:id", async (req, res) => { + const { id } = req.params; + + try { + const deletedId = await activityTypeService.deleteActivityType(id); + res.status(200).json({ id: deletedId }); + } catch (e: unknown) { + if (e instanceof NotFoundError) { + res.status(404).send(getErrorMessage(e)); + } else { + res.status(500).send(INTERNAL_SERVER_ERROR_MESSAGE); + } + } +}); + +export default activityTypeRouter; diff --git a/backend/typescript/server.ts b/backend/typescript/server.ts index 4baf25e..640c84d 100644 --- a/backend/typescript/server.ts +++ b/backend/typescript/server.ts @@ -7,7 +7,7 @@ import YAML from "yamljs"; import { sequelize } from "./models"; import authRouter from "./rest/authRoutes"; -import activityRouter from "./rest/activityRoutes"; +import activityTypeRouter from "./rest/activityTypeRoutes"; import behaviourRouter from "./rest/behaviourRoutes"; import animalTypeRouter from "./rest/animalTypeRoutes"; import entityRouter from "./rest/entityRoutes"; @@ -36,7 +36,7 @@ app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use("/auth", authRouter); -app.use("/activities", activityRouter); +app.use("/activity-types", activityTypeRouter); app.use("/animal-types", animalTypeRouter); app.use("/behaviours", behaviourRouter); app.use("/entities", entityRouter); diff --git a/backend/typescript/services/implementations/activityService.ts b/backend/typescript/services/implementations/activityService.ts deleted file mode 100644 index e6be92d..0000000 --- a/backend/typescript/services/implementations/activityService.ts +++ /dev/null @@ -1,119 +0,0 @@ -import PgActivity from "../../models/activity.model"; -import { - IActivityService, - ActivityRequestDTO, - ActivityResponseDTO, -} from "../interfaces/activityService"; -import { getErrorMessage, NotFoundError } from "../../utilities/errorUtils"; -import logger from "../../utilities/logger"; - -const Logger = logger(__filename); - -class ActivityService implements IActivityService { - /* eslint-disable class-methods-use-this */ - async getActivity(id: string): Promise { - let activity: PgActivity | null; - try { - activity = await PgActivity.findByPk(id, { raw: true }); - if (!activity) { - throw new NotFoundError(`Activity id ${id} not found`); - } - } catch (error: unknown) { - Logger.error( - `Failed to get activity. Reason = ${getErrorMessage(error)}`, - ); - throw error; - } - - return { - id: activity.id, - activityName: activity.activity_name, - }; - } - - async getActivities(): Promise { - try { - const activities: Array = await PgActivity.findAll({ - raw: true, - }); - return activities.map((activity) => ({ - id: activity.id, - activityName: activity.activity_name, - })); - } catch (error: unknown) { - Logger.error( - `Failed to get activities. Reason = ${getErrorMessage(error)}`, - ); - throw error; - } - } - - async createActivity( - activity: ActivityRequestDTO, - ): Promise { - let newActivity: PgActivity | null; - try { - newActivity = await PgActivity.create({ - activity_name: activity.activityName, - }); - } catch (error: unknown) { - Logger.error( - `Failed to create activity. Reason = ${getErrorMessage(error)}`, - ); - throw error; - } - return { - id: newActivity.id, - activityName: newActivity.activity_name, - }; - } - - async updateActivity( - id: string, - activity: ActivityRequestDTO, - ): Promise { - let resultingActivity: PgActivity | null; - let updateResult: [number, PgActivity[]] | null; - try { - updateResult = await PgActivity.update( - { - activity_name: activity.activityName, - }, - { where: { id }, returning: true }, - ); - - if (!updateResult[0]) { - throw new NotFoundError(`Activity id ${id} not found`); - } - [, [resultingActivity]] = updateResult; - } catch (error: unknown) { - Logger.error( - `Failed to update activity. Reason = ${getErrorMessage(error)}`, - ); - throw error; - } - return { - id: resultingActivity.id, - activityName: resultingActivity?.activity_name, - }; - } - - async deleteActivity(id: string): Promise { - try { - const deleteResult: number | null = await PgActivity.destroy({ - where: { id }, - }); - if (!deleteResult) { - throw new NotFoundError(`Activity id ${id} not found`); - } - return id; - } catch (error: unknown) { - Logger.error( - `Failed to delete activity. Reason = ${getErrorMessage(error)}`, - ); - throw error; - } - } -} - -export default ActivityService; diff --git a/backend/typescript/services/implementations/activityTypeService.ts b/backend/typescript/services/implementations/activityTypeService.ts new file mode 100644 index 0000000..8b93383 --- /dev/null +++ b/backend/typescript/services/implementations/activityTypeService.ts @@ -0,0 +1,121 @@ +import PgActivityType from "../../models/activityType.model"; +import { + IActivityTypeService, + ActivityTypeRequestDTO, + ActivityTypeResponseDTO, +} from "../interfaces/activityTypeService"; +import { getErrorMessage, NotFoundError } from "../../utilities/errorUtils"; +import logger from "../../utilities/logger"; + +const Logger = logger(__filename); + +class ActivityTypeService implements IActivityTypeService { + /* eslint-disable class-methods-use-this */ + async getActivityType(id: string): Promise { + let activityType: PgActivityType | null; + try { + activityType = await PgActivityType.findByPk(id, { raw: true }); + if (!activityType) { + throw new NotFoundError(`ActivityType id ${id} not found`); + } + } catch (error: unknown) { + Logger.error( + `Failed to get activity type. Reason = ${getErrorMessage(error)}`, + ); + throw error; + } + + return { + id: activityType.id, + activityName: activityType.activity_name, + }; + } + + async getActivityTypes(): Promise { + try { + const activityTypes: Array = await PgActivityType.findAll( + { + raw: true, + }, + ); + return activityTypes.map((activityType) => ({ + id: activityType.id, + activityName: activityType.activity_name, + })); + } catch (error: unknown) { + Logger.error( + `Failed to get activity types. Reason = ${getErrorMessage(error)}`, + ); + throw error; + } + } + + async createActivityType( + activityType: ActivityTypeRequestDTO, + ): Promise { + let newActivityType: PgActivityType | null; + try { + newActivityType = await PgActivityType.create({ + activity_name: activityType.activityName, + }); + } catch (error: unknown) { + Logger.error( + `Failed to create activity type. Reason = ${getErrorMessage(error)}`, + ); + throw error; + } + return { + id: newActivityType.id, + activityName: newActivityType.activity_name, + }; + } + + async updateActivityType( + id: string, + activityType: ActivityTypeRequestDTO, + ): Promise { + let resultingActivityType: PgActivityType | null; + let updateResult: [number, PgActivityType[]] | null; + try { + updateResult = await PgActivityType.update( + { + activity_name: activityType.activityName, + }, + { where: { id }, returning: true }, + ); + + if (!updateResult[0]) { + throw new NotFoundError(`ActivityType id ${id} not found`); + } + [, [resultingActivityType]] = updateResult; + } catch (error: unknown) { + Logger.error( + `Failed to update activity type. Reason = ${getErrorMessage(error)}`, + ); + throw error; + } + return { + id: resultingActivityType.id, + activityName: resultingActivityType?.activity_name, + }; + } + + async deleteActivityType(id: string): Promise { + try { + const deleteResult: number | null = await PgActivityType.destroy({ + where: { id }, + }); + if (!deleteResult) { + throw new NotFoundError(`ActivityType id ${id} not found`); + } + return id; + } catch (error: unknown) { + Logger.error( + `Failed to delete activity type. Reason = ${getErrorMessage(error)}`, + ); + throw error; + } + } +} + +export default ActivityTypeService; diff --git a/backend/typescript/services/interfaces/activityService.ts b/backend/typescript/services/interfaces/activityService.ts deleted file mode 100644 index 6a38bf7..0000000 --- a/backend/typescript/services/interfaces/activityService.ts +++ /dev/null @@ -1,54 +0,0 @@ -export interface ActivityRequestDTO { - activityName: string; -} - -export interface ActivityResponseDTO { - id: number; - activityName: string; -} - -export interface IActivityService { - /** - * retrieve the Activity with the given id - * @param id Activity id - * @returns requested Activity - * @throws Error if retrieval fails - */ - getActivity(id: string): Promise; - - /** - * retrieve all Activities - * @param - * @returns returns array of Activities - * @throws Error if retrieval fails - */ - getActivities(): Promise; - - /** - * create a Activity with the fields given in the DTO, return created Activity - * @param activity new Activity - * @returns the created Activity - * @throws Error if creation fails - */ - createActivity(activity: ActivityRequestDTO): Promise; - - /** - * update the Activity with the given id with fields in the DTO, return updated Activity - * @param id Activity id - * @param activity Updated Activity - * @returns the updated Activity - * @throws Error if update fails - */ - updateActivity( - id: string, - activity: ActivityRequestDTO, - ): Promise; - - /** - * delete the Activity with the given id - * @param id Activity id - * @returns id of the Activity deleted - * @throws Error if deletion fails - */ - deleteActivity(id: string): Promise; -} diff --git a/backend/typescript/services/interfaces/activityTypeService.ts b/backend/typescript/services/interfaces/activityTypeService.ts new file mode 100644 index 0000000..6ed667a --- /dev/null +++ b/backend/typescript/services/interfaces/activityTypeService.ts @@ -0,0 +1,55 @@ +export interface ActivityTypeRequestDTO { + activityName: string; +} + +export interface ActivityTypeResponseDTO { + id: number; + activityName: string; +} + +export interface IActivityTypeService { + /** + * Retrieve the ActivityType with the given id. + * @param id ActivityType id + * @returns Requested ActivityType + * @throws Error if retrieval fails + */ + getActivityType(id: string): Promise; + + /** + * Retrieve all Activity Types. + * @returns Returns an array of ActivityTypes + * @throws Error if retrieval fails + */ + getActivityTypes(): Promise; + + /** + * Create an ActivityType with the fields given in the DTO, return created ActivityType + * @param activity New ActivityType + * @returns The created ActivityType + * @throws Error if creation fails + */ + createActivityType( + activity: ActivityTypeRequestDTO, + ): Promise; + + /** + * Update the ActivityType with the given id with fields in the DTO, return updated ActivityType + * @param id ActivityType id + * @param activity Updated ActivityType + * @returns The updated ActivityType + * @throws Error if update fails + */ + updateActivityType( + id: string, + activity: ActivityTypeRequestDTO, + ): Promise; + + /** + * Delete the ActivityType with the given id + * @param id ActivityType id + * @returns id of the ActivityType deleted + * @throws Error if deletion fails + */ + deleteActivityType(id: string): Promise; +} From 93e8c9985519a275ea93d22f076cecdc3a0627e2 Mon Sep 17 00:00:00 2001 From: Jerry Cheng Date: Wed, 9 Oct 2024 22:33:16 -0400 Subject: [PATCH 2/2] changed user_pet_activity to activity --- ...02.40.17.rename-activity-table-to-activity-type.ts | 11 +++++++++++ backend/typescript/models/activity.model.ts | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/backend/typescript/migrations/2024.10.09T02.40.17.rename-activity-table-to-activity-type.ts b/backend/typescript/migrations/2024.10.09T02.40.17.rename-activity-table-to-activity-type.ts index 1f5632e..bbd58a1 100644 --- a/backend/typescript/migrations/2024.10.09T02.40.17.rename-activity-table-to-activity-type.ts +++ b/backend/typescript/migrations/2024.10.09T02.40.17.rename-activity-table-to-activity-type.ts @@ -4,6 +4,7 @@ import { Migration } from "../umzug"; const OLD_TABLE_NAME = "activities"; const NEW_TABLE_NAME = "activity_types"; const USER_PET_ACTIVITIES_TABLE = "user_pet_activities"; +const ACTIVITIES_TABLE = "activities"; export const up: Migration = async ({ context: sequelize }) => { // Rename the activities table to activity_types @@ -27,9 +28,19 @@ export const up: Migration = async ({ context: sequelize }) => { key: "id", }, }); + + // Change the name of user_pet_activities to activities + await sequelize + .getQueryInterface() + .renameTable(USER_PET_ACTIVITIES_TABLE, ACTIVITIES_TABLE); }; export const down: Migration = async ({ context: sequelize }) => { + // Rename the activities table back to user_pet_activities + await sequelize + .getQueryInterface() + .renameTable(ACTIVITIES_TABLE, USER_PET_ACTIVITIES_TABLE); + // Rename the activity_types table back to activities await sequelize .getQueryInterface() diff --git a/backend/typescript/models/activity.model.ts b/backend/typescript/models/activity.model.ts index bd7058e..56926bf 100644 --- a/backend/typescript/models/activity.model.ts +++ b/backend/typescript/models/activity.model.ts @@ -10,10 +10,10 @@ import User from "./user.model"; import Pet from "./pet.model"; import ActivityType from "./activityType.model"; -@Table({ timestamps: false, tableName: "user_pet_activities" }) +@Table({ timestamps: false, tableName: "activities" }) export default class Activity extends Model { @Column({}) - user_pet_activity_id!: number; + activity_id!: number; @ForeignKey(() => User) // in case of null, task has not been assigned @Column({})