Exemplo n.º 1
0
        public List <FIL.Contracts.DataModels.ScheduleDetail> GetScheduleDetails(FIL.Contracts.Commands.CreateEventV1.EventRecurranceCommand eventRecurranceCommand)
        {
            List <FIL.Contracts.DataModels.ScheduleDetail> eventSchedules  = new List <Contracts.DataModels.ScheduleDetail>();
            List <FIL.Contracts.DataModels.ScheduleDetail> eventSchedules1 = new List <Contracts.DataModels.ScheduleDetail>();
            var startDateTime = _commonUtilityProvider.GetUtcDate(eventRecurranceCommand.StartDateTime, eventRecurranceCommand.LocalStartTime, eventRecurranceCommand.TimeZoneOffSet);
            var endDateTime   = _commonUtilityProvider.GetUtcDate(eventRecurranceCommand.EndDateTime, eventRecurranceCommand.LocalEndTime, eventRecurranceCommand.TimeZoneOffSet);

            if (eventRecurranceCommand.ActionType == Contracts.Commands.CreateEventV1.ActionType.BulkReschedule)
            {
                SaveEventSchedule(eventRecurranceCommand);
                eventSchedules = _scheduleDetailRepository.GetAllByEventScheduleId(eventRecurranceCommand.EventScheduleId).ToList();
            }
            else
            {
                var scheduleDetail = _scheduleDetailRepository.Get(eventRecurranceCommand.ScheduleDetailId);
                scheduleDetail.StartDateTime = startDateTime;
                scheduleDetail.EndDateTime   = endDateTime;
                scheduleDetail.UpdatedBy     = eventRecurranceCommand.ModifiedBy;
                scheduleDetail.ModifiedBy    = eventRecurranceCommand.ModifiedBy;
                scheduleDetail.UpdatedUtc    = DateTime.UtcNow;
                eventSchedules.Add(scheduleDetail);
                return(eventSchedules);
            }
            foreach (var scheduleDetail in eventSchedules)
            {
                if (scheduleDetail.StartDateTime.Date >= startDateTime.Date && scheduleDetail.EndDateTime.Date <= endDateTime.Date)
                {
                    scheduleDetail.StartDateTime = _commonUtilityProvider.GetUtcDate(scheduleDetail.StartDateTime, eventRecurranceCommand.LocalStartTime, eventRecurranceCommand.TimeZoneOffSet);
                    scheduleDetail.EndDateTime   = _commonUtilityProvider.GetUtcDate(scheduleDetail.EndDateTime, eventRecurranceCommand.LocalEndTime, eventRecurranceCommand.TimeZoneOffSet);
                    scheduleDetail.UpdatedBy     = eventRecurranceCommand.ModifiedBy;
                    scheduleDetail.ModifiedBy    = eventRecurranceCommand.ModifiedBy;
                    scheduleDetail.UpdatedUtc    = DateTime.UtcNow;
                    eventSchedules1.Add(scheduleDetail);
                }
            }
            return(eventSchedules);
        }
        protected override async Task <ICommandResult> Handle(EventRecurranceCommand command)
        {
            try
            {
                /* If action is bulk insert */
                if (command.ActionType == ActionType.BulkInsert)
                {
                    var scheduleDetails = _saveScheduleDetailProvider.GetScheduleDetails(command);
                    try
                    {
                        /* Use dapper plus */
                        _scheduleDetailRepository.SaveAll(scheduleDetails);
                    }
                    catch (Exception e)
                    {
                        /* use dapper execute */
                        _scheduleDetailRepository.ExecuteCommand(FIL.Contracts.Utils.Constant.SqlStatement.ScheduleDetail.InsertCommand, scheduleDetails);
                    }
                    UpdateEventDetail(command);
                    UpdateEventAttribute(command);
                }
                /* If action is bulk reschedule or single reschedule */
                else if (command.ActionType == ActionType.BulkReschedule || command.ActionType == ActionType.SingleReschedule)
                {
                    var scheduleDetails = _updateScheduleDetailProvider.GetScheduleDetails(command);
                    try
                    {
                        /* Use dapper plus */
                        _scheduleDetailRepository.SaveAll(scheduleDetails);
                    }
                    catch (Exception e)
                    {
                        /* use dapper execute */
                        _scheduleDetailRepository.ExecuteCommand(FIL.Contracts.Utils.Constant.SqlStatement.ScheduleDetail.UpdateCommand, scheduleDetails);
                    }
                }
                /* If action is delete bulk or single delete */
                else if (command.ActionType == ActionType.BulkDelete || command.ActionType == ActionType.SingleDelete)
                {
                    var scheduleDetails = _deleteScheduleDetailProvider.GetScheduleDetails(command);
                    try
                    {
                        /* Use dapper plus */
                        _scheduleDetailRepository.DeleteAll(scheduleDetails);
                    }
                    catch (Exception e)
                    {
                        /* use dapper execute */
                        _scheduleDetailRepository.ExecuteCommand(FIL.Contracts.Utils.Constant.SqlStatement.ScheduleDetail.DeleteCommand, scheduleDetails);
                    }

                    if (command.ActionType == ActionType.BulkDelete)
                    {
                        var currentScheduleDetails = _scheduleDetailRepository.GetAllByEventScheduleId(command.EventScheduleId);
                        if (!currentScheduleDetails.Any())
                        {
                            _deleteScheduleDetailProvider.DeleteEventSchedule(command);
                        }
                    }
                }
                return(new EventRecurranceCommandResult
                {
                    Success = true
                });
            }
            catch (Exception e)
            {
                _logger.Log(LogCategory.Error, e);
                return(new EventRecurranceCommandResult {
                });
            }
        }