Ejemplo n.º 1
0
        public Location Update(Location location, LocationFormModel model)
        {
            Mapper.Map(model, location);

            Update(location);
            SaveChanges();

            return(location);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Create([FromBody] LocationFormModel model)
        {
            if (!Roles.IsLibrarian(User.Identity.Name) && !Roles.IsAdmin(User.Identity.Name))
            {
                return(Forbid());
            }

            var entity = Mapper.Map <Location>(model);

            Locations.Create(entity);

            return(CreatedAtAction(nameof(Fetch), new { locationId = entity.LocationId }, Mapper.Map <LocationFormModel>(entity)));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> Update([FromBody] LocationFormModel model, int locationId)
        {
            if (!Roles.IsLibrarian(User.Identity.Name) && !Roles.IsAdmin(User.Identity.Name))
            {
                return(Forbid());
            }

            var entity = Locations.GetLocation(locationId);

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

            Locations.Update(entity, model);

            return(Accepted());
        }
        public async Task <IActionResult> Post([FromBody] LocationFormModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            user = new User();

            if (!ModelState.IsValid || model == null || user == null)
            {
                return(BadRequest());
            }

            // https://maps.google.com/maps/api/geocode/json?latlng=x,y&sensor=false&key=AIzaSyCFcgapJG17nwAFC0Yohs0x6Z9IsBwclq4

            var apiKey  = _config["GoogleMaps:ApiKey"];
            var request = new HttpRequestMessage(HttpMethod.Get,
                                                 $"https://maps.google.com/maps/api/geocode/json?latlng={model.Latitude.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)}," +
                                                 $"{model.Longitude.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)}&sensor=false&key=AIzaSyCFcgapJG17nwAFC0Yohs0x6Z9IsBwclq4");

            var client   = _clientFactory.CreateClient();
            var response = await client.SendAsync(request);

            var parsedJson = JObject.Parse(await response.Content.ReadAsStringAsync());

            var googleResponse = parsedJson.ToObject <GoogleFormModel>();

            Prediction pred = new Prediction()
            {
                CreationDate = DateTime.Now,
                User         = null,
                Class        = model.Class,
                Latitude     = model.Latitude,
                Longitude    = model.Longitude,
                LocationName = googleResponse?.results?.FirstOrDefault()?.formatted_address ?? "Unknown"
            };


            _appDbContext.Add(pred);

            await _appDbContext.SaveChangesAsync();

            return(Ok());
        }