Skip to content

Commit

Permalink
Merge pull request #8 from moevm/all-endpoints
Browse files Browse the repository at this point in the history
All endpoints
  • Loading branch information
TheAnton1 authored Dec 23, 2023
2 parents 47f63b7 + 3972440 commit 2de08a2
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 9 deletions.
23 changes: 22 additions & 1 deletion backend/src/controllers/PatientController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import express from "express";
import { patientService } from "../services/PatientsService";

export class PatientController {
static async getPatientInfo(req: express.Request, res: express.Response) {
static async getPatientById(req: express.Request, res: express.Response) {
try {
const patient = await patientService.getPatientById(req.params.id)

res.json(patient)
} catch (error) {
if (error instanceof Error) {
res.status(400).send(error.message)
}
}
}

static async getAllPatients(req: express.Request, res: express.Response) {
try {
const patient = await patientService.getAllPatients()

res.json(patient)
} catch (error) {
if (error instanceof Error) {
res.status(400).send(error.message)
}
}
}
}
6 changes: 3 additions & 3 deletions backend/src/init_data/init_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class DataInitializer {
const patientIds: Types.ObjectId[] = patientUsers.map(patient => patient._id!) as Types.ObjectId[];

const testData: PatientDto[] = [
{ _id: patientIds[0], name: "Максим", surname: "Тишкин", email: "[email protected]", birthdate: "22.08.2002", contact: "987654321", bloodgroup: "A+" },
{ _id: patientIds[1], name: "Саша", surname: "Морозов", email: "[email protected]", birthdate: "22.08.2002", contact: "987654321", bloodgroup: "A+" },
{ _id: patientIds[2], name: "Миша", surname: "Переверза", email: "[email protected]", birthdate: "22.08.2002", contact: "987654321", bloodgroup: "A+" },
{ _id: patientIds[0], name: "Максим", surname: "Тишкин", email: "[email protected]", birthdate: "22.08.2002", contact: "987654321", bloodgroup: "A+", card: "0001" },
{ _id: patientIds[1], name: "Саша", surname: "Морозов", email: "[email protected]", birthdate: "22.08.2002", contact: "987654321", bloodgroup: "A+", card: "0002" },
{ _id: patientIds[2], name: "Миша", surname: "Переверза", email: "[email protected]", birthdate: "22.08.2002", contact: "987654321", bloodgroup: "A+", card: "0003" },
];

await PatientsMongoCollection.insertMany(testData);
Expand Down
1 change: 1 addition & 0 deletions backend/src/models/dto/PatientDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class PatientDto {
readonly email: string,
readonly birthdate: string,
readonly contact: string,
readonly card: string,
readonly bloodgroup: string,
) {}
}
1 change: 1 addition & 0 deletions backend/src/models/mongoose/PatientModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const PatientSchema = new Schema({
email: { type: String, required: true },
birthdate: { type: String, required: true},
contact: { type: String, required: true},
card: { type: String, required: true},
bloodgroup: { type: String, required: true},
})

Expand Down
6 changes: 2 additions & 4 deletions backend/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ router.post('/login', LoginController.login)

router.get('/doctors/:id', DoctorController.getDoctorById)

router.get('/patients/', PatientController.getPatientInfo)
router.get('/patients/:id', PatientController.getPatientById)
router.get('/patients', PatientController.getAllPatients)

router.get('/admins/', AdminController.getAdminInfo)

router.get('/', LoginController.login)

export default router;
67 changes: 67 additions & 0 deletions backend/src/services/PatientsService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import mongoose from "mongoose"
import { PatientsMongoCollection } from "../models/mongoose/PatientModel"
import { AppoitmentMongoCollection } from "../models/mongoose/AppoitmentModel"
import { ProceduresMongoCollection } from "../models/mongoose/ProcedureModel"
import { DoctorMongoCollection } from "../models/mongoose/DoctorModel"

class PatientService {
public async getPatientById(id: string) {
const objId = new mongoose.Types.ObjectId(id)
const patient = await PatientsMongoCollection.findById(objId)

const appoitments = await AppoitmentMongoCollection.find({patient_id: objId})

let myAppoitments: { [key: string]: any }[] = [];

for(const appoitment of appoitments) {
const procedure = await ProceduresMongoCollection.findOne({_id: appoitment?.procedure_id})
const doctor = await DoctorMongoCollection.findOne({_id: appoitment?.doctor_id})
const date = appoitment?.date
const appointmentDetails = {
"date": date,
"procedure": procedure?.name,
"doctor": doctor?.name + " " + doctor?.surname
};
console.log(appointmentDetails);

myAppoitments.push(appointmentDetails);
}
console.log({ "name": patient?.name, "surname": patient?.surname, "appoitments": myAppoitments});

return { "name": patient?.name, "surname": patient?.surname, "appoitments": myAppoitments}
}

public async getAllPatients() {
const patients = await PatientsMongoCollection.find({})
console.log(patients);

const prettyPatients: { [key: string]: any }[] = []
patients.forEach(patient => {
const name = patient.name + " " + patient.surname
const age = this.calculateAge(patient.birthdate)
const card = patient.card
const phone = patient.contact
prettyPatients.push({
"name": name,
"age": age,
"card": card,
"phone": phone
})
});
console.log(prettyPatients);

return prettyPatients
}

private calculateAge(date: string): number {
const [day, month, year] = date.split(".")
const birthdate = new Date(`${year}-${month}-${day}`)
const currentDate = new Date()
const timediff = currentDate.getTime() - birthdate.getTime()
const age = Math.floor(timediff / (365.25 * 24 * 60 * 60 * 1000));
return age

}
}

export const patientService = new PatientService()
10 changes: 9 additions & 1 deletion backend/test.rest
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ Content-Type: application/json

###

GET http://localhost:1234/api/doctors/6586fe604d2527dccc46e86f
GET http://localhost:1234/api/doctors/658705e53b786ef1006e04f9

###

GET http://localhost:1234/api/patients/658705e53b786ef1006e04fb

###

GET http://localhost:1234/api/patients

0 comments on commit 2de08a2

Please sign in to comment.