public async Task <IActionResult> PutTherapistEvent(int eventId, TherapistEvent therapistEvent) { try { await _therapistEventService.UpdateTherapistEvent(eventId, therapistEvent); } catch (TherapistEventEventIdsDoNotMatchException e) { return(BadRequest(e)); } catch (TherapistEventDoesNotExistException e) { return(NotFound(e)); } catch (UserDoesNotExistException e) { return(BadRequest(e)); } catch (UserIsNotATherapistException e) { return(BadRequest(e)); } catch (TherapistEventCannotEndBeforeStartTimeException e) { return(BadRequest(e)); } return(NoContent()); }
public async Task <ActionResult <TherapistEvent> > PostTherapistEvent(TherapistEvent therapistEvent) { try { await _therapistEventService.AddTherapistEvent(therapistEvent); } catch (TherapistEventEventIdAlreadyExistsException e) { return(Conflict(e)); } catch (UserDoesNotExistException e) { return(NotFound(e)); } catch (UserIsNotATherapistException e) { return(BadRequest(e)); } catch (TherapistEventCannotEndBeforeStartTimeException e) { return(BadRequest(e)); } catch (DbUpdateException) { throw; } return(CreatedAtAction("GetTherapistEvents", new { id = therapistEvent.EventId }, therapistEvent)); }
/// <summary> /// Adds a therapist event to the database /// </summary> /// <param name="therapistEvent">The therapist event to be added</param> /// <returns>The therapist event that was added to the database</returns> /// <exception cref="TherapistEventEventIdAlreadyExistsException">Thrown if therapist event event id already exist</exception> /// <exception cref="TherapistActivityDoesNotExistException">Thrown if therapist event activity name does not exist</exception> /// <exception cref="UserDoesNotExistException">Thrown if therapist event therapist id does not exist</exception> /// <exception cref="DbUpdateException">Thrown if an exception occured while trying to save changes to database</exception> public async Task <TherapistEvent> AddTherapistEvent(TherapistEvent therapistEvent) { if (await TherapistEventExistsById(therapistEvent.EventId)) { throw new TherapistEventEventIdAlreadyExistsException(); } if (!await UserExists(therapistEvent.TherapistId)) { throw new UserDoesNotExistException(); } if (!await IsTherapist(therapistEvent.TherapistId)) { throw new UserIsNotATherapistException(); } if (therapistEvent.EndTime < therapistEvent.StartTime) { throw new TherapistEventCannotEndBeforeStartTimeException(); } therapistEvent.Active = true; _context.TherapistEvent.Add(therapistEvent); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { throw; } return(therapistEvent); }
/// <summary> /// Updates all fields from passed therapistEvent object to local copy in database /// </summary> /// <param name="local">Local copy of object to be modified in database</param> /// <param name="therapistEvent">New object that holds updated fields</param> private void UpdateNonNullAndNonEmptyFields(TherapistEvent local, TherapistEvent therapistEvent) { foreach (PropertyInfo prop in typeof(TherapistEvent).GetProperties()) { if (prop.GetValue(therapistEvent) != null && (prop.PropertyType != typeof(string) || !prop.GetValue(therapistEvent).Equals(""))) { prop.SetValue(local, prop.GetValue(therapistEvent)); } } }
/// <summary> /// Updates a therapist event in the database /// </summary> /// <param name="eventId">The event id of the therapist event to update</param> /// <param name="therapistEvent">The therapist event to update</param> /// <returns>The updated therapist event</returns> /// <exception cref="TherapistEventEventIdsDoNotMatchException">Thrown if event ids do not match</exception> /// <exception cref="TherapistEventDoesNotExistException">Thrown if therapist event does not exist</exception> /// <exception cref="TherapistActivityDoesNotExistException">Thrown if activity name does not match a activity name</exception> /// <exception cref="UserDoesNotExistException">Thrown if therapist id does not match a user</exception> /// <exception cref="DbUpdateConcurrencyException">Thrown if an exception occured while trying to save changes to database</exception> public async Task <TherapistEvent> UpdateTherapistEvent(int eventId, TherapistEvent therapistEvent) { if (eventId != therapistEvent.EventId) { throw new TherapistEventEventIdsDoNotMatchException(); } if (!await UserExists(therapistEvent.TherapistId)) { throw new UserDoesNotExistException(); } if (!await IsTherapist(therapistEvent.TherapistId)) { throw new UserIsNotATherapistException(); } if (therapistEvent.EndTime < therapistEvent.StartTime) { throw new TherapistEventCannotEndBeforeStartTimeException(); } //var local = _context.TherapistEvent.Local.FirstOrDefault(t => t.EventId == eventId && t.Active); var local = await _context.TherapistEvent.FindAsync(eventId); if (local == null) { throw new TherapistEventDoesNotExistException(); } UpdateNonNullAndNonEmptyFields(local, therapistEvent); _context.Entry(local).State = EntityState.Modified; //_context.Entry(therapistEvent).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } return(therapistEvent); }
public async Task <ActionResult <IEnumerable <TherapistEvent> > > GetTherapistEventsByTherapistId(TherapistEvent therapistEvent) { if (therapistEvent == null) { return(BadRequest()); } var allTherapistEvents = await _therapistEventService.GetAllTherapistEventsByTherapistId(therapistEvent); return(Ok(allTherapistEvents)); }
public void Initialize() { var options = new DbContextOptionsBuilder <CoreDbContext>() .UseInMemoryDatabase(databaseName: "TherapistEventDatabase") .Options; _testContext = new CoreDbContext(options); _testContext.Database.EnsureDeleted(); _testUsers = new List <User>(); _testTherapistEvents = new List <TherapistEvent>(); _testTargetStartDateTime = new DateTime(2010, 2, 8); _testTargetEndDateTime = new DateTime(2010, 2, 12); for (var i = 0; i < 8; i++) { var newUser = ModelFakes.UserFake.Generate(); _testContext.Add(newUser); _testContext.SaveChanges(); _testUsers.Add(ObjectExtensions.Copy(newUser)); Permission newPermission = new Permission { UserId = newUser.UserId, Role = "therapist" }; _testContext.Add(newPermission); _testContext.SaveChanges(); var newTherapistEvent = ModelFakes.TherapistEventFake.Generate(); newTherapistEvent.TherapistId = newUser.UserId; if (i <= 2) { newTherapistEvent.StartTime = _testTargetStartDateTime.AddDays(i); newTherapistEvent.EndTime = _testTargetEndDateTime.AddDays(-1 * i); } _testContext.Add(newTherapistEvent); _testContext.SaveChanges(); _testTherapistEvents.Add(ObjectExtensions.Copy(newTherapistEvent)); } _testNonTherapistUser = ModelFakes.UserFake.Generate(); _testContext.Add(_testNonTherapistUser); _testContext.SaveChanges(); _testUsers.Add(ObjectExtensions.Copy(_testNonTherapistUser)); var newEdgeTherapistEvent = ModelFakes.TherapistEventFake.Generate(); newEdgeTherapistEvent.TherapistId = _testUsers[0].UserId; newEdgeTherapistEvent.StartTime = _testTargetStartDateTime.AddDays(-1); _testContext.Add(newEdgeTherapistEvent); _testContext.SaveChanges(); _testTherapistEvents.Add(ObjectExtensions.Copy(newEdgeTherapistEvent)); newEdgeTherapistEvent = ModelFakes.TherapistEventFake.Generate(); newEdgeTherapistEvent.TherapistId = _testUsers[0].UserId; newEdgeTherapistEvent.EndTime = _testTargetEndDateTime.AddDays(1); _testContext.Add(newEdgeTherapistEvent); _testContext.SaveChanges(); _testTherapistEvents.Add(ObjectExtensions.Copy(newEdgeTherapistEvent)); _nonActiveTherapistEvent = ModelFakes.TherapistEventFake.Generate(); _nonActiveTherapistEvent.Active = false; _nonActiveTherapistEvent.StartTime = _testTargetStartDateTime; _nonActiveTherapistEvent.EndTime = _testTargetEndDateTime; _testContext.Add(_nonActiveTherapistEvent); _testContext.SaveChanges(); _testTherapistEvents.Add(ObjectExtensions.Copy(_nonActiveTherapistEvent)); _testTherapistEvent = new TherapistEvent { StartTime = DateTime.MinValue, EndTime = DateTime.MaxValue, TherapistId = _testUsers[0].UserId }; _testTherapistEventService = new TherapistEventService(_testContext); _testTherapistEventController = new TherapistEventController(_testTherapistEventService); }
/// <summary> /// Gets all therapist events within given date range of parameter therapist event /// and match therapist ids /// </summary> /// <param name="therapistEvent">The therapist event to compare against existing therapist event records</param> /// <returns>All therapist events records within date range and match therapist ids</returns> /// <exception cref="UserDoesNotExistException">Thrown if therapist id does not match a user</exception> public async Task <IEnumerable <TherapistEvent> > GetAllTherapistEventsByTherapistId(TherapistEvent therapistEvent) { if (!await UserExists(therapistEvent.TherapistId)) { throw new UserDoesNotExistException(); } return(await _context.TherapistEvent .Where(d => d.StartTime >= therapistEvent.StartTime && d.EndTime <= therapistEvent.EndTime && d.TherapistId == therapistEvent.TherapistId && d.Active) .ToListAsync()); }
/// <summary> /// Gets all therapist events within given date range of parameter therapist event /// </summary> /// <param name="therapistEvent">The therapist event to compare against existing therapist event records</param> /// <returns>All therapist event records within date range</returns> public async Task <IEnumerable <TherapistEvent> > GetAllTherapistEvents(TherapistEvent therapistEvent) { return(await _context.TherapistEvent .Where(d => d.StartTime >= therapistEvent.StartTime && d.EndTime <= therapistEvent.EndTime && d.Active) .ToListAsync()); }