예제 #1
0
        public ActionResult Payment([FromBody] BalanceInput input)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var user = accountService.GetCurrentUserForContext(HttpContext);

            if (user == null || user.Id != input.UserId)
            {
                // User should only be able to alter its own balance
                return(Unauthorized());
            }

            var transaction = new BalanceTransaction {
                UserId = user.Id, Amount = input.Amount
            };

            dbcontext.BalanceTransaction.Add(transaction);

            user.Balance += transaction.Amount;
            dbcontext.Update(user);

            dbcontext.SaveChanges();

            return(Ok());
        }
예제 #2
0
        public ActionResult Buy([FromBody] BuyInput userParam)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid body given"));
            }

            var drink = dbcontext.Drink.FirstOrDefault(d => d.Id == userParam.DrinkId);

            if (drink == null)
            {
                return(NotFound("Getränk nicht gefunden"));
            }

            var rfid = dbcontext.Rfid.Include(x => x.User).FirstOrDefault(r => r.rfId == userParam.RfId);

            if (rfid == null)
            {
                return(NotFound("Rfid nicht gefunden"));
            }

            var user = rfid.User;

            if (user == null)
            {
                return(NotFound("User nicht gefunden"));
            }

            var proceed = new Proceed {
                UserId = user.Id, DrinkId = drink.Id, Price = drink.Price
            };

            if (drink.Quantity == 0)
            {
                return(BadRequest("Can not get drink since there should be no more drinks available"));
            }

            if (user.Balance < drink.Price)
            {
                return(BadRequest("Balance is insufficient"));
            }

            dbcontext.Proceed.Add(proceed);
            user.Balance   -= drink.Price;
            user.Balance    = Math.Round(user.Balance, 2);
            drink.Quantity -= 1;

            dbcontext.Update(user);
            dbcontext.Update(drink);
            dbcontext.SaveChanges();

            return(Ok(new Order()
            {
                user = user.UserName,
                drink = drink.Name,
                price = drink.Price
            }));
        }
예제 #3
0
        public ActionResult Register([FromBody] LoginInput userParams)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("you suck"));
            }

            var user = new User()
            {
                UserName = userParams.Username,
                Password = createHashedPassword(userParams.Password)
            };

            dbcontext.User.Add(user);
            dbcontext.SaveChanges();

            return(Ok("user registered"));
        }
예제 #4
0
        public ActionResult Payment([FromBody] RestRefillment input)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var user = accountService.GetCurrentUserForContext(HttpContext);

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

            // Make sure that all drinks exist
            if (input.Items.Any(item => !dbcontext.Drink.Any(d => d.Id == item.DrinkId)))
            {
                return(NotFound());
            }

            // user should get the money they payed for the refill.
            user.Balance += input.Price;
            dbcontext.Update(user);

            // Create a refill
            var refill = new Refill {
                UserId = user.Id, Price = input.Price
            };

            dbcontext.Refill.Add(refill);

            foreach (var item in input.Items)
            {
                // Add new log entry in db
                var refillContainment = new RefillContainment
                {
                    RefillId = refill.Id,
                    // ReSharper disable once PossibleInvalidOperationException
                    // Should be set since it's required in the model
                    DrinkId  = (uint)item.DrinkId,
                    Quantity = item.Quantity
                };

                dbcontext.RefillContainment.Add(refillContainment);

                // Update drink states
                var drink = dbcontext.Drink.FirstOrDefault(d => d.Id == item.DrinkId);
                if (drink == null)
                {
                    throw new Exception($"Drink for id {item.DrinkId} went missing");
                }

                // ReSharper disable once PossibleInvalidOperationException
                // Should be set since it's required in the model
                drink.Quantity += (int)item.Quantity;
                dbcontext.Update(drink);
            }

            dbcontext.SaveChanges();

            return(Ok());
        }