public IActionResult Post(int observationId, [FromBody] ObsResourceDtoForCreationAndUpdate newObsResourceDto)
        {
            ObsResource newObsResource = _mapper.Map <ObsResource>(newObsResourceDto);

            // Verify the type
            if (newObsResourceDto.Type != "sketch" && newObsResourceDto.Type != "jot" && newObsResourceDto.Type != "image" && newObsResourceDto.Type != "link")
            {
                return(BadRequest("Invalid type"));
            }

            Observation observation = _observationsRepo.GetObservationById(observationId);

            if (observation == null)
            {
                return(NotFound("Could not find the observation"));
            }

            newObsResource.ObservationId = observation.Id;

            ObsResource addedObsResource = _obsResourceRepo.AddObsResource(newObsResource);

            if (addedObsResource == null)
            {
                return(StatusCode(500, "Something went wrong creating a resource"));
            }

            _logger.LogInformation("Created a resource:");
            _logger.LogInformation(PocoPrinter.ToString(addedObsResource));

            ObsResourceDto addedObsResourceDto = _mapper.Map <ObsResourceDto>(addedObsResource);

            return(CreatedAtRoute("GetOneObsResource", new { resourceId = addedObsResourceDto.Id }, addedObsResourceDto));
        }
Beispiel #2
0
        public IActionResult Post([FromBody] ObsSessionDtoForCreation newObsSessionDto)
        {
            ObsSession obsSession = _mapper.Map <ObsSession>(newObsSessionDto);

            // Lookup and verify the location id
            if (newObsSessionDto.LocationId != null)
            {
                Location locationEntity = _locationsRepository.GetLocation(newObsSessionDto.LocationId ?? 0);
                if (locationEntity == null)
                {
                    return(BadRequest("Invalid LocationId"));
                }
                obsSession.Location = locationEntity;
            }

            ObsSession addedObsSession = _obsSessionsRepository.AddObsSession(obsSession);

            if (addedObsSession == null)
            {
                return(StatusCode(500, "Something went wrong creating an observation session"));
            }

            _mainDbContext.SaveChanges();

            _reportTextManager.ParseAndStoreObservations(addedObsSession);

            _logger.LogInformation("Created an observation session:");
            _logger.LogInformation(PocoPrinter.ToString(addedObsSession));

            ObsSessionDto addedObsSessionDto = _mapper.Map <ObsSessionDto>(addedObsSession);

            return(CreatedAtRoute("GetOneObsSession", new { id = addedObsSessionDto.Id }, addedObsSessionDto));
        }
Beispiel #3
0
        public void testPrintPoco()
        {
            ObsSession obsSession = new ObsSession
            {
                Id       = 5,
                Date     = DateTime.Now,
                Location = new Location
                {
                    Id   = 6,
                    Name = "Some location",
                    GoogleMapsAddress = "Some address"
                },
                LocationId = 10,
                Title      = "A great title",
                Summary    = "Summary Summary Summary Summary Summary Summary Summary Summary Summary Summary "
                             + "Summary Summary Summary Summary Summary Summary Summary Summary Summary Summary "
                             + "Summary Summary Summary Summary Summary Summary Summary Summary Summary Summary "
                             + "Summary Summary Summary Summary Summary Summary Summary Summary Summary Summary "
                             + "Summary Summary Summary Summary Summary Summary Summary Summary Summary Summary "
                             + "Summary Summary Summary Summary Summary Summary Summary Summary Summary Summary ",
                ReportText = "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text "
                             + "Report text Report text Report text Report text Report text Report text Report text ",
            };

            string text = PocoPrinter.ToString(obsSession);

            Console.WriteLine(text);
        }
Beispiel #4
0
        public IActionResult Put(int id, [FromBody] ObsSessionDtoForUpdate obsSessionDtoForUpdate)
        {
            if (obsSessionDtoForUpdate == null)
            {
                return(BadRequest());
            }

            ObsSession obsSessionEntity = _obsSessionsRepository.GetObsSession(id, true, true, true);

            if (obsSessionEntity == null)
            {
                return(NotFound());
            }

            // Lookup and verify the location id
            if (obsSessionDtoForUpdate.LocationId != null)
            {
                Location locationEntity = _locationsRepository.GetLocation(obsSessionDtoForUpdate.LocationId ?? 0);
                if (locationEntity == null)
                {
                    return(NotFound($"Invalid LocationId {obsSessionDtoForUpdate.LocationId}"));
                }
                obsSessionEntity.Location = locationEntity;
            }

            _mapper.Map(obsSessionDtoForUpdate, obsSessionEntity);

            _reportTextManager.ParseAndStoreObservations(obsSessionEntity);

            var result = _obsSessionsRepository.SaveChanges();

            if (!result)
            {
                return(StatusCode(500, "Something went wrong updating the observation session"));
            }

            _logger.LogInformation("Updated an observation session:");
            _logger.LogInformation(PocoPrinter.ToString(obsSessionEntity));

            ObsSession freshObsSessionEntity = _obsSessionsRepository.GetObsSession(id, true, true, true);
            var        resultingDto          = _mapper.Map <ObsSessionDto>(freshObsSessionEntity);

            return(Ok(resultingDto));
        }
        public IActionResult Delete(int id)
        {
            ObsResource obsResourceEntity = _obsResourceRepo.GetOneResource(id);

            if (obsResourceEntity == null)
            {
                return(NotFound());
            }

            bool result = _obsResourceRepo.DeleteObsResource(obsResourceEntity);

            if (!result)
            {
                return(StatusCode(500, "Something went wrong deleting the resource"));
            }

            _logger.LogInformation("Deleted a resource:");
            _logger.LogInformation(PocoPrinter.ToString(obsResourceEntity));

            return(Ok());
        }
