public CustomResult Update(UpdatePersonnelEntranceDto dto) { var personnelEntrance = _personnelEntranceRepository.GetById(dto.Id); if (personnelEntrance != null) { var exitDate = dto.ExitDate; if (personnelEntrance.Enter != dto.Enter || personnelEntrance.ExitDate != exitDate || personnelEntrance.Exit != dto.Exit) { if (exitDate.HasValue && exitDate < personnelEntrance.EnterDate.Date) { return(new CustomResult { Message = "exit date cannot be less that enter date" + $"{personnelEntrance.EnterDate.ToShortDateString()}" }); } personnelEntrance.AutoEnter = personnelEntrance.Enter != dto.Enter ? false : personnelEntrance.AutoEnter; personnelEntrance.Enter = dto.Enter; personnelEntrance.ExitDate = exitDate; personnelEntrance.AutoExit = (personnelEntrance.Exit != dto.Exit || personnelEntrance.ExitDate != exitDate) ? false : personnelEntrance.AutoExit; personnelEntrance.Exit = dto.Exit; personnelEntrance.IsCompleted = dto.Exit.HasValue ? true : false; personnelEntrance.IsEdited = true; personnelEntrance.EditDate = DateTime.Now; _personnelEntranceRepository.Update(personnelEntrance); } } else { try { throw new LogicalException(); } catch (LogicalException ex) { _logger.LogLogicalError (ex, "PersonnelEntrance entity with the id: '{0}', is not available." + " update operation failed.", dto.Id); throw; } } return(new CustomResult { IsValid = true }); }
public IHttpActionResult Update([FromBody] UpdatePersonnelEntranceDto personnelEntrance) { if (personnelEntrance == null) { return(BadRequest()); } //custom validations var validator = new UpdatePersonnelEntranceDtoValidator(); 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 = _personnelEntranceService.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()); }