public async Task Update(AppointmentPnDbContext dbContext) { AppointmentPrefillFieldValue appointmentPrefillFieldValue = dbContext.AppointmentPrefillFieldValues.FirstOrDefault(x => x.Id == Id); if (appointmentPrefillFieldValue == null) { throw new NullReferenceException($"Could not find AppointmentPrefillFieldValue with {Id}"); } appointmentPrefillFieldValue.AppointmentId = AppointmentId; appointmentPrefillFieldValue.MicrotingSiteUid = MicrotingSiteUid; appointmentPrefillFieldValue.FieldId = FieldId; appointmentPrefillFieldValue.FieldValue = FieldValue; appointmentPrefillFieldValue.AppointmentFvId = AppointmentFvId; appointmentPrefillFieldValue.WorkflowState = WorkflowState; appointmentPrefillFieldValue.UpdatedByUserId = UpdatedByUserId; if (dbContext.ChangeTracker.HasChanges()) { appointmentPrefillFieldValue.UpdatedAt = DateTime.UtcNow; appointmentPrefillFieldValue.Version += 1; dbContext.SaveChanges(); dbContext.AppointmentPrefillFieldValueVersions.Add(MapVersions(appointmentPrefillFieldValue)); dbContext.SaveChanges(); } }
public async Task Delete(AppointmentPnDbContext dbContext) { AppointmentPrefillFieldValue appointmentPrefillFieldValue = dbContext.AppointmentPrefillFieldValues.FirstOrDefault(x => x.Id == Id); if (appointmentPrefillFieldValue == null) { throw new NullReferenceException($"Could not find AppointmentPrefillFieldValue with {Id}"); } appointmentPrefillFieldValue.WorkflowState = Constants.WorkflowStates.Removed; if (dbContext.ChangeTracker.HasChanges()) { appointmentPrefillFieldValue.UpdatedAt = DateTime.UtcNow; appointmentPrefillFieldValue.Version += 1; dbContext.SaveChanges(); dbContext.AppointmentPrefillFieldValueVersions.Add(MapVersions(appointmentPrefillFieldValue)); dbContext.SaveChanges(); } }
private AppointmentPrefillFieldValueVersion MapVersions( AppointmentPrefillFieldValue appointmentPrefillFieldValue) { AppointmentPrefillFieldValueVersion appointmentPrefillFieldValueVersion = new AppointmentPrefillFieldValueVersion() { AppointmentId = appointmentPrefillFieldValue.AppointmentId, MicrotingSiteUid = appointmentPrefillFieldValue.MicrotingSiteUid, FieldId = appointmentPrefillFieldValue.FieldId, FieldValue = appointmentPrefillFieldValue.FieldValue, AppointmentFvId = appointmentPrefillFieldValue.AppointmentFvId, Version = appointmentPrefillFieldValue.Version, CreatedAt = appointmentPrefillFieldValue.CreatedAt, CreatedByUserId = appointmentPrefillFieldValue.CreatedByUserId, UpdatedAt = appointmentPrefillFieldValue.UpdatedAt, UpdatedByUserId = appointmentPrefillFieldValue.UpdatedByUserId, WorkflowState = appointmentPrefillFieldValue.WorkflowState, AppointmentPrefillFieldValueId = appointmentPrefillFieldValue.Id }; return(appointmentPrefillFieldValueVersion); }
public async Task <OperationResult> Update(AppointmentModel appointmentModel) { using (var transaction = await _dbContext.Database.BeginTransactionAsync()) { try { Debugger.Break(); var appointment = await _dbContext.Appointments .Include(x => x.AppointmentSites) .Include(x => x.AppointmentPrefillFieldValues) .FirstOrDefaultAsync(x => x.Id == appointmentModel.Id); if (appointment.StartAt <= DateTime.UtcNow) { return(new OperationResult( false, _appointmentLocalizationService.GetString("CannotEditAppointment"))); } if (appointmentModel.ExpireAt == null || appointmentModel.StartAt == null || appointmentModel.ExpireAt <= appointmentModel.StartAt || appointmentModel.StartAt <= DateTime.UtcNow) { return(new OperationResult( false, _appointmentLocalizationService.GetString("AppointmentDateNotCorrect"))); } if (appointmentModel.ExpireAt > appointmentModel.RepeatUntil) { appointmentModel.RepeatUntil = appointmentModel.ExpireAt; } appointment.UpdatedAt = DateTime.UtcNow; appointment.UpdatedByUserId = UserId; appointment.ExpireAt = appointmentModel.ExpireAt; appointment.StartAt = appointmentModel.StartAt; appointment.Info = appointmentModel.Info; appointment.Description = appointmentModel.Description; appointment.Title = appointmentModel.Title; appointment.ColorHex = appointmentModel.ColorHex; appointment.RepeatEvery = appointmentModel.RepeatEvery; appointment.RepeatType = appointmentModel.RepeatType; appointment.RepeatUntil = appointmentModel.RepeatUntil; appointment.SdkeFormId = appointmentModel.eFormId; await appointment.Update(_dbContext); var asToDelete = appointment.AppointmentSites .Where(s => appointmentModel.SiteUids.All(x => x != s.MicrotingSiteUid)); foreach (var site in asToDelete) { await site.Delete(_dbContext); } var asToCreate = appointmentModel.SiteUids .Where(s => appointment.AppointmentSites.All(x => x.MicrotingSiteUid != s)); foreach (var siteUid in asToCreate) { var appointmentSite = new Entities.AppointmentSite() { AppointmentId = appointment.Id, MicrotingSiteUid = siteUid }; await appointmentSite.Create(_dbContext); } var afToDelete = appointment.AppointmentPrefillFieldValues .Where(f => appointmentModel.Fields.All(x => x.FieldId != f.FieldId)); foreach (var field in afToDelete) { await field.Delete(_dbContext); } var afToCreate = appointmentModel.Fields .Where(f => appointment.AppointmentPrefillFieldValues.All(x => x.FieldId != f.FieldId)); foreach (var field in afToCreate) { var appointmentPrefillField = new Entities.AppointmentPrefillFieldValue() { AppointmentId = appointment.Id, FieldId = field.FieldId, FieldValue = field.FieldValue }; await appointmentPrefillField.Create(_dbContext); } transaction.Commit(); return(new OperationResult( true, _appointmentLocalizationService.GetString("AppointmentUpdatedSuccessfully"))); } catch (Exception e) { Trace.TraceError(e.Message); transaction.Rollback(); return(new OperationResult( false, _appointmentLocalizationService.GetString("ErrorWhileUpdatingAppointment"))); } } }