public async Task <OperationResult> Create(AppointmentModel appointmentModel)
        {
            using (var transaction = await _dbContext.Database.BeginTransactionAsync())
            {
                try
                {
                    if (appointmentModel.ExpireAt == null || appointmentModel.StartAt == null ||
                        appointmentModel.ExpireAt <= appointmentModel.StartAt || appointmentModel.StartAt <= DateTime.UtcNow)
                    {
                        return(new OperationResult(
                                   false,
                                   _appointmentLocalizationService.GetString("AppointmentDateNotCorrect")));
                    }

                    var appointment = new Entities.Appointment
                    {
                        CreatedAt       = DateTime.UtcNow,
                        CreatedByUserId = UserId,
                        ExpireAt        = appointmentModel.ExpireAt,
                        StartAt         = appointmentModel.StartAt,
                        Info            = appointmentModel.Info,
                        Description     = appointmentModel.Description,
                        Title           = appointmentModel.Title,
                        ColorHex        = appointmentModel.ColorHex,
                        RepeatEvery     = appointmentModel.RepeatEvery,
                        RepeatType      = appointmentModel.RepeatType,
                        RepeatUntil     = appointmentModel.RepeatUntil,
                        SdkeFormId      = appointmentModel.eFormId
                    };

                    await appointment.Create(_dbContext);

                    foreach (var siteUid in appointmentModel.SiteUids)
                    {
                        var appointmentSite = new Entities.AppointmentSite()
                        {
                            AppointmentId    = appointment.Id,
                            MicrotingSiteUid = siteUid
                        };
                        await appointmentSite.Create(_dbContext);
                    }

                    transaction.Commit();
                    return(new OperationResult(
                               true,
                               _appointmentLocalizationService.GetString("AppointmentCreatedSuccessfully")));
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    Trace.TraceError(e.Message);
                    return(new OperationResult(false,
                                               _appointmentLocalizationService.GetString("ErrorWhileCreatingAppointment")));
                }
            }
        }
        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")));
                }
            }
        }