示例#1
0
        public async Task <Customer> Add(Customer customer)
        {
            var entity = _context.Add(customer).Entity;
            await _context.SaveChangesAsync();

            return(entity);
        }
示例#2
0
        public async Task <IActionResult> Create([Bind("LocationId,LocationName")] Location location)
        {
            if (ModelState.IsValid)
            {
                _context.Add(location);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(location));
        }
示例#3
0
        // POST: odata/Orders
        public async Task <int> Post()
        {
            var bodyString = Request.Content.ReadAsStringAsync().Result;
            var neworder   = JsonConvert.DeserializeObject <NewOrder>(bodyString);

            for (int i = 0; i < neworder.ProductsID.Count(); i++)
            {
                neworder.Cost += db.Products.Find(neworder.ProductsID.ElementAt(i)).Cost *neworder.ProductsAmount.ElementAt(i);
            }
            Order order = new Order
            {
                UserID  = neworder.UserID,
                StatusO = neworder.StatusO,
                Cost    = neworder.Cost
            };

            db.Orders.Add(order);
            await db.SaveChangesAsync();

            Created(order);

            int orderID = db.Orders.ToList().Last().OrderID;

            for (int i = 0; i < neworder.ProductsID.Count(); i++)
            {
                decimal newAmount = db.Products.Find(neworder.ProductsID.ElementAt(i)).Amount - neworder.ProductsAmount.ElementAt(i);
                if (newAmount < 0)
                {
                    neworder.ProductsAmount[i] = neworder.ProductsAmount[i] + newAmount;
                    newAmount = 0;
                }
                db.Products.Find(neworder.ProductsID.ElementAt(i)).Amount = newAmount;
                Updated(db.Products.Find(neworder.ProductsID.ElementAt(i)));

                ItemsOfOrder itemsOfOrder = new ItemsOfOrder
                {
                    ProductID = neworder.ProductsID[i],
                    OrderID   = orderID,
                    Amount    = neworder.ProductsAmount[i],
                    Cost      = db.Products.Find(neworder.ProductsID.ElementAt(i)).Cost *neworder.ProductsAmount.ElementAt(i)
                };

                db.ItemsOfOrders.Add(itemsOfOrder);
                await db.SaveChangesAsync();

                Created(itemsOfOrder);
            }

            return(orderID);
        }
示例#4
0
        public async Task <bool> DecrementOnLineCreate(int locationId, OrderLineItem lineItem)
        {
            bool success = true;
            // var detail = await _context.LineItems.Where(o => o.OrderId == order.OrderId).ToListAsync();
            var products = await _context.Products.Where(p => p.LocationId == locationId).Select(c => c).ToListAsync();

            //  for every line item decrement the corresponding product quantity

            /*foreach(var item in lineItem.)
             * {
             *      var update = products.Where(p => p.ProductId == item.ProductId).FirstOrDefault();
             *      update.Qty -= item.Qty;
             * }*/
            foreach (var item in products)
            {
                if (item.ProductId == lineItem.ProductId)
                {
                    item.Qty -= lineItem.Qty;
                }
            }

            await _context.SaveChangesAsync();

            return(success);
        }
示例#5
0
        // GET: odata/ItemsOfOrdersByOrder(5)
        //[EnableQuery]
        //public IQueryable<ItemsOfOrder> GetItemOfOrders(int key)
        //{
        //    return db.ItemsOfOrders.Where(itemOfOrder => itemOfOrder.OrderID == key);
        //}

        // POST: odata/ItemsOfOrder
        public async Task <IHttpActionResult> Post(ItemsOfOrder itemsOfOrder)
        {
            db.ItemsOfOrders.Add(itemsOfOrder);
            await db.SaveChangesAsync();

            return(Created(itemsOfOrder));
        }
示例#6
0
        public async Task <IHttpActionResult> Post(User user)
        {
            db.Users.Add(user);
            await db.SaveChangesAsync();

            return(Created(user));
        }
示例#7
0
        public async Task <Order> AddOrder(int customerId, int locationId, double orderTotal)
        {
            _logger.LogInformation("Creating new order for customer: {0}", customerId);
            var newOrder = new Order
            {
                CustomerId = customerId,
                LocationId = locationId,
                OrderTotal = orderTotal,
                Timestamp  = DateTime.Now
            };

            _context.Orders.Add(newOrder);

            await _context.SaveChangesAsync();

            var order = await _context.Orders.Where(o => o.Timestamp == newOrder.Timestamp && o.CustomerId == newOrder.CustomerId).FirstOrDefaultAsync();

            return(order);
        }
示例#8
0
        public async Task Comment(NewCommentModel model)
        {
            validator.ValidateAndThrow(model);

            var user = await userAccess.GetUser();

            if (user.Banned)
            {
                throw new Exception("Can't post while banned!");
            }

            var newComment = new Dal.Entities.Comment
            {
                FileId     = model.FileId,
                Uploader   = user.UserName,
                UploadTime = DateTime.Now,
                Text       = model.Text
            };

            dbContext.Comments.Add(newComment);

            await dbContext.SaveChangesAsync();

            logger.LogInformation(user.Id + ": commented on a file!(ID:" + model.FileId + ")");
        }
示例#9
0
        public async Task EditUsers(List <UserModel> users)
        {
            var adminUser = await userAccess.GetUser();

            foreach (var user in users)
            {
                var applicationUser = await dbContext.ApplicationUsers.FirstAsync(u => u.Id == user.Id);

                if (user.Money != applicationUser.Money || user.Vat != applicationUser.Vat || user.Banned != applicationUser.Banned)
                {
                    applicationUser.Banned = user.Banned;
                    applicationUser.Money  = user.Money;
                    applicationUser.Vat    = user.Vat;

                    await dbContext.SaveChangesAsync();

                    logger.LogInformation(adminUser.Id + ": edited a user(" + user.Id + ")");
                }
            }
        }