From 323364a1d3d80891ba11414f10502370ecae9dcf Mon Sep 17 00:00:00 2001 From: Selena Zhang Date: Wed, 1 Nov 2023 21:54:41 -0400 Subject: [PATCH] feat: create impl file, add prisma client --- .../graphql/resolvers/residentResolvers.ts | 1 + backend/typescript/prisma/schema.prisma | 2 +- .../implementations/residentService.ts | 68 +++++++++++++++++++ .../services/interfaces/residentService.ts | 54 +++++++-------- package-lock.json | 66 ++++++++++++++++++ package.json | 6 ++ 6 files changed, 169 insertions(+), 28 deletions(-) create mode 100644 package-lock.json create mode 100644 package.json diff --git a/backend/typescript/graphql/resolvers/residentResolvers.ts b/backend/typescript/graphql/resolvers/residentResolvers.ts index 343ba44a..1ea46c63 100644 --- a/backend/typescript/graphql/resolvers/residentResolvers.ts +++ b/backend/typescript/graphql/resolvers/residentResolvers.ts @@ -1,3 +1,4 @@ +import ResidentService from "../../services/implementations/residentService"; import type IResidentService from "../../services/interfaces/residentService"; import type { ResidentDTO, CreateResidentDTO, UpdateResidentDTO } from "../../services/interfaces/residentService"; diff --git a/backend/typescript/prisma/schema.prisma b/backend/typescript/prisma/schema.prisma index abfb9d6b..73164e45 100644 --- a/backend/typescript/prisma/schema.prisma +++ b/backend/typescript/prisma/schema.prisma @@ -133,7 +133,7 @@ model notification { model notification_user { notification notification @relation(fields: [notification_id], references: [id]) - notification_id Int + notification_id Int @id @default(autoincrement()) recipient resident @relation(fields: [recipient_id], references: [id]) recipient_id Int seen Boolean @default(false) diff --git a/backend/typescript/services/implementations/residentService.ts b/backend/typescript/services/implementations/residentService.ts index e69de29b..54d376f3 100644 --- a/backend/typescript/services/implementations/residentService.ts +++ b/backend/typescript/services/implementations/residentService.ts @@ -0,0 +1,68 @@ +import { PrismaClient } from '@prisma/client'; +import type IResidentService from "../interfaces/residentService"; +import type { ResidentDTO, CreateResidentDTO, UpdateResidentDTO } from "../../services/interfaces/residentService"; +import logger from "../../utilities/logger"; + +const Prisma = new PrismaClient(); +//TODO: do logging +//const Logger = logger(__filename); + +class ResidentService implements IResidentService { + async add_resident(residentInfo: CreateResidentDTO): Promise { + try { + let newResident = await Prisma.resident.create({ + data: residentInfo, + }); + return newResident; + } catch (error: unknown) { + //log it + throw error; + } + } + async update_resident(residentId: number, residentInfo: UpdateResidentDTO): Promise { + try { + let updatedResident = await Prisma.resident.update({ + where: {id: residentId}, + data: residentInfo, + }); + return updatedResident; + } catch (error: unknown) { + //log it + throw error; + } + } + async delete_resident(residentId: number): Promise { + try { + let deletedResident = await Prisma.resident.delete({ + where: {id: residentId} + }); + return deletedResident; + } catch (error: unknown) { + //log it + throw error; + } + } + async get_all_residents(): Promise { + try { + let allResidents = await Prisma.resident.findMany(); + return allResidents; + } catch (error: unknown) { + //log it + throw error; + } + } + async get_residents_by_id(residentId: number[]): Promise { + try { + let allResidentsById = await Prisma.resident.findMany({ + where: {id: {in: residentId}} + }); + return allResidentsById; + } catch (error: unknown) { + //log it + throw error; + } + } + +} + +export default ResidentService; \ No newline at end of file diff --git a/backend/typescript/services/interfaces/residentService.ts b/backend/typescript/services/interfaces/residentService.ts index 0ce81da2..f223ad3d 100644 --- a/backend/typescript/services/interfaces/residentService.ts +++ b/backend/typescript/services/interfaces/residentService.ts @@ -1,45 +1,44 @@ export interface ResidentDTO { id: number; - firstName: string; - lastName: string; + first_name: string; + last_name: string; email: string; - phoneNumber: string; - displayName: string; - profilePictureLink: string; - birthdate: Date; + phone_number?: string | null; + display_name: string; + profile_picture_link?: string | null; + birthdate?: Date | null; credits: number; - dateJoined: Date; - dateLeft: Date; - // tasks: [Task] - // warnings: [Warning] + date_joined: Date; + date_left?: Date | null; } export interface CreateResidentDTO { - firstName: string; - lastName: string; + first_name: string; + last_name: string; email: string; - phoneNumber: string; - displayName: string; - profilePictureLink: string; - birthdate: Date; + phone_number?: string | null; + display_name: string; + profile_picture_link?: string | null; + birthdate?: Date | null; credits: number; - dateJoined: Date; + date_joined: Date; } export interface UpdateResidentDTO { - firstName: string; - lastName: string; + first_name: string; + last_name: string; email: string; - phoneNumber: string; - displayName: string; - profilePictureLink: string; - birthdate: Date; + phone_number?: string | null; + display_name: string; + profile_picture_link?: string | null; + birthdate?: Date | null; credits: number; - dateJoined: Date; - dateLeft: Date; + date_joined: Date; + date_left?: Date | null; } -interface IResidenceService { +interface IResidentService { + /** * Adds a resident * @param residentInfo: a CreateResidentDTO with the new resident's information @@ -62,7 +61,7 @@ interface IResidenceService { * @returns: a ResidentDTO with deleted resident's info * @throws Error if resident deletion fails */ - delete_resident(id: number): Promise; + delete_resident(residentId: number): Promise; /** * Gets all residents @@ -80,3 +79,4 @@ interface IResidenceService { get_residents_by_id(residentId: number[]): Promise>; } +export default IResidentService; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..2b17682d --- /dev/null +++ b/package-lock.json @@ -0,0 +1,66 @@ +{ + "name": "marillac-place", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "@prisma/client": "^5.5.2", + "graphql-tag": "^2.12.6" + } + }, + "node_modules/@prisma/client": { + "version": "5.5.2", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.5.2.tgz", + "integrity": "sha512-54XkqR8M+fxbzYqe+bIXimYnkkcGqgOh0dn0yWtIk6CQT4IUCAvNFNcQZwk2KqaLU+/1PHTSWrcHtx4XjluR5w==", + "hasInstallScript": true, + "dependencies": { + "@prisma/engines-version": "5.5.1-1.aebc046ce8b88ebbcb45efe31cbe7d06fd6abc0a" + }, + "engines": { + "node": ">=16.13" + }, + "peerDependencies": { + "prisma": "*" + }, + "peerDependenciesMeta": { + "prisma": { + "optional": true + } + } + }, + "node_modules/@prisma/engines-version": { + "version": "5.5.1-1.aebc046ce8b88ebbcb45efe31cbe7d06fd6abc0a", + "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.5.1-1.aebc046ce8b88ebbcb45efe31cbe7d06fd6abc0a.tgz", + "integrity": "sha512-O+qHFnZvAyOFk1tUco2/VdiqS0ym42a3+6CYLScllmnpbyiTplgyLt2rK/B9BTjYkSHjrgMhkG47S0oqzdIckA==" + }, + "node_modules/graphql": { + "version": "16.8.1", + "resolved": "https://registry.npmjs.org/graphql/-/graphql-16.8.1.tgz", + "integrity": "sha512-59LZHPdGZVh695Ud9lRzPBVTtlX9ZCV150Er2W43ro37wVof0ctenSaskPPjN7lVTIN8mSZt8PHUNKZuNQUuxw==", + "peer": true, + "engines": { + "node": "^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0" + } + }, + "node_modules/graphql-tag": { + "version": "2.12.6", + "resolved": "https://registry.npmjs.org/graphql-tag/-/graphql-tag-2.12.6.tgz", + "integrity": "sha512-FdSNcu2QQcWnM2VNvSCCDCVS5PpPqpzgFT8+GXzqJuoDd0CBncxCY278u4mhRO7tMgo2JjgJA5aZ+nWSQ/Z+xg==", + "dependencies": { + "tslib": "^2.1.0" + }, + "engines": { + "node": ">=10" + }, + "peerDependencies": { + "graphql": "^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0" + } + }, + "node_modules/tslib": { + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz", + "integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 00000000..29f34634 --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "@prisma/client": "^5.5.2", + "graphql-tag": "^2.12.6" + } +}