1- using System ;
2- using HealthCareABApi . Models ;
3- using HealthCareABApi . Repositories . Data ;
4- using Microsoft . EntityFrameworkCore ;
5-
6- namespace HealthCareABApi . Repositories . Implementations
7- {
8- public class AppointmentRepository : IAppointmentRepository
9- {
10- private readonly HealthCareDbContext _Dbcontext ;
11-
12- public AppointmentRepository ( HealthCareDbContext context )
13- {
14- _Dbcontext = context ;
15- }
16-
17- public async Task < IEnumerable < Appointment > > GetAllAsync ( )
18- {
19- return await _Dbcontext . Appointment
20- . Include ( a => a . Patient )
21- . Include ( a => a . Caregiver )
22- . ToListAsync ( ) ;
1+ using System ;
2+ using HealthCareABApi . Models ;
3+ using HealthCareABApi . Repositories . Data ;
4+ using Microsoft . EntityFrameworkCore ;
5+
6+ namespace HealthCareABApi . Repositories . Implementations
7+ {
8+ public class AppointmentRepository : IAppointmentRepository
9+ {
10+ private readonly HealthCareDbContext _Dbcontext ;
11+
12+ public AppointmentRepository ( HealthCareDbContext context )
13+ {
14+ _Dbcontext = context ;
15+ }
16+
17+ public async Task < IEnumerable < Appointment > > GetAllAsync ( )
18+ {
19+ return await _Dbcontext . Appointment
20+ . Include ( a => a . Patient )
21+ . Include ( a => a . Caregiver )
22+ . ToListAsync ( ) ;
2323 }
2424
2525 public async Task < Appointment > GetByIdAsync ( int id )
@@ -39,87 +39,126 @@ public async Task<Appointment> GetByPatientAndTimeAsync(int patientId, DateTime
3939 a . PatientId == patientId &&
4040 EF . Functions . DateDiffSecond ( a . DateTime , localTime ) == 0 && // Ignorerar millisekunder i databasen och kollar tiden på sekundnivå
4141 a . Status == AppointmentStatus . Scheduled ) ;
42- }
43-
44- public async Task < IEnumerable < Appointment > > GetByUserIdAsync ( int patientId )
45- {
46- return await _Dbcontext . Appointment
47- . Include ( x => x . Caregiver )
48- . Include ( x => x . Patient )
49- . Where ( x => x . PatientId == patientId && x . Status == AppointmentStatus . Completed )
50- . ToListAsync ( ) ;
51- }
52-
53- public async Task CreateAsync ( Appointment appointment )
54- {
55- if ( appointment == null )
56- {
57- throw new ArgumentNullException ( nameof ( appointment ) , "Appointment is null and will blow up the system in 3........2........1........." ) ;
58- }
59-
60- try
61- {
62- var availability = await _Dbcontext . Availability
63- . FirstOrDefaultAsync ( a =>
64- a . Caregiver . Id == appointment . CaregiverId &&
65- a . StartTime <= appointment . DateTime &&
66- a . EndTime > appointment . DateTime &&
67- ! a . IsBooked ) ;
68-
69- if ( availability == null )
70- {
71- throw new InvalidOperationException ( "No available slot for this time." ) ;
72- }
73-
74- availability . IsBooked = true ;
75- availability . Appointment = appointment ;
76-
77- await _Dbcontext . Appointment . AddAsync ( appointment ) ;
78-
79- await _Dbcontext . SaveChangesAsync ( ) ;
80- //await _Dbcontext.Appointment.AddAsync(appointment);
81- //await _Dbcontext.SaveChangesAsync();
82- }
83- catch ( DbUpdateException ex )
84- {
85- throw new InvalidOperationException ( "Database error while creating appointment" , ex ) ;
86- }
87- catch ( Exception ex )
88- {
89- throw new InvalidOperationException ( "Error creating new appointment" , ex ) ;
90- }
91- }
92-
93- public async Task < bool > UpdateAsync ( int id , Appointment appointment )
94- {
95- var exist = await _Dbcontext . Appointment . Where ( a => a . Id == id ) . FirstOrDefaultAsync ( ) ;
96-
97- if ( exist == null )
98- {
99- return false ;
100- }
101-
102- _Dbcontext . Appointment . Entry ( exist ) . CurrentValues . SetValues ( appointment ) ;
103- await _Dbcontext . SaveChangesAsync ( ) ;
104- return true ;
105- }
106-
107- public async Task DeleteAsync ( int id )
108- {
109- try
110- {
111- await _Dbcontext . Appointment . Where ( a => a . Id == id ) . ExecuteDeleteAsync ( ) ;
112- await _Dbcontext . SaveChangesAsync ( ) ;
113- }
114- catch ( DbUpdateException ex )
115- {
116- throw new InvalidOperationException ( "Database error while deleting appointment" , ex ) ;
117- }
118- catch ( Exception ex )
119- {
120- throw new InvalidOperationException ( "Error deleting appointment" , ex ) ;
121- }
12242 }
123- }
124- }
125-
43+
44+ public async Task < IEnumerable < Appointment > > GetCompletedByUserIdAsync ( int userId )
45+ {
46+ return await _Dbcontext . Appointment
47+ . Include ( x => x . Caregiver )
48+ . Include ( x => x . Patient )
49+ . Where ( x => x . PatientId == userId && x . Status == AppointmentStatus . Completed )
50+ . ToListAsync ( ) ;
51+ }
52+
53+ public async Task CreateAsync ( Appointment appointment )
54+ {
55+ if ( appointment == null )
56+ {
57+ throw new ArgumentNullException ( nameof ( appointment ) , "Appointment is null and will blow up the system in 3........2........1........." ) ;
58+ }
59+
60+ try
61+ {
62+ var availability = await _Dbcontext . Availability
63+ . FirstOrDefaultAsync ( a =>
64+ a . Caregiver . Id == appointment . CaregiverId &&
65+ a . StartTime <= appointment . DateTime &&
66+ a . EndTime > appointment . DateTime &&
67+ ! a . IsBooked ) ;
68+
69+ if ( availability == null )
70+ {
71+ throw new InvalidOperationException ( "No available slot for this time." ) ;
72+ }
73+
74+ availability . IsBooked = true ;
75+ availability . Appointment = appointment ;
76+
77+ await _Dbcontext . Appointment . AddAsync ( appointment ) ;
78+
79+ await _Dbcontext . SaveChangesAsync ( ) ;
80+ }
81+ catch ( DbUpdateException ex )
82+ {
83+ throw new InvalidOperationException ( "Database error while creating appointment" , ex ) ;
84+ }
85+ catch ( Exception ex )
86+ {
87+ throw new InvalidOperationException ( "Error creating new appointment" , ex ) ;
88+ }
89+ }
90+
91+ public async Task < bool > UpdateAsync ( int id , Appointment appointment )
92+ {
93+ var exist = await _Dbcontext . Appointment . Where ( a => a . Id == id ) . FirstOrDefaultAsync ( ) ;
94+
95+ if ( exist == null )
96+ {
97+ return false ;
98+ }
99+
100+ _Dbcontext . Appointment . Entry ( exist ) . CurrentValues . SetValues ( appointment ) ;
101+ await _Dbcontext . SaveChangesAsync ( ) ;
102+ return true ;
103+ }
104+
105+ public async Task DeleteAsync ( int id )
106+ {
107+ using ( var transaction = await _Dbcontext . Database . BeginTransactionAsync ( ) )
108+ {
109+
110+ try
111+ {
112+ var relatedAvailability = await _Dbcontext . Availability
113+ . Where ( a => a . AppointmentId == id )
114+ . FirstOrDefaultAsync ( ) ;
115+
116+ if ( relatedAvailability == null )
117+ {
118+ throw new ArgumentNullException ( "No availability is related to this appointment." ) ;
119+ }
120+
121+ relatedAvailability . AppointmentId = null ;
122+ relatedAvailability . IsBooked = false ;
123+ await _Dbcontext . SaveChangesAsync ( ) ; // Need to update and save appointmentId in selected slot before deleting the appointment cuz of FK constraint
124+
125+ await _Dbcontext . Appointment . Where ( a => a . Id == id ) . ExecuteDeleteAsync ( ) ;
126+ await _Dbcontext . SaveChangesAsync ( ) ;
127+
128+ await transaction . CommitAsync ( ) ;
129+ }
130+ catch ( DbUpdateException ex )
131+ {
132+ await transaction . RollbackAsync ( ) ;
133+ throw new InvalidOperationException ( "Database error while deleting appointment" , ex ) ;
134+ }
135+ catch ( Exception ex )
136+ {
137+ await transaction . RollbackAsync ( ) ;
138+ throw new InvalidOperationException ( "Error deleting appointment" , ex ) ;
139+ }
140+ }
141+ }
142+
143+ public async Task < IEnumerable < Appointment > > GetScheduledAppointmentsAsync ( int userId )
144+ {
145+ try
146+ {
147+ return await _Dbcontext . Appointment
148+ . Include ( a => a . Caregiver )
149+ . Include ( a => a . Patient )
150+ . Where ( a => a . PatientId == userId && a . Status == AppointmentStatus . Scheduled && a . DateTime > DateTime . Now )
151+ . ToListAsync ( ) ;
152+ }
153+ catch ( DbUpdateException ex )
154+ {
155+ throw new InvalidOperationException ( "Database error while fetching scheduled appointments" ) ;
156+ }
157+ catch ( Exception ex )
158+ {
159+ throw new InvalidOperationException ( "Error fetching scheduled appointments." , ex ) ;
160+ }
161+ }
162+ }
163+ }
164+
0 commit comments