public async Task <IActionResult> CreateObservationAsync(ObservationAddDto model) { try { var requestingUser = await _userManager.FindByNameAsync(User.Identity.Name); if (requestingUser is null) { _logger.LogError(LoggingEvents.GetItem, "requesting user not found"); return(StatusCode(500, "requesting user not found")); } var observedBirdSpecies = await _birdRepository.GetBirdAsync(model.Bird.BirdId); if (observedBirdSpecies is null) { string message = $"Bird species with id '{model.BirdId}' was not found."; _logger.LogError(LoggingEvents.GetItem, message); return(StatusCode(500, message)); } // date from client model is already Utc.... // model.ObservationDateTime = model.ObservationDateTime.ToLocalTime(); // var x = model.ObservationDateTime; // var y = model.ObservationDateTime = model.ObservationDateTime.ToLocalTime(); // var z = model.ObservationDateTime = model.ObservationDateTime.ToUniversalTime(); var observation = _mapper.Map <ObservationAddDto, Observation>(model); DateTime createdDate = _systemClock.GetNow; observation.ApplicationUser = requestingUser; observation.Bird = observedBirdSpecies; observation.CreationDate = createdDate; observation.LastUpdateDate = createdDate; //rerun validation on observation model if (!TryValidateModel(observation, nameof(observation))) { _logger.LogError(LoggingEvents.UpdateItem, "Observation model state is invalid: " + ModelStateErrorsExtensions.GetModelStateErrorMessages(ModelState)); return(StatusCode(500, "observation ModelState is invalid")); } _observationPositionRepository.Add(observation.Position); _observationRepository.Add(observation); _observationNoteRepository.AddRange(observation.Notes); await _unitOfWork.CompleteAsync(); return(CreatedAtAction(nameof(CreateObservationAsync), _mapper.Map <Observation, ObservationDto>(observation))); } catch (Exception ex) { _logger.LogError(LoggingEvents.GetListNotFound, ex, "An error occurred creating an observation."); return(StatusCode(500, "an unexpected error occurred")); } }
public void Add(Observation observation) { _repository.Add(observation); _uow.Commit(); }