Exemplo n.º 1
0
        public IHttpActionResult PostNewShare(FormDataCollection data)
        {
            if (!Session.Authorized)
            {
                return(Unauthorized());
            }

            // Controllo se l'utente non ha già una condivisione in corso, nel caso la restituisco
            var share = _shareRepo.GetUserActiveShare(Session.User.Id);

            if (share != null && share.Host.Id == Session.User.Id)
            {
                return(Ok(share));
            }

            // Altrimenti ne creo una nuova
            var valueMap = FormDataConverter.Convert(data);

            var lat = valueMap.Get("startLat");
            var lng = valueMap.Get("startLng");

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


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

            double startLat;
            double startLng;

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

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


            var shareId = _shareRepo.CreateShare(Session.User.Id, startLat, startLng);

            share = _shareRepo.GetUserShare(Session.User.Id, shareId);

            return(Ok(share));
        }
Exemplo n.º 2
0
        public IHttpActionResult PostNewChat(FormDataCollection data)
        {
            if (!Session.Authorized)
            {
                return(Unauthorized());
            }

            var valueMap = FormDataConverter.Convert(data);

            var recipientId = valueMap.Get("recipientId").AsInt();

            if (recipientId == Session.User.Id)
            {
                return(BadRequest("L'id del destinatario è uguale all'id del mittente"));
            }

            // Controllo se era già stata creata una chat tra i due utenti
            var chat = _chatRepo.GetByUserIds(Session.User.Id, recipientId);

            if (chat != null)
            {
                return(Ok(chat));
            }

            // Se non esiste ne creo una nuova

            var newChatId = _chatRepo.NewChat(Session.User.Id, recipientId);

            chat = _chatRepo.GetByChatId(Session.User.Id, newChatId);

            return(Ok(chat));
        }
Exemplo n.º 3
0
        public IHttpActionResult PostBanUserFromShare(FormDataCollection data)
        {
            if (!Session.Authorized)
            {
                return(Unauthorized());
            }

            var valueMap = FormDataConverter.Convert(data);
            var guestId  = valueMap.Get("guestId").AsInt();

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

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

            var shareGuest = _shareRepo.GetGuest(share.Id, guestId);

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

            if (shareGuest.CurrentStatus == Guest.Status.Leaved)
            {
                return(NotFound());
            }

            shareGuest.CurrentStatus = Guest.Status.Leaved;

            _shareRepo.LeaveShare(share.Id, shareGuest.User.Id);

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

            #pragma warning disable 4014
            FirebaseCloudMessanger.SendMessage(
                shareGuest.User,
                "Sei stato espulso", Session.User + " ti ha espulso dalla condivisione in corso",
                msgData,
                "it.gruppoinfor.hometowork.SHARE_BAN");
            #pragma warning restore 4014

            return(Ok(true));
        }
Exemplo n.º 4
0
        public IHttpActionResult PostMessage(int chatId, FormDataCollection data)
        {
            if (!Session.Authorized)
            {
                return(Unauthorized());
            }

            var chat = _chatRepo.GetByChatId(Session.User.Id, chatId);

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

            var valueMap = FormDataConverter.Convert(data);
            var text     = valueMap.Get("text");

            if (text.IsNullOrWhiteSpace())
            {
                return(BadRequest("Corpo messaggio mancante o vuoto"));
            }

            var messageId = _chatRepo.InsertMessage(Session.User.Id, chatId, text);

            var msgData = new Dictionary <string, string>
            {
                { "TYPE", "NEW_MESSAGE" },
                { "CHAT_ID", chatId.ToString() },
                { "MESSAGE_ID", messageId.ToString() }
            };

            #pragma warning disable 4014
            FirebaseCloudMessanger.SendMessage(
                chat.User,
                "Nuovo messaggio", Session.User + " ti ha inviato un nuovo messaggio",
                msgData,
                "it.gruppoinfor.hometowork.NEW_MESSAGE");
            #pragma warning restore 4014


            var message = _chatRepo.getMessageById(messageId);

            return(Ok(message));
        }
Exemplo n.º 5
0
        public IHttpActionResult PostFcmToken(FormDataCollection data)
        {
            if (!Session.Authorized)
            {
                return(Unauthorized());
            }

            var valueMap = FormDataConverter.Convert(data);
            var newToken = valueMap.Get("token");

            if (newToken.IsNullOrWhiteSpace())
            {
                return(BadRequest("Token Firebase Cloud Messaging mancante"));
            }

            var updated = _userRepo.SetUserFirebaseToken(Session.User.Id, newToken);

            return(Ok(updated));
        }
Exemplo n.º 6
0
        public HttpResponseMessage Login(FormDataCollection data)
        {
            var valueMap = FormDataConverter.Convert(data);

            var email = valueMap.Get("email");

            if (email.IsNullOrWhiteSpace())
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Email non presente"));
            }

            if (!StringUtils.ValidateEmail(email))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Email non valida. Formato non corretto"));
            }

            var password = valueMap.Get("password");

            if (password.IsNullOrWhiteSpace())
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Password non presente"));
            }


            var salt         = _userRepo.GetUserSalt(email);
            var passwordHash = HashingUtils.Sha256(password + salt);

            var user = _userRepo.Login(email, passwordHash);

            if (user == null)
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized,
                                              "Nessun utente trovato con le credenziali immesse"));
            }

            var sessionToken = _userRepo.NewSessionToken(user.Id);

            var response = Request.CreateResponse(HttpStatusCode.OK, user);

            response.Headers.Add("X-User-Session-Token", sessionToken);
            return(response);
        }
Exemplo n.º 7
0
        public IHttpActionResult PostStatus(FormDataCollection data)
        {
            if (!Session.Authorized)
            {
                return(Unauthorized());
            }

            var valueMap = FormDataConverter.Convert(data);

            var status = valueMap.Get("status");

            if (status.IsNullOrWhiteSpace())
            {
                return(BadRequest("Stato non inserito o non valido"));
            }

            var updated = _userRepo.UpdateUserStatus(Session.User.Id, status);

            return(Ok(updated));
        }
Exemplo n.º 8
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));
        }
Exemplo n.º 9
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));
        }
Exemplo n.º 10
0
        public IHttpActionResult PostJoinShare(int shareId, FormDataCollection data)
        {
            if (!Session.Authorized)
            {
                return(Unauthorized());
            }

            var valueMap = FormDataConverter.Convert(data);

            var lat = valueMap.Get("joinLat");
            var lng = valueMap.Get("joinLng");

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


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

            double joinLat;
            double joinLng;

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

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

            var share = _shareRepo.GetUserShare(Session.User.Id, shareId);

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


            var shareGuest = _shareRepo.GetGuest(shareId, Session.User.Id);

            if (shareGuest != null)
            {
                if (shareGuest.CurrentStatus == Guest.Status.Leaved)
                {
                    shareGuest.CurrentStatus = Guest.Status.Joined;
                    _shareRepo.JoinShare(shareId, shareGuest.User.Id, joinLat, joinLng);
                }
            }
            else
            {
                _shareRepo.JoinShare(shareId, Session.User.Id, joinLat, joinLng);
            }

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

            #pragma warning disable 4014
            FirebaseCloudMessanger.SendMessage(
                share.Host,
                "Nuovo ospite", Session.User + " si è unito alla condivisione in corso",
                msgData,
                "it.gruppoinfor.hometowork.SHARE_JOIN");
            #pragma warning restore 4014

            share = _shareRepo.GetUserShare(Session.User.Id, shareId);

            return(Ok(share));
        }