예제 #1
0
        public CustomResult Update(UpdatePersonnelDutyEntranceDto dto)
        {
            var personnelDutyEntrance = _personnelDutyEntranceRepository.GetById(dto.Id);

            if (personnelDutyEntrance != null)
            {
                var endDate = dto.EndDate;

                if (personnelDutyEntrance.Start != dto.Start ||
                    personnelDutyEntrance.EndDate != endDate ||
                    personnelDutyEntrance.End != dto.End)
                {
                    if (endDate.HasValue &&
                        endDate < personnelDutyEntrance.StartDate.Date)
                    {
                        return(new CustomResult
                        {
                            Message = "end date cannot be less that start date" +
                                      $"{personnelDutyEntrance.StartDate.ToShortDateString()}"
                        });
                    }

                    personnelDutyEntrance.Start       = dto.Start;
                    personnelDutyEntrance.EndDate     = endDate;
                    personnelDutyEntrance.End         = dto.End;
                    personnelDutyEntrance.IsCompleted = dto.End.HasValue;

                    _personnelDutyEntranceRepository.Update(personnelDutyEntrance);
                }
            }
            else
            {
                try
                {
                    throw new LogicalException();
                }
                catch (LogicalException ex)
                {
                    _logger.LogLogicalError
                        (ex, "PersonnelDutyEntrance entity with the id: '{0}', is not available." +
                        " update operation failed.", dto.Id);
                    throw;
                }
            }

            return(new CustomResult
            {
                IsValid = true
            });
        }
        public IHttpActionResult Update([FromBody] UpdatePersonnelDutyEntranceDto personnelEntrance)
        {
            if (personnelEntrance == null)
            {
                return(BadRequest());
            }
            //custom validations
            var validator = new UpdatePersonnelDutyEntranceDtoValidator();
            var results   = validator.Validate(personnelEntrance);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    ModelState.AddModelError(failure.PropertyName, failure.ErrorMessage);
                }
            }
            if (!ModelState.IsValid)
            {
                string errorMessage = new ModelStateError(_logger).OutputMessage(ModelState);
                return(BadRequest(errorMessage));
            }
            try
            {
                var result = _personnelDutyEntranceService.Update(personnelEntrance);
                if (!result.IsValid)
                {
                    return(BadRequest(result.Message));
                }
            }
            catch (LogicalException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch
            {
                return(BadRequest(AppSettings.INTERNAL_SERVER_ERROR_MESSAGE));
            }
            return(Ok());
        }