Beispiel #6
0
        public IActionResult Delete(int id)
        {
            ObsSession obsSessionEntity = _obsSessionsRepository.GetObsSession(id);

            if (obsSessionEntity == null)
            {
                return(NotFound());
            }


            bool result = _obsSessionsRepository.DeleteObsSession(obsSessionEntity);

            if (!result)
            {
                return(StatusCode(500, "Something went wrong deleting the observation session"));
            }

            _logger.LogInformation("Deleted an observation session:");
            _logger.LogInformation(PocoPrinter.ToString(obsSessionEntity));

            return(Ok());
        }
        public IActionResult Put(int resourceId, [FromBody] ObsResourceDtoForCreationAndUpdate obsResourceDtoForUpdate)
        {
            if (obsResourceDtoForUpdate == null)
            {
                return(BadRequest());
            }

            ObsResource obsResourceEntity = _obsResourceRepo.GetOneResource(resourceId);

            if (obsResourceEntity == null)
            {
                return(NotFound());
            }

            // Verify the type
            if (obsResourceDtoForUpdate.Type != "sketch" &&
                obsResourceDtoForUpdate.Type != "jot" &&
                obsResourceDtoForUpdate.Type != "image" &&
                obsResourceDtoForUpdate.Type != "link" &&
                obsResourceDtoForUpdate.Type != "aladin")
            {
                return(BadRequest("Invalid type"));
            }

            _mapper.Map(obsResourceDtoForUpdate, obsResourceEntity);

            _obsResourceRepo.SaveChanges();

            _logger.LogInformation("Updated a resource:");
            _logger.LogInformation(PocoPrinter.ToString(obsResourceEntity));

            ObsResource freshObsResourceEntity = _obsResourceRepo.GetOneResource(resourceId);
            var         resultingDto           = _mapper.Map <ObsResourceDto>(freshObsResourceEntity);

            return(Ok(resultingDto));
        }
        public void ParseAndStoreObservations(ObsSession obsSession)
        {
            // Parse
            IDictionary <string, Observation> updatedObservations = Parse(obsSession);

            // All returned observations are to be either updated or added, that is decided further down
            // by looking at the currently stored observations.

            // Replace list of Observations on the ObsSession
            _dbContext.Entry(obsSession).Collection("Observations").Load();

            List <Observation> observationsToDelete = obsSession.Observations
                                                      .Where(oldObs => !updatedObservations.ContainsKey(oldObs.Identifier)) // those where there is (not any/no) match in the updatedObservations list
                                                      .ToList();

            // Find out which observations to delete (and not just update)
            foreach (Observation observationToDelete in observationsToDelete)
            {
                // Log them if they have resources on them
                _dbContext.Entry(observationToDelete).Collection("ObsResources").Load();
                if (observationToDelete.ObsResources.Count > 0)
                {
                    _logger.LogInformation("Implicitly (under the hood) deleting an observation containing the following resources:");
                    foreach (ObsResource obsResource in observationToDelete.ObsResources)
                    {
                        _logger.LogInformation(PocoPrinter.ToString(obsResource));
                    }
                }
                // we never delete the ObsResources   // doesn't seem needed, just loading them

                // Load the Observations' DsoObservations and remove them
                _dbContext.Entry(observationToDelete).Collection("DsoObservations").Load();
                //observationToDelete.DsoObservations.RemoveAll(dsoObs => true);  // doesn't seam needed, just loading them

                // Then delete them
                Debug.WriteLine("Deleting observation with identifier " + observationToDelete.Identifier);
                obsSession.Observations.Remove(observationToDelete);
            }

            // First off, start by creating a dictionary of the existing observations for easier lookup
            IDictionary <string, Observation> existingObservations = new Dictionary <string, Observation>();

            foreach (Observation existingObservation in obsSession.Observations)
            {
                existingObservations.Add(existingObservation.Identifier, existingObservation);
            }

            // Then go through observations that already existed, and that should be updated with the new data
            foreach (Observation existingObservation in obsSession.Observations)
            {
                if (updatedObservations.ContainsKey(existingObservation.Identifier))
                {
                    Observation updatedObservation = updatedObservations[existingObservation.Identifier];

                    // Transfer/update the observation
                    existingObservation.Text         = updatedObservation.Text;
                    existingObservation.DisplayOrder = updatedObservation.DisplayOrder;
                    existingObservation.NonDetection = updatedObservation.NonDetection;

                    _dbContext.Entry(existingObservation).Collection("DsoObservations").Load();
                    _dbContext.Entry(existingObservation).Collection("ObsResources").Load();

                    // Set the existing observation id or we get duplicate errors etc when persisting.
                    // During the Parse() stage above we are not aware of the existing observations' ids.
                    foreach (DsoObservation dsoObservation in updatedObservation.DsoObservations)
                    {
                        dsoObservation.ObservationId = existingObservation.Id;
                    }

                    UpdateDsoObservations(existingObservation, updatedObservation);
                    AddNewObsResources(existingObservation, updatedObservation);
                }
            }

            // Finally, add those observations that are new, that didn't exist before
            foreach (Observation updatedObservation in updatedObservations.Values)
            {
                if (!existingObservations.ContainsKey(updatedObservation.Identifier)) // it doesn't exists, add it!
                {
                    var newObservation = updatedObservation;                          // just to clearly indicate that it's a new one
                    // New obs resources will automatically tag along in this situation and get created.
                    Debug.WriteLine("Adding new observation for observation with Identifier " + newObservation.Identifier);
                    obsSession.Observations.Add(newObservation);

                    // Any obs resources get automatically created.
                }
            }

            SaveChanges();
        }