public ActionResult PlaceOrder(SaveOrder model)
        {
            try
            {
                if (model != null)
                {
                    var UserId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
                    model.OrderBy = int.Parse(UserId);
                    var product           = _dbContext.Products.SingleOrDefault(x => x.ProductId == model.ProductId);
                    var availableQuantity = product.Quantity;
                    var appliedQuantity   = model.Quantity;

                    if (appliedQuantity > availableQuantity)
                    {
                        var resultReturnModel = new ReturnModelValue(enumReturnStatus.Failed, "There is no such quanties vailable", "Select Within a given quantity", null);
                        return(Ok(resultReturnModel));
                    }

                    else if (appliedQuantity == availableQuantity)
                    {
                        _dbContext.SaveOrders.Add(model);
                        _dbContext.SaveChanges();
                        var data = _dbContext.Products.SingleOrDefault(x => x.ProductId == model.ProductId);
                        _dbContext.Remove(data);
                        _dbContext.SaveChanges();
                        var resultReturnModel = new ReturnModelValue(enumReturnStatus.Success, "Order Purchase", "Purchased", null);
                        return(Ok(resultReturnModel));
                    }
                    else if (appliedQuantity < availableQuantity)
                    {
                        _dbContext.SaveOrders.Add(model);
                        _dbContext.SaveChanges();
                        var data            = _dbContext.Products.SingleOrDefault(x => x.ProductId == model.ProductId);
                        var CurrentQuantity = availableQuantity - appliedQuantity;
                        data.Quantity = CurrentQuantity;
                        _dbContext.Update(data);
                        _dbContext.SaveChanges();
                        var resultReturnModel = new ReturnModelValue(enumReturnStatus.Success, "Order Purchaes", "Purchased", null);
                        return(Ok(resultReturnModel));
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
            return(null);
        }
        public async Task <ActionResult> CreateProduct(ProductDetails model)
        {
            try
            {
                if (model != null)
                {
                    await _dbContext.Products.AddAsync(model);

                    _dbContext.SaveChanges();
                    return(new OkObjectResult(new ReturnModelValue(enumReturnStatus.Success, "Product Created", "Product Created Cuccessfully.", model)));
                }

                else
                {
                    return(new OkObjectResult(new ReturnModelValue(enumReturnStatus.Failed, "Please Pass Proper Product", "Product Not Created.", null)));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }
Esempio n. 3
0
        public UserDetails Create(UserDetails user, string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }

            if (_dbContext.Users.Any(x => x.UserName == user.UserName))
            {
                throw new AppException("Username \"" + user.UserName + "\" is already taken");
            }

            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            _dbContext.Users.Add(user);
            _dbContext.SaveChanges();

            return(user);
        }