Exemplo n.º 1
0
        public async Task <IHttpActionResult> Register([FromBody] FacebookAccount i_Account)
        {
            using (CarWatchDBEntities entities = new CarWatchDBEntities())
            {
                var isExistNickname = await entities.FacebookAccounts.AnyAsync(e => e.Nickname == i_Account.Nickname);

                if (isExistNickname)
                {
                    return(BadRequest("nameinuse"));
                }

                if (i_Account.LicensePlate != null)
                {
                    var isExistLicensePlate = await entities.FacebookAccounts.AnyAsync(e => e.LicensePlate == i_Account.LicensePlate);

                    if (isExistLicensePlate)
                    {
                        return(BadRequest("licenseinuse"));
                    }
                }

                entities.FacebookAccounts.Add(i_Account);
                await entities.SaveChangesAsync();

                return(Ok());
            }
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> SendByNickname([FromBody] Message i_Message)
        {
            string nickname = Thread.CurrentPrincipal.Identity.Name;

            if (i_Message.Sender != nickname)
            {
                return(BadRequest("Nicknames do not match."));
            }

            using (CarWatchDBEntities entities = new CarWatchDBEntities())
            {
                FacebookAccount account = await entities.FacebookAccounts.FirstOrDefaultAsync(e => e.Nickname == i_Message.Receiver);

                if (account == null)
                {
                    return(BadRequest("The receiver nickname was not found."));
                }
                i_Message.Time = DateTime.UtcNow;
                entities.Messages.Add(i_Message);
                if (account.ChatPartner != i_Message.Sender)
                {
                    TodoItem todoItem = new TodoItem();
                    todoItem.Text = i_Message.Sender + ";" + account.Nickname + ";sendChatMessage;" + i_Message.Content;
                    var response = await client.PostAsJsonAsync("tables/TodoItem/PostTodoItem?ZUMO-API-VERSION=2.0.0", todoItem);
                }
                await entities.SaveChangesAsync();

                return(Ok());
            }
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> AddProposal([FromBody] Proposal i_ParkingSpotProposal)
        {
            string nickname = Thread.CurrentPrincipal.Identity.Name;

            if (i_ParkingSpotProposal.Nickname != nickname)
            {
                return(BadRequest("Nicknames do not match."));
            }

            using (CarWatchDBEntities entities = new CarWatchDBEntities())
            {
                Proposal result = await entities.Proposals.FirstOrDefaultAsync(e => e.Nickname == nickname);

                if (result != null)
                {
                    entities.Proposals.Remove(result);
                }
                i_ParkingSpotProposal.TimeOpened = DateTime.UtcNow;
                entities.Proposals.Add(i_ParkingSpotProposal);
                FacebookAccount proposerAccount = await entities.FacebookAccounts.FirstOrDefaultAsync(e => e.Nickname == nickname);

                proposerAccount.Rank++;
                await entities.SaveChangesAsync();

                return(Ok());
            }
        }
Exemplo n.º 4
0
        public async Task <IHttpActionResult> SetChatPartner([FromBody] ChatPartner i_Partner)
        {
            string nickname = Thread.CurrentPrincipal.Identity.Name;

            using (CarWatchDBEntities entities = new CarWatchDBEntities())
            {
                FacebookAccount account = await entities.FacebookAccounts.FirstOrDefaultAsync(e => e.Nickname == nickname);

                if (account == null)
                {
                    return(BadRequest("This nickname is not registered."));
                }
                account.ChatPartner = i_Partner.Partner;
                await entities.SaveChangesAsync();

                return(Ok());
            }
        }
Exemplo n.º 5
0
        public async Task <IHttpActionResult> RemoveSearch(Object obj)
        {
            string nickname = Thread.CurrentPrincipal.Identity.Name;

            using (CarWatchDBEntities entities = new CarWatchDBEntities())
            {
                Search result = await entities.Searches.FirstOrDefaultAsync(e => e.Nickname == nickname);

                if (result == null)
                {
                    return(BadRequest("This nickname has not been searching for a parking spot."));
                }
                entities.Searches.Remove(result);
                await entities.SaveChangesAsync();

                return(Ok());
            }
        }
Exemplo n.º 6
0
        public async Task <IHttpActionResult> UpdateDriverLocation(Point i_Point)
        {
            string nickname = Thread.CurrentPrincipal.Identity.Name;

            using (CarWatchDBEntities entities = new CarWatchDBEntities())
            {
                Exchange result = await entities.Exchanges.FirstOrDefaultAsync(e => e.ConsumerNickname == nickname && e.Status == 0);

                if (result == null)
                {
                    return(BadRequest("you are not involved with an open exchange."));
                }
                result.DriverLongitude = i_Point.Longitude;
                result.DriverLatitude  = i_Point.Latitude;
                await entities.SaveChangesAsync();

                return(Ok());
            }
        }
Exemplo n.º 7
0
        public async void Match()
        {
            using (CarWatchDBEntities entities = new CarWatchDBEntities())
            {
                while (true)
                {
                    List <Proposal> proposalList = entities.Proposals.ToList();
                    foreach (Proposal proposal in proposalList)
                    {
                        List <Search> searchList = await entities.Searches.Where(
                            e => 2 *k_EarthRadius *(SqlFunctions.SquareRoot(SqlFunctions.Square(SqlFunctions.Sin((SqlFunctions.Radians(proposal.Latitude) - SqlFunctions.Radians(e.Latitude)) / 2)) + SqlFunctions.Cos(SqlFunctions.Radians(proposal.Latitude)) * SqlFunctions.Cos(SqlFunctions.Radians(e.Latitude)) * SqlFunctions.Square(SqlFunctions.Sin((SqlFunctions.Radians(proposal.Longitude) - SqlFunctions.Radians(e.Longitude)) / 2)))) <= e.Distance).ToListAsync();

                        if (searchList.Count > 0)
                        {
                            List <ExtendedSearch> extendedSearchList = new List <ExtendedSearch>();
                            foreach (Search search in searchList)
                            {
                                FacebookAccount account = await entities.FacebookAccounts.FirstOrDefaultAsync(e => e.Nickname.CompareTo(search.Nickname) == 0);

                                extendedSearchList.Add(new ExtendedSearch(search, account));
                            }
                            extendedSearchList.Sort(new ExtendedSearchComparer());
                            Search   searchToRemove = extendedSearchList[0].Search;
                            Exchange exchange       = createExchangeObject(searchToRemove, proposal);
                            entities.Exchanges.Add(exchange);
                            entities.Searches.Remove(searchToRemove);
                            entities.Proposals.Remove(proposal);
                            FacebookAccount proposerAccount = await entities.FacebookAccounts.FirstOrDefaultAsync(e => e.Nickname.CompareTo(proposal.Nickname) == 0);

                            proposerAccount.Rank += 4;
                            await entities.SaveChangesAsync();

                            sendPushNotifications(searchToRemove, proposal);
                            alertOtherSearchers(extendedSearchList, proposal);
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
        public async Task <IHttpActionResult> SetLicensePlateByNickname([FromBody] FacebookAccount i_Account)
        {
            using (CarWatchDBEntities entities = new CarWatchDBEntities())
            {
                var isExist = await entities.FacebookAccounts.AnyAsync(e => e.Nickname == i_Account.Nickname);

                if (!isExist)
                {
                    return(BadRequest("This nickname is not registered."));
                }
                var isTaken = await entities.FacebookAccounts.AnyAsync(e => e.LicensePlate == i_Account.LicensePlate);

                if (isTaken)
                {
                    return(BadRequest("This license plate is already in use."));
                }
                var result = await entities.FacebookAccounts.Where(e => e.Nickname == i_Account.Nickname).FirstOrDefaultAsync();

                result.LicensePlate = i_Account.LicensePlate;
                await entities.SaveChangesAsync();

                return(Ok());
            }
        }
Exemplo n.º 9
0
        public async Task <IHttpActionResult> AddSearch([FromBody] Search i_ParkingSpotSearch)
        {
            string nickname = Thread.CurrentPrincipal.Identity.Name;

            if (i_ParkingSpotSearch.Nickname != nickname)
            {
                return(BadRequest("Nicknames do not match."));
            }

            using (CarWatchDBEntities entities = new CarWatchDBEntities())
            {
                Search result = await entities.Searches.FirstOrDefaultAsync(e => e.Nickname == nickname);

                if (result != null)
                {
                    entities.Searches.Remove(result);
                }
                i_ParkingSpotSearch.TimeOpened = DateTime.UtcNow;
                entities.Searches.Add(i_ParkingSpotSearch);
                await entities.SaveChangesAsync();

                return(Ok());
            }
        }
Exemplo n.º 10
0
        public async Task <IHttpActionResult> RemoveProposal(Object obj)
        {
            string nickname = Thread.CurrentPrincipal.Identity.Name;

            using (CarWatchDBEntities entities = new CarWatchDBEntities())
            {
                Proposal result = await entities.Proposals.FirstOrDefaultAsync(e => e.Nickname == nickname);

                if (result == null)
                {
                    return(BadRequest("This nickname has not proposed a parking spot."));
                }
                entities.Proposals.Remove(result);
                FacebookAccount proposerAccount = await entities.FacebookAccounts.FirstOrDefaultAsync(e => e.Nickname == nickname);

                if (proposerAccount.Rank > 0)
                {
                    proposerAccount.Rank--;
                }
                await entities.SaveChangesAsync();

                return(Ok());
            }
        }
Exemplo n.º 11
0
        public async Task <IHttpActionResult> UpdateExchangeStatus([FromBody] ExchangeStatus i_Status)
        {
            string nickname = Thread.CurrentPrincipal.Identity.Name;

            using (CarWatchDBEntities entities = new CarWatchDBEntities())
            {
                Exchange result = await entities.Exchanges.FirstOrDefaultAsync(e => (e.ConsumerNickname == nickname || e.ProviderNickname == nickname) && e.Status == 0);

                if (result == null)
                {
                    return(BadRequest("you are not involved with an open exchange."));
                }

                if (i_Status.status == (int)e_ExchangeStatus.Success)
                {
                    pushToClient(result.ConsumerNickname, "exchangeStatusUpdate", k_ExchangeSuccessMessage);
                    pushToClient(result.ProviderNickname, "exchangeStatusUpdate", k_ExchangeSuccessMessage);
                    FacebookAccount proposer = await entities.FacebookAccounts.FirstOrDefaultAsync(e => e.Nickname == result.ProviderNickname);

                    proposer.Rank += 5;
                }
                else if (i_Status.status == (int)e_ExchangeStatus.CanceledBySearcherPriorArrival)
                {
                    pushToClient(result.ConsumerNickname, "exchangeStatusUpdate", k_ExchangeCancelBySearcherMessage);
                    pushToClient(result.ProviderNickname, "exchangeStatusUpdate", k_ExchangeCancelBySearcherMessage);
                }
                else if (i_Status.status == (int)e_ExchangeStatus.CanceledByProposerPriorArrival)
                {
                    pushToClient(result.ConsumerNickname, "exchangeStatusUpdate", k_ExchangeCancelByProposerMessage);
                    pushToClient(result.ProviderNickname, "exchangeStatusUpdate", k_ExchangeCancelByProposerMessage);
                    FacebookAccount proposerAccount = await entities.FacebookAccounts.FirstOrDefaultAsync(e => e.Nickname == result.ProviderNickname);

                    proposerAccount.Rank -= 2;
                    if (proposerAccount.Rank < 0)
                    {
                        proposerAccount.Rank = 0;
                    }
                }
                else if (i_Status.status == (int)e_ExchangeStatus.CancelNoParkingSpot)
                {
                    pushToClient(result.ConsumerNickname, "exchangeStatusUpdate", k_ExchangeCancelNoParkingSpotMessage);
                    pushToClient(result.ProviderNickname, "exchangeStatusUpdate", k_ExchangeCancelNoParkingSpotMessage);
                    FacebookAccount proposerAccount = await entities.FacebookAccounts.FirstOrDefaultAsync(e => e.Nickname == result.ProviderNickname);

                    proposerAccount.Rank -= 3;
                    if (proposerAccount.Rank < 0)
                    {
                        proposerAccount.Rank = 0;
                    }
                }
                else
                {
                    return(BadRequest("Illegal status code."));
                }
                result.Status        = i_Status.status;
                result.TimeExchanged = DateTime.UtcNow;
                await entities.SaveChangesAsync();

                return(Ok(result));
            }
        }