Ejemplo n.º 1
0
        public int DeleteAll(string items)
        {
            try
            {
                var idsToRemove = items.ParseToIntArray().ToList();
                idsToRemove.ForEach(i => _messageRepository.Delete(i, DeleteState.SoftDelete));

                return(idsToRemove.Count);
            }
            catch (LogicalException ex)
            {
                _logger.LogRunTimeError(ex, ex.Message);
                throw;
            }
        }
Ejemplo n.º 2
0
        public string OutputMessage(ModelStateDictionary modelState)
        {
            var errorMessage = "";

            foreach (var errorKey in modelState.Keys)
            {
                modelState.TryGetValue(errorKey, out ModelState errorValue);

                foreach (var item in errorValue.Errors)
                {
                    var error = new ValidationError
                    {
                        Value     = errorValue.Value,
                        Exception = item.Exception,
                        Message   = item.ErrorMessage
                    };
                    if (error.Exception != null)
                    {
                        _logger.LogRunTimeError(error.Exception, error.Exception.Message);
                    }
                    errorMessage += error.GenerateMessage();
                }
            }
            return(errorMessage);
        }
        public async Task <int> DeleteAll(string items)
        {
            try
            {
                var idsToRemove = items.ParseToIntArray().ToList();
                foreach (var id in idsToRemove)
                {
                    var person = _personnelRepository.GetById(id);
                    if (person != null)
                    {
                        using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                        {
                            _personnelRepository.Delete(person.Id, DeleteState.SoftDelete);
                            //should also delete auto user for the personnel
                            await _authService.DeleteUserByUsernameAsync(person.Code);

                            scope.Complete();
                        }
                    }
                }

                return(idsToRemove.Count);
            }
            catch (LogicalException ex)
            {
                _logger.LogRunTimeError(ex, ex.Message);
                throw;
            }
        }
Ejemplo n.º 4
0
 private void Commit()
 {
     try
     {
         _context.SaveChanges();
     }
     catch (DbUpdateException ex)
     {
         _logger.LogRunTimeError(ex, "exception message: {0}, inner exception message: {1}"
                                 , ex.Message, ex.InnerException != null
             ? ex.InnerException.Message : "[empty]");
         throw;
     }
 }
Ejemplo n.º 5
0
        public async Task <CustomResult <string> > Update(UpdatePersonnelDutyDto dto)
        {
            var personnelDuty = _personnelDutyRepository.GetById(dto.Id);

            if (personnelDuty != null)
            {
                dto.PersonnelId = personnelDuty.PersonnelId;
                var personnel = _personnelRepository.GetById(dto.PersonnelId);
                if (personnel == null)
                {
                    return(new CustomResult <string>
                    {
                        Message = "person is not available"
                    });
                }

                var user = await _authService.FindUserByUsernameAsync(personnel.Code);

                if (user == null)
                {
                    return(new CustomResult <string>
                    {
                        Message = "user for person not found"
                    });
                }

                if (AlreadyRequested(personnel.Id, dto.DailyDuty, dto.HourlyDuty, personnelDuty.Id))
                {
                    return(new CustomResult <string>
                    {
                        Message = "cannot add multiple duties in the time period"
                    });
                }

                if (personnelDuty.RequestAction == RequestAction.Unknown)
                {
                    DateTime from = DateTime.Now;
                    DateTime to   = DateTime.Now;
                    switch (dto.DutyDuration)
                    {
                    case RequestDuration.Daily:
                        if (personnelDuty.DutyDuration == dto.DutyDuration)     //update the same type
                        {
                            var dailyDuty = personnelDuty as PersonnelDailyDuty;
                            dailyDuty.PersonnelId        = dto.PersonnelId;
                            dailyDuty.DutyId             = dto.DutyId;
                            dailyDuty.SubmittedDate      = DateTime.Now;
                            dailyDuty.DutyDuration       = dto.DutyDuration;
                            dailyDuty.RequestDescription = dto.RequestDescription;
                            dailyDuty.FromDate           = dto.DailyDuty.FromDate;
                            dailyDuty.ToDate             = dto.DailyDuty.ToDate;

                            from = dailyDuty.FromDate;
                            to   = dailyDuty.ToDate;
                        }
                        else     //update entity type -> remove and insert again
                        {
                            try
                            {
                                await DeleteAndInsertAgain(dto);
                            }
                            catch (TransactionAbortedException ex)
                            {
                                _logger.LogRunTimeError(ex, ex.Message
                                                        + " Either update or delete operation failed.");
                                throw;
                            }
                            catch (Exception ex)
                            {
                                _logger.LogRunTimeError(ex, ex.Message);
                                throw;
                            }
                        }
                        break;

                    case RequestDuration.Hourly:
                        if (personnelDuty.DutyDuration == dto.DutyDuration)     //update the same type
                        {
                            var hourlyDuty = personnelDuty as PersonnelHourlyDuty;
                            hourlyDuty.PersonnelId        = dto.PersonnelId;
                            hourlyDuty.DutyId             = dto.DutyId;
                            hourlyDuty.SubmittedDate      = DateTime.Now;
                            hourlyDuty.DutyDuration       = dto.DutyDuration;
                            hourlyDuty.RequestDescription = dto.RequestDescription;
                            hourlyDuty.Date     = dto.HourlyDuty.Date;
                            hourlyDuty.FromTime = dto.HourlyDuty.FromTime;
                            hourlyDuty.ToTime   = dto.HourlyDuty.ToTime;

                            from = hourlyDuty.Date.Add(hourlyDuty.FromTime);
                            to   = hourlyDuty.Date.Add(hourlyDuty.ToTime);
                        }
                        else     //update entity type -> remove and insert again
                        {
                            try
                            {
                                await DeleteAndInsertAgain(dto);
                            }
                            catch (TransactionAbortedException ex)
                            {
                                _logger.LogRunTimeError(ex, ex.Message
                                                        + " Either update or delete operation failed.");
                                throw;
                            }
                            catch (Exception ex)
                            {
                                _logger.LogRunTimeError(ex, ex.Message);
                                throw;
                            }
                        }
                        break;

                    default:
                        break;
                    }

                    //Send Request Message to Approval Proc
                    //remove prev
                    _messageService.RemoveRequest(RequestType.Duty, personnelDuty.Id);
                    //insert new
                    var notificationReceiverId = await _requestMessageHandlerService
                                                 .HandleDutyRequest(personnelDuty.Id, personnelDuty.DutyDuration
                                                                    , from, to, user.Username);

                    if (notificationReceiverId == null)
                    {
                        return(new CustomResult <string>
                        {
                            Message = "approval procedure not found"
                        });
                    }

                    return(new CustomResult <string>
                    {
                        IsValid = true,
                        ReturnId = notificationReceiverId
                    });
                }
                else
                {
                    string requestAction = personnelDuty.RequestAction == RequestAction.Accept
                        ? "confirmed" : personnelDuty.RequestAction == RequestAction.PartialAccept
                        ? "waiting for confirmation" : "rejected";
                    return(new CustomResult <string>
                    {
                        IsValid = false,
                        Message = "cannot update the duty info, status: "
                                  + requestAction
                    });
                }
            }
            else
            {
                try
                {
                    throw new LogicalException();
                }
                catch (LogicalException ex)
                {
                    _logger.LogLogicalError
                        (ex, "PersonnelDuty entity with the id: '{0}', is not available." +
                        " update operation failed.", dto.Id);
                    throw;
                }
            }
        }