Skip to content

Commit

Permalink
feat: create impl file, add prisma client
Browse files Browse the repository at this point in the history
  • Loading branch information
selena1108 committed Nov 2, 2023
1 parent 15a1ab9 commit 323364a
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 28 deletions.
1 change: 1 addition & 0 deletions backend/typescript/graphql/resolvers/residentResolvers.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
2 changes: 1 addition & 1 deletion backend/typescript/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
68 changes: 68 additions & 0 deletions backend/typescript/services/implementations/residentService.ts
Original file line number Diff line number Diff line change
@@ -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<ResidentDTO> {
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<ResidentDTO> {
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<ResidentDTO> {
try {
let deletedResident = await Prisma.resident.delete({
where: {id: residentId}
});
return deletedResident;
} catch (error: unknown) {
//log it
throw error;
}
}
async get_all_residents(): Promise<ResidentDTO[]> {
try {
let allResidents = await Prisma.resident.findMany();
return allResidents;
} catch (error: unknown) {
//log it
throw error;
}
}
async get_residents_by_id(residentId: number[]): Promise<ResidentDTO[]> {
try {
let allResidentsById = await Prisma.resident.findMany({
where: {id: {in: residentId}}
});
return allResidentsById;
} catch (error: unknown) {
//log it
throw error;
}
}

}

export default ResidentService;
54 changes: 27 additions & 27 deletions backend/typescript/services/interfaces/residentService.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<ResidentDTO>;
delete_resident(residentId: number): Promise<ResidentDTO>;

/**
* Gets all residents
Expand All @@ -80,3 +79,4 @@ interface IResidenceService {
get_residents_by_id(residentId: number[]): Promise<Array<ResidentDTO>>;
}

export default IResidentService;
66 changes: 66 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"@prisma/client": "^5.5.2",
"graphql-tag": "^2.12.6"
}
}

0 comments on commit 323364a

Please sign in to comment.