Skip to content

Commit

Permalink
Merge pull request #5 from advanced-computer-lab-2023/VCP-20-backend
Browse files Browse the repository at this point in the history
Vcp 20 backend
  • Loading branch information
amir-awad authored Oct 8, 2023
2 parents 237dbfd + 3c183d0 commit 9946066
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 9 deletions.
1 change: 1 addition & 0 deletions clinic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "./src/app.js",
"type": "module",
"scripts": {
"dev": "nodemon ./src/app.js",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint . --fix"
},
Expand Down
2 changes: 2 additions & 0 deletions clinic/src/api/HealthPackageAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ export const healthPackage = (app) => {
}
});



};
28 changes: 28 additions & 0 deletions clinic/src/api/doctor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import DoctorService from '../service/doctor-service.js';
import { isValidMongoId } from '../utils/Validation.js';
import { NOT_FOUND_STATUS_CODE, ERROR_STATUS_CODE, OK_STATUS_CODE } from '../utils/Constants.js';

export const doctor = (app) => {
const service = new DoctorService();

app.get('/doctor/:id', async (req,res) => {
try{
const id = req.params.id;
if(!isValidMongoId(id))
return res.status(ERROR_STATUS_CODE).json({ message:'Invalid ID' });
const doctor = await service.getDoctorById(id);
if(doctor){
res.status(OK_STATUS_CODE).json({ doctor });
}
else{
res.status(NOT_FOUND_STATUS_CODE).json({ message:'doctor not found' });
}
}
catch(error){
res.status(ERROR_STATUS_CODE).json({ message:error });
}
});



};
11 changes: 7 additions & 4 deletions clinic/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import express from 'express';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import { healthPackage } from './api/HealthPackageAPI.js';
import { doctor } from './api/doctor.js';
import { clinic } from './api/clinic.js';
import { PORT } from './utils/Constants.js';
import cors from 'cors';
//import {doctor } from './api/doctor.js';
Expand All @@ -27,17 +29,18 @@ await connect();

app.use(express.json());
app.use(cors({
origin: ["http://localhost:3000","http://localhost:3001", "http://localhost:3002"],
origin: ['http://localhost:3000','http://localhost:3001', 'http://localhost:3002'],
credentials: true
}))
}));

clinic(app);
healthPackage(app);
//admin(app);
//doctor(app);
//appointment(app);
doctor(app);

const port = process.env.PORT || PORT;

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
});
5 changes: 4 additions & 1 deletion clinic/src/database/models/Doctor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ const Doctor = mongoose.Schema({
educationalBackground: {
type: String,
required: true
}
},
availableSlots: {
type: [Date]
},
});

const DoctorModel = mongoose.model('Doctor', Doctor);
Expand Down
9 changes: 7 additions & 2 deletions clinic/src/database/repository/doctor-repository.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
//import DoctorModel from '../models/Doctor.js';
import Doctor from '../models/Doctor.js';

class DoctorRepository{


async findDoctorById(id){
const doctor = await Doctor.findById(id, '-userData.password');
return doctor;
}

}

export default DoctorRepository;
17 changes: 15 additions & 2 deletions clinic/src/service/doctor-service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
//import DoctorRepository from '../database/repository/doctor-repository.js';
import DoctorRepository from '../database/repository/doctor-repository.js';

class DoctorService{
class DoctorService {
constructor() {
this.repository = new DoctorRepository();
}

async getDoctorById(id){
const doctor = await this.repository.findDoctorById(id);
if(doctor){
return doctor;
}
else{
console.log('no doctor wuth this id was found');
}
}

}

export default DoctorService;
5 changes: 5 additions & 0 deletions clinic/src/utils/Validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import mongoose from 'mongoose';

export const isValidMongoId = (id) => {
return mongoose.Types.ObjectId.isValid(id);
};

0 comments on commit 9946066

Please sign in to comment.