public async Task <HistoricalEntity> Insert(HistoricalInputEntity historicalInput) { if (historicalInput.Occurrence == DateTime.MinValue) { throw new ApiException(HttpStatusCode.BadRequest, HistoricalsMessages.InvalidOccurrence(historicalInput.Occurrence)); } var(leader, led) = await employeesService.ObtainPair(historicalInput?.LeaderId, historicalInput?.LedId); var historicalList = await historicalsRepository.ObtainByPair(historicalInput.LeaderId, historicalInput.LedId); var occurrenceObtained = historicalList?.FirstOrDefault(historical => historical.Occurrence.Date == historicalInput.Occurrence.Date); if (occurrenceObtained != null) { throw new ApiException(HttpStatusCode.Conflict, HistoricalsMessages.Conflict(leader.Email, led.Email, historicalInput.Occurrence)); } _ = await oneononesService.ObtainByPair(leader.Id, led.Id); var historical = new HistoricalEntity(leader, led, historicalInput.Occurrence, historicalInput.Commentary); var inserted = await historicalsRepository.Insert(historical); if (!inserted) { throw new ApiException(HttpStatusCode.InternalServerError, HistoricalsMessages.Insert(historical.Leader.Email, historical.Led.Email, historical.Occurrence)); } return(historical); }
public static HistoricalInputEntity ToEntity(this HistoricalInputViewModel viewModel) { if (viewModel == null) { return(null); } var entity = new HistoricalInputEntity { LeaderId = viewModel.LeaderId, LedId = viewModel.LedId, Occurrence = viewModel.Occurrence, Commentary = viewModel.Commentary, }; return(entity); }