Skip to content

Commit

Permalink
Merge pull request #2 from advanced-computer-lab-2023/VCP-9
Browse files Browse the repository at this point in the history
Vcp 9 backend
  • Loading branch information
amir-awad authored Oct 8, 2023
2 parents 7ee7510 + 189abe5 commit 8d7f861
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 53 deletions.
30 changes: 15 additions & 15 deletions clinic/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ const app = express();
const mongoURL = process.env.MONGO_URI;

const connect = async () => {
try {
await mongoose.connect(mongoURL);
console.log('Database connected');
} catch (err) {
console.error('Error connecting to the database:', err);
}
try {
await mongoose.connect(mongoURL);
console.log('Database connected');
} catch (err) {
console.error('Error connecting to the database:', err);
}
};

await connect();

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

healthPackage(app);
Expand All @@ -48,5 +48,5 @@ appointment(app);
const port = process.env.PORT || PORT;

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
console.log(`Server is running on port ${port}`);
});
19 changes: 18 additions & 1 deletion patient/src/api/patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { isValidMongoId } from '../utils/Validation.js';
export const patient = (app) => {
const service = new PatientService();

app.get('/all-patients', async (req, res) => {
app.get('/patients', async (req, res) => {
const allPatients = await service.getAllPatient();
if (allPatients.length > EMPTY_SIZE) {
res.status(OK_STATUS_CODE).json(allPatients);
Expand All @@ -21,6 +21,23 @@ export const patient = (app) => {
}
});

app.get('/family-members/:id', async (req, res) => {
const { id } = req.params;
if (!isValidMongoId(id)) {
return res
.status(NOT_FOUND_STATUS_CODE)
.json({ message: 'family members not found' });
}
try {
const data = await service.getFamilyMembers(id);
res.status(OK_STATUS_CODE).json(data);
} catch (err) {
res.status(NOT_FOUND_STATUS_CODE).json({
message: 'family members not found',
});
}
});

app.patch('/family-members/:id', async (req, res) => {
const { id } = req.params;
if (!isValidMongoId(id)) {
Expand Down
36 changes: 18 additions & 18 deletions patient/src/database/repository/patient-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import PatientModel from '../models/Patient.js';
import { FAMILY_MEMBERS_PROJECTION } from '../../utils/Constants.js';

class PatientRepository {
async findAllPatients() {
const allPatients = await PatientModel.find();
return allPatients;
}
async findFamilyMembers(id) {
const familyMembers = await PatientModel.findById(
id,
FAMILY_MEMBERS_PROJECTION
);
return familyMembers;
}
async findAllPatients() {
const allPatients = await PatientModel.find();
return allPatients;
}
async findFamilyMembers(id) {
const familyMembers = await PatientModel.findById(
id,
FAMILY_MEMBERS_PROJECTION
);
return familyMembers;
}

async addFamilyMember(id, familyMembers) {
return await PatientModel.findOneAndUpdate(
{ _id: id },
{ familyMembers },
{ new: true, runValidators: true }
).select(FAMILY_MEMBERS_PROJECTION);
}
async addFamilyMember(id, familyMembers) {
return await PatientModel.findOneAndUpdate(
{ _id: id },
{ familyMembers },
{ new: true, runValidators: true }
).select(FAMILY_MEMBERS_PROJECTION);
}
}

export default PatientRepository;
36 changes: 18 additions & 18 deletions patient/src/service/patient-service.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import PatientRepository from '../database/repository/patient-repository.js';

class PatientService {
constructor() {
this.repository = new PatientRepository();
}
constructor() {
this.repository = new PatientRepository();
}

async getAllPatient() {
const patients = await this.repository.findAllPatients();
return patients;
}
async getAllPatient() {
const patients = await this.repository.findAllPatients();
return patients;
}

async getFamilyMembers(id) {
const familyMembers = await this.repository.findFamilyMembers(id);
return familyMembers;
}
async getFamilyMembers(id) {
const familyMembers = await this.repository.findFamilyMembers(id);
return familyMembers;
}

async addFamilyMember(id, updates) {
const familyMembers = await this.repository.addFamilyMember(
id,
updates
);
return familyMembers;
}
async addFamilyMember(id, updates) {
const familyMembers = await this.repository.addFamilyMember(
id,
updates
);
return familyMembers;
}
}

export default PatientService;
2 changes: 1 addition & 1 deletion patient/src/utils/Validation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mongoose from 'mongoose';

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

0 comments on commit 8d7f861

Please sign in to comment.