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

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

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

            var guest = _shareRepo.GetGuest(currentShare.Id, Session.User.Id);

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


            var valueMap = FormDataConverter.Convert(data);

            var lat = valueMap.Get("completeLat");
            var lng = valueMap.Get("completaLng");

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


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

            double completeLat;
            double completeLng;

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

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

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

            _shareRepo.CompleteShare(currentShare.Id, Session.User.Id, completeLat, completeLng, distance);

            var host = currentShare.Host;

            var msgData = new Dictionary <string, string>
            {
                { "TYPE", "SHARE_COMPLETED" }
            };

            #pragma warning disable 4014
            FirebaseCloudMessanger.SendMessage(
                host,
                "Condivisione completata",
                Session.User + " ha completato la condivisione percorrendo " + distance / 1000.0 + " Km",
                msgData,
                "it.gruppoinfor.hometowork.SHARE_COMPLETED");
            #pragma warning restore 4014


            _userRepo.AddExpToUser(guest.User.Id, guest.Distance / 100);

            var completedShare = _shareRepo.GetGuest(currentShare.Id, Session.User.Id);

            return(Ok(completedShare));
        }