public void GetAddedNotes_ReturnsNullReferenceException_WhenEitherOrBothArgumentaAreNull(List <ObservationNoteDto> notes)
        {
            // Act & Assert
            var ex = Assert.Throws <NullReferenceException>(() => ObservationNotesHelper.GetAddedNotes(notes));

            Assert.Equal("The notes collection is null", ex.Message);
        }
        public void GeAddedNotes_ReturnsZeroResult_WhenAppropriate(List <ObservationNoteDto> notes)
        {
            //Act
            var result = ObservationNotesHelper.GetAddedNotes(notes);

            // Assert
            Assert.IsAssignableFrom <IEnumerable <ObservationNoteDto> >(result);
            Assert.Empty(result);
        }
        public void GetAddedNotes_ReturnsMultipleResults_WhenAppropriate(List <ObservationNoteDto> notes)
        {
            //Act
            var result = ObservationNotesHelper.GetAddedNotes(notes);

            // Assert
            Assert.IsAssignableFrom <IEnumerable <ObservationNoteDto> >(result);
            Assert.Equal(2, result.Count());
            var deleted1 = result.Where(t => t.Id == 0).FirstOrDefault();

            Assert.Contains(deleted1, result);

            var deleted2 = result.Where(t => t.Id == 0).FirstOrDefault();

            Assert.Contains(deleted2, result);
        }
Example #4
0
        public async Task <IActionResult> PutObservationAsync(int id, ObservationEditDto model)
        {
            try
            {
                if (id != model.ObservationId)
                {
                    _logger.LogError(LoggingEvents.UpdateItem, $"Id '{id}' not equal to model id '{model.ObservationId}'");
                    return(BadRequest("An error occurred (id)"));
                }

                var observation = await _observationRepository.GetObservationAsync(id, false);

                if (observation is null)
                {
                    string message = $"observation with id '{model.ObservationId}' was not found.";
                    _logger.LogError(LoggingEvents.UpdateItem, message);
                    return(StatusCode(500, message));
                }

                var username = User.Identity.Name;

                if (username != observation.ApplicationUser.UserName)
                {
                    return(Unauthorized("Requesting user is not allowed to edit this observation"));
                }

                // 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();

                _mapper.Map <ObservationEditDto, Observation>(model, observation);

                var bird = await _birdRepository.GetBirdAsync(model.Bird.BirdId);

                if (bird is null)
                {
                    string message = $"The observed bird could not be found for observation with id '{model.ObservationId}'.";
                    _logger.LogError(LoggingEvents.UpdateItem, message);
                    return(StatusCode(500, message));
                }

                observation.Bird = bird;

                var position = await _observationPositionRepository.SingleOrDefaultAsync(o => o.ObservationId == observation.ObservationId);

                if (position is null)
                {
                    string message = $"The position could not be found for observation with id '{model.ObservationId}'.";
                    _logger.LogError(LoggingEvents.UpdateItem, message);
                    return(StatusCode(500, message));
                }

                position.Latitude         = model.Position.Latitude;
                position.Longitude        = model.Position.Longitude;
                position.FormattedAddress = model.Position.FormattedAddress;
                position.ShortAddress     = model.Position.ShortAddress;

                // ToDo: separate ObservationNotesController to handle this stuff.
                // ...need to redesign UI first...
                var notes = await _observationNoteRepository.FindAsync(x => x.Observation.ObservationId == id);

                var notesDeleted = ObservationNotesHelper.GetDeletedNotes(notes, model.Notes);
                if (notesDeleted.Any())
                {
                    _observationNoteRepository.RemoveRange(notesDeleted);
                }

                var notesAdded = ObservationNotesHelper.GetAddedNotes(model.Notes);
                if (notesAdded.Any())
                {
                    var added = _mapper.Map(notesAdded, new List <ObservationNote>());
                    added.ForEach(a => a.Observation = observation);
                    _observationNoteRepository.AddRange(added);
                }

                // ToDo: is the condition necessary here?
                if (notes.Any())
                {
                    _mapper.Map <List <ObservationNoteDto>, IEnumerable <ObservationNote> >(model.Notes, notes);
                }

                observation.LastUpdateDate = _systemClock.GetNow;

                if (!TryValidateModel(observation, nameof(observation)))
                {
                    _logger.LogError(LoggingEvents.UpdateItemNotFound, "Observation has an invalid model state: " + ModelStateErrorsExtensions.GetModelStateErrorMessages(ModelState), id);
                    return(StatusCode(500, "observation ModelState is invalid"));
                }

                await _unitOfWork.CompleteAsync();

                return(Ok(_mapper.Map <Observation, ObservationEditDto>(observation)));
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.UpdateItemNotFound, ex, "An error occurred updating (PUT) observation with id: {ID}", id);
                return(StatusCode(500, "an unexpected error occurred"));
            }
        }