public async Task <ActionResult <Reservation> > Delete(int id)
        {
            ShopStorage shopStorage = new ShopStorage(_db);

            shopStorage.CancelReserve(id);

            return(Ok());
        }
        public async Task <ActionResult <Product> > Post(Product product)
        {
            if (product == null)
            {
                return(BadRequest());
            }
            ShopStorage shopStorage = new ShopStorage(_db);
            await shopStorage.AddProduct(product);

            return(Ok(product));
        }
        public async Task <ActionResult <Product> > Put(Product product)
        {
            if (product == null)
            {
                return(BadRequest());
            }

            if (!_db.Products.Any(x => x.Id == product.Id))
            {
                return(NotFound());
            }
            ShopStorage shopStorage = new ShopStorage(_db);
            await shopStorage.UpdateProduct(product.Name, product.Amount);

            return(Ok(product));
        }
        public void MainTest()
        {
            // Init
            ExpressOrdersContext dbContext = new ExpressOrdersContext();

            if (!dbContext.Database.EnsureCreated())
            {
                dbContext.Database.ExecuteSqlRaw("DELETE FROM public.\"Order\"");
                dbContext.Database.ExecuteSqlRaw("DELETE FROM public.\"Product\"");
            }

            ShopStorage      storage = new ShopStorage();
            Random           random  = new Random();
            ProductDataModel product = new ProductDataModel("Car");
            int productsCount        = 100;

            storage.Add(product, productsCount);

            // Act
            for (int k = 0; k < 10; k++)
            {
                Thread thread = new Thread(
                    new ThreadStart(() =>
                {
                    ShopStorage threadStorage = new ShopStorage();
                    for (int i = 0; i < 1000; i++)
                    {
                        try
                        {
                            threadStorage.Reserve(product, random.Next(1, 3));
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            break;
                        }
                    }
                })
                    );

                thread.Start();
                thread.Join();
            }

            // Assert
            Assert.Equal(0, dbContext.Product.First().Stock);
            Assert.Equal(productsCount, dbContext.Order.Sum(o => o.Count));
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Подготовка.");
            PrepareDb();
            Console.WriteLine("Подготовка закончена.");
            ShopStorage      storage = new ShopStorage();
            Random           random  = new Random();
            ProductDataModel product = new ProductDataModel("Car");

            storage.Add(product, 100);
            Console.WriteLine("Запуск потоков.");

            for (int k = 0; k < 10; k++)
            {
                Thread thread = new Thread(
                    new ThreadStart(() =>
                {
                    ShopStorage threadStorage             = new ShopStorage();
                    Dictionary <int, List <bool> > result = new Dictionary <int, List <bool> >();

                    for (int i = 0; i < 1000; i++)
                    {
                        try
                        {
                            int randomCount = random.Next(1, 3);
                            threadStorage.Reserve(product, randomCount);
                        }
                        catch (ArgumentOutOfRangeException e)
                        {
                            Console.WriteLine(e.Message);
                            // break;
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                })
                    );

                thread.Start();
                thread.Join();
            }

            Console.WriteLine("Completed.");
            Console.ReadLine();
        }
Exemplo n.º 6
0
        public static void LoadAllStoragesFromDB()
        {
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            DataTable result = Database.ExecutePreparedStatement("SELECT * FROM bar_storages", parameters);

            if (result.Rows.Count != 0)
            {
                foreach (DataRow row in result.Rows)
                {
                    ShopStorage ShopStorage = new ShopStorage
                    {
                        NameShop        = (string)row["NameShop"],
                        Position        = JsonConvert.DeserializeObject <Vector3>((string)row["Position"]),
                        eCola           = (int)row["eCola"],
                        Sprunk          = (int)row["Sprunk"],
                        Schokoriegel    = (int)row["Schokoriegel"],
                        Wasser          = (int)row["Wasser"],
                        Chips           = (int)row["Chips"],
                        Repair_Kit      = (int)row["Repair_Kit"],
                        Bottle          = (int)row["Bottle"],
                        Flashlight      = (int)row["Flashlight"],
                        Beer            = (int)row["Beer"],
                        Caffe           = (int)row["Caffe"],
                        Benzin_Kanister = (int)row["Benzin_Kanister"],
                        Tabak           = (int)row["Tabak"],
                        Gamburger       = (int)row["Gamburger"],
                        Viske           = (int)row["Viske"]
                    };

                    Marker shopmarker = API.shared.createMarker(1, ShopStorage.Position - new Vector3(0, 0, 1f), new Vector3(), new Vector3(), new Vector3(0.85f, 0.85f, 0.85f), 100, 0, 0, 255);
                    API.shared.createTextLabel("Магазин " + ShopStorage.NameShop, ShopStorage.Position, 15f, 0.65f);

                    ColShape ShopStorageColshape = API.shared.createCylinderColShape(ShopStorage.Position, 0.50f, 1f);
                    ShopStorageColshape.setData("IS_BAR_STORAGE", true);
                    ShopStorage.ColShape = ShopStorageColshape;

                    ShopStorageList.Add(ShopStorage);
                }
                API.shared.consoleOutput(LogCat.Info, result.Rows.Count + "Складов загружено!");
            }
            else
            {
                API.shared.consoleOutput(LogCat.Info, "Не удалось загрузить склады!");
            }
        }
Exemplo n.º 7
0
        public void Reserve_10Threads_1000Times_Test()
        {
            var context = new DatabaseContext(_options);

            _shopStorage = new ShopStorage(context);

            var result = _shopStorage.DeleteAllReserve(_productName).Result;

            var product = _shopStorage.AddProduct(_productName, 100).Result ?? _shopStorage.UpdateProduct(_productName, 100).Result;

            Task[] tasks = new Task[10];
            for (int i = 0; i < 10; i++)
            {
                tasks[i] = new Task(Reserve_RndAmount_1000Times);
                tasks[i].Start();
            }

            Task.WaitAll(tasks);
        }
        public async Task <ActionResult <Reservation> > Post(Reservation reservation)
        {
            if (reservation == null)
            {
                return(BadRequest());
            }

            var product = _db.Products.FirstOrDefault(x => x.Name == reservation.ProductName);

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

            ShopStorage shopStorage = new ShopStorage(_db);
            await shopStorage.Reserve(reservation.ProductName, reservation.Amount, reservation.ClientFullName);


            await _db.SaveChangesAsync();

            return(Ok(reservation));
        }