public async Task <IActionResult> AdmitAsync([FromBody] RoomPatient RoomPatient) { var Patient = await _context.Patient.FirstOrDefaultAsync(x => x.Id == RoomPatient.PatientId && x.IsAdmitted == false); // If we are not able to find the patient or if the patient is already admitted return BadRequest if (Patient == null) { return(BadRequest()); } var Room = await _context.Room.FirstOrDefaultAsync(x => x.Id == RoomPatient.RoomId && x.CurrentCapacity > 0); // If we are not able to find the room or the rooms capacity is 0 return BadRequest if (Room == null) { return(BadRequest()); } // Admit the patient Patient.IsAdmitted = true; // Decrease the capacity of the room by 1 Room.CurrentCapacity = Room.CurrentCapacity - 1; _context.Patient.Update(Patient); _context.Room.Update(Room); // Log that a Patient is now added to a particular Room _context.Add(RoomPatient); await _context.SaveChangesAsync(); return(NoContent()); }
public async Task PatientCheckoutTwiceTestsAsync() { var scopeFactory = _factory.Services; using (var scope = scopeFactory.CreateScope()) { var context = scope.ServiceProvider.GetService <DataContext>(); await DBUtilities.InitializeDbForTestsAsync(context); // Arrange var Patient = await context.Patient.FirstOrDefaultAsync(); var Room = await context.Room.FirstOrDefaultAsync(); var RoomPatient = new RoomPatient { RoomId = Room.Id, PatientId = Patient.Id }; var Request = new HttpRequestMessage(HttpMethod.Post, "api/patient/admit"); Request.Content = new StringContent(JsonSerializer.Serialize(RoomPatient), Encoding.UTF8, "application/json"); var Request1 = new HttpRequestMessage(HttpMethod.Post, "api/patient/checkout"); Request1.Content = new StringContent(JsonSerializer.Serialize(Patient), Encoding.UTF8, "application/json"); var Request2 = new HttpRequestMessage(HttpMethod.Post, "api/patient/checkout"); Request2.Content = new StringContent(JsonSerializer.Serialize(Patient), Encoding.UTF8, "application/json"); // Act var Response = await httpClient.SendAsync(Request); Response.EnsureSuccessStatusCode(); var Response1 = await httpClient.SendAsync(Request1); Response1.EnsureSuccessStatusCode(); var Response2 = await httpClient.SendAsync(Request2); // Assert var StatusCode = Response2.StatusCode; Assert.Equal(HttpStatusCode.BadRequest, StatusCode); } }