public IActionResult PlaceOrder([FromBody] OrderDetails orderDetails) { try { if (orderDetails != null) { var UserId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; orderDetails.OrderBy = int.Parse(UserId); var product = _dbContext.Products.SingleOrDefault(x => x.ProductId == orderDetails.ProductId); var availableQuantity = product.Quantity; var appliedQuantity = orderDetails.Quantity; if (appliedQuantity > availableQuantity) { var response = new ReturnResultStatus(Status.Failed, "Product Quantity Applied", "Product available quantity is less then product applied quantity."); return(Ok(response.Message)); } else if (appliedQuantity == availableQuantity) { _dbContext.Orders.Add(orderDetails); _dbContext.SaveChanges(); var data = _dbContext.Products.SingleOrDefault(x => x.ProductId == orderDetails.ProductId); _dbContext.Remove(data); _dbContext.SaveChanges(); var response = new ReturnResultStatus(Status.Success, "Order Purchase", "Order placed successfully."); return(Ok(response.Message)); } else if (appliedQuantity < availableQuantity) { _dbContext.Orders.Add(orderDetails); _dbContext.SaveChanges(); var data = _dbContext.Products.SingleOrDefault(x => x.ProductId == orderDetails.ProductId); var CurrentQuantity = availableQuantity - appliedQuantity; data.Quantity = CurrentQuantity; _dbContext.Update(data); _dbContext.SaveChanges(); var response = new ReturnResultStatus(Status.Success, "Order Purchase", "Order placed successfully."); return(Ok(response.Message)); } } } catch (Exception ex) { return(BadRequest(new { message = ex.Message })); } return(null); }
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 exist."); } byte[] passwordHash, passwordSalt; CreatePasswordHash(password, out passwordHash, out passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; _dbContext.Users.Add(user); _dbContext.SaveChanges(); return(user); }
public async Task <ActionResult> CreateProduct([FromBody] ProductDetails productDetails) { try { if (productDetails != null) { await _dbContext.Products.AddAsync(productDetails); _dbContext.SaveChanges(); var response = new ReturnResultStatus(Status.Success, "Product Created", "Product created successfully."); return(new OkObjectResult(response.Message)); } else { var response = new ReturnResultStatus(Status.Failed, "Product Not Created", "Unable to create product."); return(new OkObjectResult(response.Message)); } } catch (Exception ex) { return(BadRequest(new { message = ex.Message })); } }