public async Task <IActionResult> EditProduct([FromBody] ProductDetails productDetails) { try { var result = await _dbContext.Products.Where(x => x.ProductId == productDetails.ProductId).SingleOrDefaultAsync(); if (result != null) { result.ProductName = productDetails.ProductName; result.Price = productDetails.Price; result.Description = productDetails.Description; result.ProductImage = productDetails.ProductImage; result.Quantity = productDetails.Quantity; _dbContext.Products.Update(result); _dbContext.SaveChanges(); var response = new ReturnResultStatus(Status.Success, "Product Updated", "Product updated successfully."); return(new OkObjectResult(response.Message)); } else { var response = new ReturnResultStatus(Status.Failed, "Product Not found", "Product details not found."); return(Ok(response.Message)); } } catch (Exception ex) { return(BadRequest(new { message = ex.Message })); } }
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 async Task <ActionResult> GetProductById(int?id) { try { var result = await _dbContext.Products.Where(x => x.ProductId == id).FirstOrDefaultAsync(); if (result != null) { var response = new ReturnResultStatus(Status.Success, "Product List", "Product list", result); return(Ok(response.Data)); } else { var response = new ReturnResultStatus(Status.Failed, "Product Not Found", "Product not found"); return(Ok(response.Message)); } } catch (Exception ex) { return(BadRequest(new { message = ex.Message })); } }
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 })); } }
public async Task <IActionResult> DeleteProduct([FromBody] ProductDetails productDetails) { try { var result = await _dbContext.Products.Where(x => x.ProductId == productDetails.ProductId).SingleOrDefaultAsync(); if (result != null) { _dbContext.Remove(result); _dbContext.SaveChanges(); var response = new ReturnResultStatus(Status.Success, "Product Deleted", "Product deleted successfully."); return(Ok(response.Message)); } else { var response = new ReturnResultStatus(Status.Failed, "Product not found", "Unable to delete Product."); return(Ok(response.Message)); } } catch (Exception ex) { return(BadRequest(new { message = ex.Message })); } }
/// <summary> /// App异常 /// </summary> /// <param name="errorStatus">错误状态</param> /// <param name="message">消息</param> /// <param name="fun">委托</param> public AppException(ReturnResultStatus errorStatus, string message, Func <bool> fun = null) : this(errorStatus, message, null, fun) { }
/// <summary> /// App异常 /// </summary> /// <param name="errorStatus">错误状态</param> /// <param name="message">消息</param> /// <param name="exception">异常</param> /// <param name="fun">委托</param> public AppException(ReturnResultStatus errorStatus, string message, Exception exception, Func <bool> fun = null) : base(message, exception) { this.ErrorStatus = (int)errorStatus; fun?.Invoke(); }
/// <summary> /// 根据状态返回结果 /// </summary> /// <param name="Status"></param> /// <param name="Message">消息</param> /// <returns></returns> public static ReturnResult ResultMsg(ReturnResultStatus Status, string Message) { return(new ReturnResult(Status, Message)); }
/// <summary> /// 状态 + 消息 /// </summary> /// <param name="Status">状态</param> /// <param name="Message">消息</param> public ReturnResult(ReturnResultStatus Status, string Message) { this.Status = (int)Status; this.Message = Message; }
/// <summary> /// 状态 /// </summary> /// <param name="Status">状态</param> public ReturnResult(ReturnResultStatus Status) { this.Status = (int)Status; }