public async Task <IActionResult> Create([FromForm] IncidentViewModel incidentViewModel)
        {
            if (incidentViewModel.Description.ToLower().Contains("space") &&
                incidentViewModel.Description.ToLower().Contains("invader"))
            {
                return(RedirectToAction("Index", "Invaders"));
            }

            var imageName   = $"{Guid.NewGuid().ToString()}.jpg";
            var imageStream = new MemoryStream();

            incidentViewModel.File.CopyTo(imageStream);
            imageStream.Position = 0;

            var newIncidentInput = new CreateIncidentInput
            {
                Description = incidentViewModel.Description,
                Longitude   = incidentViewModel.Longitude,
                Latitude    = incidentViewModel.Latitude,
                Time        = DateTime.Now,
                ImageName   = imageName,
                ImageBytes  = ReadFully(imageStream)
            };

            _incidentAppService.RecordNewIncident(newIncidentInput);
            await _imageStorageService.UploadImageBytes(imageName, imageStream.GetAllBytes());

            return(View("Index"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> RecordIncident([FromBody] IncidentModel incident)
        {
            var imageName  = $"{Guid.NewGuid().ToString()}.jpg";
            var imageBytes = Convert.FromBase64String(incident.Image);

            await _imageStorageService.UploadImageBytes(imageName, imageBytes);

            var newIncidentInput = new CreateIncidentInput
            {
                Description = incident.Description,
                Longitude   = incident.Position.Longitude,
                Latitude    = incident.Position.Latitude,
                Time        = DateTime.Now,
                ImageName   = imageName,
                ImageBytes  = imageBytes
            };

            _incidentAppService.RecordNewIncident(newIncidentInput);
            return(Ok());
        }
        public bool RecordNewIncident(CreateIncidentInput incidentRequest)
        {
            var incident = _objectMapper.Map <Incident>(incidentRequest);

            incident.PositivitySentimentScore = _languageAnalysys.GetSentimentScore(incident.Description);

            var phrases      = _languageAnalysys.GetKeyPrases(incident.Description);
            var incidentTags = (from phrase in phrases
                                let databasePhrase = _tagRepository.GetAll().FirstOrDefault(tag => tag.Name == phrase)
                                                     select databasePhrase == null
                    ? _tagRepository.InsertAndGetId(new Tag {
                Name = phrase
            })
                    : databasePhrase.Id).ToList();

            var imageTags = _imageAnalysisService.AnalyzeImage(incidentRequest.ImageBytes).Result.Description.Tags;

            incidentTags.AddRange((from imageTag in imageTags
                                   let databasePhrase = _tagRepository.GetAll().FirstOrDefault(tag => tag.Name == imageTag)
                                                        select databasePhrase == null
                    ? _tagRepository.InsertAndGetId(new Tag {
                Name = imageTag
            })
                    : databasePhrase.Id).ToList());

            incident.ImageId = _imageRepository.InsertAndGetId(new Image {
                ImagePath = incidentRequest.ImageName
            });
            var incidentId = _incidentRepository.InsertAndGetId(incident);

            foreach (var incidentTag in incidentTags)
            {
                _incidentTagRepository.Insert(new IncidentTag {
                    IncidentId = incidentId, TagId = incidentTag
                });
            }
            return(true);
        }