예제 #1
0
        /// <summary>
        /// Check is posibe to confirm user order
        /// </summary>
        /// <param name="userChoice"></param>
        /// <returns>true if posible confirm order</returns>
        private bool CheckUserChoise(UserChoice userChoice)
        {
            bool result = false;
            UserChoice oldChoice = db.UserChoices.Where(uc => uc.Id == userChoice.Id).First();
            db.Entry(oldChoice).State = EntityState.Detached;
            Menu menu = db.Menus.First(m => m.Id == userChoice.MenuId);
            int balance = db.GetUserBalance(userChoice.UserID);
            

            if (User.IsInRole("Admin") || User.IsInRole("GlobalAdmin"))
            {
                if (userChoice.confirm && !oldChoice.confirm)
                {
                    balance = balance - menu.Price;
                    if (balance >= 0)
                    {
                        result = true;
                    }
                }

                if (!userChoice.confirm && oldChoice.confirm)
                {
                    result = true;
                }
            }

            if (userChoice.UserID == User.Identity.GetUserId() && !userChoice.confirm)
            {
                if (balance >= menu.Price)
                {
                    result = true;
                }
            }
            
            return result;
        }
예제 #2
0
        public IHttpActionResult Post(UserChoice userChoice)
        {
            if (userChoice.UserID == null)
            {
                userChoice.UserID = User.Identity.GetUserId();
                Validate(userChoice);
            } 

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Menu menu = db.Menus.First(m => m.Id == userChoice.MenuId);
            int balance = db.GetUserBalance(userChoice.UserID);
            if (balance >= menu.Price)
            {
                db.UserChoices.Add(userChoice);
                db.SaveChanges();

            }
            else
            {
                return BadRequest("Not enough money");
            }

            return CreatedAtRoute("Get", new { id = userChoice.Id }, userChoice);
        }
예제 #3
0
        public IHttpActionResult Put(int id, UserChoice userChoice)
        {
            if (!ModelState.IsValid || id != userChoice.Id)
            {
                return BadRequest(ModelState);
            }
            
            
            if (!CheckUserChoise(userChoice))
            {
                return BadRequest("Not enough money");
            }

            userChoice.Menu = null;
            db.Entry(userChoice).State = EntityState.Modified;
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserChoiceExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }