public async Task <IActionResult> SaveCourses([FromBody] IList <Course> courses)
        {
            IActionResult result = BadRequest();

            if (courses == null)
            {
                return(result);
            }

            var allCoursesValid = courses.All(x => x != null && x.IsValid(false));

            if (ModelState.IsValid && allCoursesValid)
            {
                try
                {
                    MakeProvidersDistinctReferences(ref courses);
                    MakeRoutesDistinctReferences(ref courses);

                    AssociateWithLocations(ref courses);
                    AssociateWithSubjects(ref courses);

                    ResolveSalaryAndFeesReferences(ref courses);

                    foreach (var course in courses)
                    {
                        await _context.AddOrUpdateCourse(course);
                    }

                    _context.SaveChanges();
                    _logger.LogInformation($"Added/Updated Course successfully");

                    var exitcode = await _locationRequester.RequestLocations();

                    if (exitcode != 0)
                    {
                        _logger.LogWarning($"Failed to geocode {exitcode} location");
                    }

                    result = Ok();
                }
                // Note: if any exception is catch it course.IsValid() needs to be revisited
                catch (DbUpdateException ex)
                {
                    _logger.LogWarning(ex, "Failed to save the courses to database");
                }
                catch (Exception ex)
                {
                    // Note: notice that ef core dont respect id generation for some reason.
                    _logger.LogWarning(ex, "Failed to save the course");
                }
            }

            return(result);
        }
Beispiel #2
0
        public async Task <int> RequestLocations()
        {
            var locations = GetLocationsToGeocode();
            var totalLocationsToGeocode = GetTotalLocationsToGeocode();
            var geocoder = new TECH_DEBT__TemporarilyCopied__Geocoder(_config.ApiKey, _httpClient);
            var utcNow   = DateTime.UtcNow;

            _logger.Information($"Geocode proccessing a total of : {locations.Count()}/{totalLocationsToGeocode}");

            var failures = new Dictionary <string, string>();

            foreach (var location in locations)
            {
                // The try catch needs to be here as geocoder consume external service
                try
                {
                    var coordinates = await geocoder.ResolvePostCodeAsync(location.GeoAddress);

                    if (coordinates != null)
                    {
                        location.FormattedAddress = coordinates.FormattedLocation;
                        location.Longitude        = coordinates.Longitude;
                        location.Latitude         = coordinates.Latitude;
                        location.LastGeocodedUtc  = utcNow;
                    }
                    else
                    {
                        failures.Add(location.Id.ToString(), location.GeoAddress);
                    }
                }
                catch (Exception ex)
                {
                    new TelemetryClient().TrackException(ex,
                                                         new Dictionary <string, string> {
                        { "Type", "SearchAndCompareGeocoder" },
                        { "Message", $"Geocode unable to resolve: {location.Id}, {location.GeoAddress}" }
                    }
                                                         );
                    _logger.Error(ex, $"Geocode unable to resolve: {location.Id}, {location.GeoAddress}");
                    failures.Add(location.Id.ToString(), location.GeoAddress);
                }
            }

            if (failures.Any())
            {
                foreach (var failure in failures)
                {
                    _logger.Warning($"Geocode unable to resolve: {failure.Key}, {failure.Value}");
                }

                var msg = $"Geocode failures a total of : {failures.Count()}.{locations.Count()}";
                _logger.Warning(msg);

                new TelemetryClient().TrackTrace($"Geocode failures a total of : {failures.Count()}.{locations.Count()}", SeverityLevel.Error, failures);
            }

            _context.SaveChanges();

            return(failures.Count());
        }