public async Task <IActionResult> PutProduct([FromQuery] int p_id, Product product) { if (p_id != product.PId) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(p_id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Customer> > PostCustomer([FromBody] Customer customer) { //Check if Customer already Exists with exact same properties bool CustomerExists = await CustomerExistsByObject(customer); if (CustomerExists) { return(BadRequest(new ErrorRecordRepeat())); } _context.Customer.Add(customer); await _context.SaveChangesAsync(); return(CreatedAtAction("GetCustomer", new { id = customer.CId }, customer)); }
public async Task <ActionResult <IList <Supplier> > > PostSupplier([FromBody] IList <Supplier> suppliers) { foreach (Supplier supplier in suppliers) { bool supplierexists = await SupplierExistsByObject(supplier); if (supplierexists) { return(BadRequest(new ErrorRecordRepeat())); } } _context.Supplier.AddRange(suppliers); await _context.SaveChangesAsync(); return(CreatedAtAction("GetSupplier", suppliers)); }
public async Task <ActionResult <JObject> > PostOrders([FromBody] JObject orders) { if (orders == null) { BadRequest(); } //Get Order Date var o_date = Convert.ToDateTime(orders.GetValue("ODate")); //Get Order Type var o_type = orders.GetValue("OType").ToString(); //Create Order Object by looking at order type Orders order = new Orders() { ODate = o_date, OType = o_type }; //Set Customer id if order type is sale else set Supplier id if purchase if (o_type == "purchase") { order.SId = Convert.ToInt32(orders.GetValue("SId")); } else if (o_type == "sale") { order.CId = Convert.ToInt32(orders.GetValue("CId")); } //Add Order to the Database _context.Orders.Add(order); await _context.SaveChangesAsync(); //Get newly created order id int OId = order.OId; //To store total amount of the products int total = 0; foreach (JToken item in orders.Last.Values()) { OrderProduct orderproduct = new OrderProduct() { OpQuantity = item.Value <int>("OpQuantity"), OId = OId, PId = item.Value <int>("PId") }; //If order type is sale then Check if product stock is sufficient if (o_type == "sale") { if (!IsProductStockSufficient(orderproduct.PId, orderproduct.OpQuantity)) { return(BadRequest(new ErrorInsufficientStock())); } } //Add Order to OrderProduct Table _context.OrderProduct.Add(orderproduct); //Get Product Object Product product = _context.Product.FirstOrDefault(p => p.PId == orderproduct.PId); //Return Bad Request if any product doesnt exist if (product == null) { BadRequest(); } //Calculate total total += (product.PPrice * orderproduct.OpQuantity); //Change State to Modified _context.Entry(product).State = EntityState.Modified; //Increment or Decrement stock by checking order type if (o_type == "purchase") { //Increment Product Stock by Quantity if (product != null) { product.PStock += orderproduct.OpQuantity; } } else if (o_type == "sale") { //Decrement Product Stock by Quantity if (product != null) { product.PStock -= orderproduct.OpQuantity; } } } //Update Order Total order.OTotalAmount = total; //Save Changes to Database await _context.SaveChangesAsync(); //Add total to Returning Object orders.Add("OTotal", total); return(Created("", orders)); }