0% found this document useful (0 votes)
4 views

service 4

The document defines a DoctorServices class that implements IDoctorServices for managing doctor records in a healthcare application. It includes methods for retrieving, adding, updating, and deleting doctor information, as well as checking their availability and filtering by specialization. Each method handles exceptions and returns appropriate messages based on the success or failure of the operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

service 4

The document defines a DoctorServices class that implements IDoctorServices for managing doctor records in a healthcare application. It includes methods for retrieving, adding, updating, and deleting doctor information, as well as checking their availability and filtering by specialization. Each method handles exceptions and returns appropriate messages based on the success or failure of the operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using HealthCare.

Data;
using HealthCare.Domain;
using HealthCare.Domain.Models;
using HealthCare.Services.Interfaces;
using Microsoft.EntityFrameworkCore;

namespace HealthCare.Services.Services
{
public class DoctorServices : IDoctorServices
{
private HealthCareDbContext _dbContext;
public DoctorServices(HealthCareDbContext dbContext)
{
_dbContext = dbContext;
}

public async Task<object> GetDoctors()


{
try
{
List<Doctor> resDoctors = await _dbContext.Doctors
.OrderBy(doc => doc.Id)
.ToListAsync();
if (resDoctors.Count == 0) { return "No doctors exist"; }
return resDoctors;
}
catch
{
return "Empty Doctors List";
}
}

public async Task<string> AddDoctor(Doctor doctor)


{
try
{
_dbContext.Doctors.Add(doctor);
_dbContext.SaveChanges();
return "Doctor added successfully!!";
}
catch (Exception ex)
{
return "Enter Valid Data";
}
}

public async Task<object> GetDoctors(int id)


{
try
{
Doctor resDoctor = _dbContext.Doctors.FirstOrDefault(doc => doc.Id
== id);
if (resDoctor == null) { return $"No Doctor found with Id {id}"; }
return resDoctor;
}
catch (Exception ex)
{
return "An error occurred while fetching the doctor details.";
}
}

public async Task<object> GetDoctors(string name)


{
try
{
List<Doctor> resDoctor = await _dbContext.Doctors
.OrderBy(doc => doc.Name)
.Where(doc => doc.Name.Contains(name)).ToListAsync();
if (resDoctor.Count == 0) { return $"No Doctor found with Name:
{name}"; }
return resDoctor;
}
catch (Exception ex)
{
return "Enter Valid Name";
}
}

public async Task<object> UpdateDoctorInfo(int id, Doctor doctor)


{
try
{
Doctor? updateDoctor = await
_dbContext.Doctors.FirstOrDefaultAsync(doc => doc.Id == id);
if (updateDoctor != null)
{
updateDoctor.Name = doctor.Name;
updateDoctor.Gender = doctor.Gender;
updateDoctor.Age = doctor.Age;
updateDoctor.EmailAddress = doctor.EmailAddress;
updateDoctor.PhoneNumber = doctor.PhoneNumber;
updateDoctor.Address = doctor.Address;
updateDoctor.Specialization = doctor.Specialization;
updateDoctor.IsAvailable = doctor.IsAvailable;
updateDoctor.Experience = doctor.Experience;
_dbContext.SaveChanges();
return "Doctor Details Updated Successfully!!";
}
return $"Doctor not found with Id {id}";
}
catch (Exception ex)
{
return "Enter Valid Data";
}
}

public async Task<object> DeleteDoctor(int id)


{
try
{
Doctor? deleteDoctor = await
_dbContext.Doctors.FirstOrDefaultAsync(doc => doc.Id == id);
if (deleteDoctor == null) { return $"No doctor found with Id {id}";
}
_dbContext.Doctors.Remove(deleteDoctor);
_dbContext.SaveChanges();
return "Doctor Deleted Successfully!!";
}
catch (Exception ex)
{
return "Enter Valid Id";
}
}

public async Task<object> UpdateAvailability(int id, bool availability)


{
try
{
Doctor? updateDoctor = await
_dbContext.Doctors.FirstOrDefaultAsync(doc => doc.Id == id);
if (updateDoctor != null)
{
updateDoctor.IsAvailable = availability;
_dbContext.SaveChanges();
return "Availability Updated Successfully!!";
}
return $"No doctor found with Id {id}";
}
catch (Exception ex)
{
return "Enter Valid Data";
}
}

public async Task<object> GetBySpecializations()


{
try
{
List<Doctor> resDoctors = _dbContext.Doctors
.Select(doc => doc)
.OrderBy(doc => doc.Specialization)
.ToList();
if(resDoctors.Count == 0) { return "No Specializations found"; }
return resDoctors;
}
catch(Exception ex)
{
return "No Doctors found for specialization";
}
}

public async Task<object> GetDoctorsBySpecialization(string specialization)


{
try
{
List<Doctor> resDoctors = _dbContext.Doctors
.Select(doc => doc)
.Where(doc => doc.Specialization.Contains(specialization))
.OrderBy(doc => doc.Specialization)
.ToList();
if (resDoctors.Count == 0) { return $"No Doctor with specialization
{specialization}"; }
return resDoctors;
}
catch (Exception ex)
{
return "Enter valid specialization";
}
}

}
}

You might also like