Пример #1
0
        public static async Task <int> GetNumberOfPlaces(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "get-places-number/")] HttpRequestMessage request,
            ILogger log)
        {
            log.LogInformation("GetNumberOfPlaces");
            //addition
            CloudTable       table  = null;
            CloudTableClient client = null;

            try
            {
                StorageCredentials  creds   = new StorageCredentials(Environment.GetEnvironmentVariable("accountName"), Environment.GetEnvironmentVariable("accountKey"));
                CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);

                client = account.CreateCloudTableClient();

                table = client.GetTableReference("Garage");
                await table.CreateIfNotExistsAsync();

                Console.WriteLine(table.Uri.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            /*get the avaliable places*/
            TableQuery <availablePlaces> idQuery = new TableQuery <availablePlaces>()
                                                   .Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "places"));
            TableQuerySegment <availablePlaces> queryResult = await table.ExecuteQuerySegmentedAsync(idQuery, null);

            availablePlaces places = queryResult.FirstOrDefault();

            Console.Out.WriteLine("RowKey" + places.RowKey);
            return(int.Parse(places.numOfPlaces));
        }
Пример #2
0
        public static async Task <status> GetIfIsOpen(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "get-isOpen/{id}/{state}")] HttpRequestMessage request,
            string id,
            string state,
            [SignalR(HubName = "CounterHub")] IAsyncCollector <SignalRMessage> signalRMessages,
            ILogger log)
        {
            log.LogInformation("Getting if to open or not.");
            //addition
            CloudTable       table  = null;
            CloudTableClient client = null;

            try
            {
                StorageCredentials  creds   = new StorageCredentials(Environment.GetEnvironmentVariable("accountName"), Environment.GetEnvironmentVariable("accountKey"));
                CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);

                client = account.CreateCloudTableClient();

                table = client.GetTableReference("Users");
                await table.CreateIfNotExistsAsync();

                Console.WriteLine(table.Uri.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            status st = new status();

            CloudTable garageTable   = client.GetTableReference("Garage");
            Boolean    isInTheGarage = await isIdInTable(garageTable, "PartitionKey", id);

            /*get the avaliable places*/
            TableQuery <availablePlaces> idQuery = new TableQuery <availablePlaces>()
                                                   .Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "places"));
            TableQuerySegment <availablePlaces> queryResult = await garageTable.ExecuteQuerySegmentedAsync(idQuery, null);

            availablePlaces places = queryResult.FirstOrDefault();

            if (isInTheGarage && state.Equals("in"))
            {
                st.isOpen = "arleady in the Garage!";
                return(st);
            }
            else if (!isInTheGarage && state.Equals("out"))
            {
                st.isOpen = "arleady out of the Garage!";
                return(st);
            }
            else if (isInTheGarage && state.Equals("out"))
            {
                //remove car from garage
                TableOperation retrieve = TableOperation.Retrieve <Request>(id, "car");
                TableResult    result   = await garageTable.ExecuteAsync(retrieve);

                var            deleteEntity = (TableEntity)result.Result;
                TableOperation delete       = TableOperation.Delete(deleteEntity);
                await garageTable.ExecuteAsync(delete);

                st.isOpen = "open";
                //add one to the avaliavle places
                places.numOfPlaces = (int.Parse(places.numOfPlaces) + 1).ToString();
                //signalR
                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    Target    = "placesUpdate",
                    Arguments = new [] { places.numOfPlaces }
                });

                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    Target    = "garageUpdate",
                    Arguments = new[] { (Object)id }
                });

                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    Target    = ((Request)result.Result).UserName + "Notify",
                    Arguments = new[] { ((Request)result.Result).ownerName + " left the garage" }
                });

                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    Target    = "adminNotify",
                    Arguments = new[] { ((Request)result.Result).ownerName + " left the garage" }
                });

                TableOperation add = TableOperation.InsertOrReplace(places);
                await garageTable.ExecuteAsync(add);

                return(st);
            }
            //in
            if (int.Parse(places.numOfPlaces) == 0)
            {
                st.isOpen = "don't open the Garage is FULL!";
                return(st);
            }
            List <String> users = await GetUsersFromTable(table);

            foreach (string regesterdUser in users)
            {
                CloudTable usersTable = client.GetTableReference("Table00" + regesterdUser);
                await usersTable.CreateIfNotExistsAsync();

                Boolean isIdRegestered = await isIdInTable(usersTable, "RowKey", id);

                if (isIdRegestered)
                {
                    //add to garage
                    Request newCar = new Request();
                    newCar.PartitionKey = id;
                    newCar.RowKey       = "car";
                    newCar.ownerName    = ownerName;
                    newCar.UserName     = regesterdUser;

                    TableOperation add = TableOperation.InsertOrReplace(newCar);
                    await garageTable.ExecuteAsync(add);

                    st.isOpen = "open";
                    //update the avaliable places
                    places.numOfPlaces = (int.Parse(places.numOfPlaces) - 1).ToString();
                    await signalRMessages.AddAsync(
                        new SignalRMessage
                    {
                        Target    = "placesUpdate",
                        Arguments = new[] { places.numOfPlaces }
                    });

                    await signalRMessages.AddAsync(
                        new SignalRMessage
                    {
                        Target    = "garageUpdate",
                        Arguments = new[] { (Object)id }
                    });

                    await signalRMessages.AddAsync(
                        new SignalRMessage
                    {
                        Target    = regesterdUser + "Notify",
                        Arguments = new[] { ownerName + " entered the garage" }
                    });

                    await signalRMessages.AddAsync(
                        new SignalRMessage
                    {
                        Target    = "adminNotify",
                        Arguments = new[] { ownerName + " entered the garage" }
                    });

                    TableOperation addOrReplace = TableOperation.InsertOrReplace(places);
                    await garageTable.ExecuteAsync(addOrReplace);

                    return(st);
                }
            }
            st.isOpen = "don't open";
            return(st);
        }