Esempio n. 1
0
        public IHttpActionResult PostFinishShare(FormDataCollection data)
        {
            if (!Session.Authorized)
            {
                return(Unauthorized());
            }

            var share = _shareRepo.GetUserActiveShare(Session.User.Id);

            if (share == null)
            {
            }

            var valueMap = FormDataConverter.Convert(data);

            var lat = valueMap.Get("finishLat");
            var lng = valueMap.Get("finishLng");

            if (lat.IsNullOrWhiteSpace())
            {
                return(BadRequest("Latitudine finale mancante"));
            }


            if (lng.IsNullOrWhiteSpace())
            {
                return(BadRequest("Longitudine finale mancante"));
            }

            double finishLat;
            double finishLng;

            try
            {
                finishLat = double.Parse(lat);
            }
            catch (Exception e)
            {
                return(BadRequest("Formato latitutudine non corretto"));
            }

            try
            {
                finishLng = double.Parse(lng);
            }
            catch (Exception e)
            {
                return(BadRequest("Formato longitudine non corretto"));
            }



            if (share.Host.Id != Session.User.Id)
            {
                return(BadRequest());
            }


            var request = new DirectionsRequest()
            {
                Origin      = new GoogleApi.Entities.Common.Location(share.StartLat, share.StartLng),
                Destination = new GoogleApi.Entities.Common.Location(finishLat, finishLng),
                Key         = "AIzaSyDrnpGbytKl9jFKNTmI3B2vXh_68x3DG2Y"
            };
            var result   = GoogleMaps.Directions.Query(request);
            var distance = result.Routes.First().Legs.First().Distance.Value;

            _shareRepo.FinishShare(share.Id, finishLat, finishLng, distance);

            var totalDistance = 0;

            var guests = _shareRepo.GetShareGuests(share.Id);

            foreach (var guest in guests)
            {
                totalDistance += guest.Distance;
            }

            _userRepo.AddExpToUser(share.Host.Id, totalDistance / 100);

            return(Ok(share));
        }