public void SaveSuggestion(TouristAttractionSuggestion suggestion)
 {
     if (suggestion.Id == 0)
     {
         context.TouristAttractionSuggestions.Add(suggestion);
     }
     context.SaveChanges();
 }
        public void Reject(int suggestionId, int rejectorId)
        {
            var suggestion = new TouristAttractionSuggestion {
                Id = suggestionId, Approved = false, AnsweredBy = rejectorId, AnsweredDate = DateTime.UtcNow
            };

            context.TouristAttractionSuggestions.Attach(suggestion);
            context.Entry(suggestion).Property(s => s.AnsweredBy).IsModified = true;
            context.Entry(suggestion).Property(s => s.Approved).IsModified   = true;

            context.Entry(suggestion).Property(s => s.AnsweredDate).IsModified = true;
            context.SaveChanges();
        }
        public IActionResult Approve(TouristAttractionSuggestion suggestion)
        {
            if (_suggestionRepository.IsExistingAttraction(suggestion.Id))
            {
                ModelState.AddModelError("existingAttraction", "Ya existe una atracción turística con la información provista en la sugerencia, se recomienda rechazarla");
                return(View(nameof(Approval), suggestion));
            }
            int approverId = Convert.ToInt32(User.Claims.Where(c => c.Type == ClaimTypes.NameIdentifier).First().Value);

            _suggestionRepository.Approve(suggestion, approverId);
            TempData["approved"] = true;
            return(RedirectToAction(nameof(Search), new { status = TempData["status"] }));
        }
        public ActionResult Save([FromBody] TouristAttractionSuggestion suggestion)
        {
            if (suggestion.Name == null ||
                suggestion.Address == null ||
                suggestion.Geoposition == null ||
                suggestion.GooglePlaceId == null ||
                suggestion.CategoryId == 0 ||
                suggestion.CityId == 0)
            {
                return(Ok(new { isSuccessful = false, message = "Datos incompletos de la atracción turística" }));
            }
            if (_touristAttractionSuggestionRepository.IsAttractionRegistered(suggestion.GooglePlaceId))
            {
                return(Ok(new { isSuccessful = false, message = "Ya existe una atracción turística registrada con la información proporcionada" }));
            }
            _touristAttractionSuggestionRepository.SaveSuggestion(suggestion);
            var notificationEmails = _administratorRepository.GetNotificationEmails();

            _mailer.Send(notificationEmails.ToList(), "Nueva sugerencia registrada", $"Se registró una nueva sugerencia de atracción turística para la atracción '{suggestion.Name}'.");
            return(Ok(new { isSuccessful = true, message = "Sugerencia registrada exitosamente" }));
        }
        public void Approve(TouristAttractionSuggestion suggestion, int approverId)
        {
            suggestion.Approved     = true;
            suggestion.AnsweredBy   = approverId;
            suggestion.AnsweredDate = DateTime.UtcNow;
            context.TouristAttractionSuggestions.Attach(suggestion);
            context.Entry(suggestion).Property(s => s.Rating).IsModified      = true;
            context.Entry(suggestion).Property(s => s.WebsiteUrl).IsModified  = true;
            context.Entry(suggestion).Property(s => s.AnsweredBy).IsModified  = true;
            context.Entry(suggestion).Property(s => s.Approved).IsModified    = true;
            context.Entry(suggestion).Property(s => s.PhoneNumber).IsModified = true;

            context.Entry(suggestion).Property(s => s.AnsweredDate).IsModified = true;

            //Creation of new touristic attraction
            var newAttraction = context.TouristAttractionSuggestions
                                .Where(s => s.Id == suggestion.Id)
                                .Select(s => new TouristAttraction
            {
                CreatedBy   = approverId,
                Active      = true,
                Address     = s.Address,
                CategoryId  = s.CategoryId,
                CityId      = s.CityId,
                Geoposition = new Geoposition {
                    Latitude = s.Geoposition.Latitude, Longitude = s.Geoposition.Longitude
                },
                GooglePlaceId = s.GooglePlaceId,
                Name          = s.Name,
                PhoneNumber   = suggestion.PhoneNumber,
                Rating        = suggestion.Rating,
                WebsiteUrl    = suggestion.WebsiteUrl
            }).First();

            context.TouristAttractions.Add(newAttraction);
            context.SaveChanges();
            //After save it will be needed to recreate the matrix
            _touristAttractionConnectionRepository.RecalculateConnections(newAttraction.Id);
        }