public ServiceResponse <Data.Models.Product> CreateProduct(Data.Models.Product product) { try { _db.Products.Add(product); var newInventory = new ProductInventory { Product = product, QuantityOnHand = 0, IdealQuantity = 10, }; _db.ProductInventories.Add(newInventory); _db.SaveChanges(); return(new ServiceResponse <Data.Models.Product> { Data = product, IsSuccess = true, Time = DateTime.UtcNow, Message = "Saved new product" }); } catch (Exception e) { return(new ServiceResponse <Data.Models.Product> { Data = product, Time = DateTime.UtcNow, IsSuccess = false, Message = "Error saving product" }); } }
public ServiceResponse <Data.Models.Customer> CreateCustomer(Data.Models.Customer customer) { try { _db.Customers.Add(customer); _db.SaveChanges(); return(new ServiceResponse <Data.Models.Customer> { Data = customer, Time = DateTime.UtcNow, IsSuccess = true, Message = "Successfully added customer" }); } catch (Exception e) { return(new ServiceResponse <Data.Models.Customer> { Data = null, Time = DateTime.UtcNow, IsSuccess = false, Message = "Error adding Customer" + e.StackTrace }); } }
public ServiceResponse <bool> GenerateOpenOrder(SalesOrder order) { _logger.LogInformation("Generating new order"); foreach (var item in order.SalesOrderItems) { item.Product = _productService.GetProductById(item.Product.Id); var inventoryId = _inventoryService.GetByProductId(item.Product.Id).Id; _inventoryService.UpdateUnitAvailable(inventoryId, -item.Quantity); } try { _db.SalesOrders.Add(order); _db.SaveChanges(); return(new ServiceResponse <bool> { Data = true, IsSuccess = true, Message = $"Open order created for: {order.Id}", Time = DateTime.UtcNow }); } catch (Exception e) { return(new ServiceResponse <bool> { IsSuccess = false, Data = false, Message = "Error opening order: " + e.StackTrace, Time = DateTime.UtcNow }); } }
public ServiceResponse <ProductInventory> UpdateUnitAvailable(int id, int adjustment) { try { var inventory = _db.ProductInventories .Include(inv => inv.Product) .First(inv => inv.Product.Id == id); inventory.QuantityOnHand += adjustment; try { CreateSnapShot(inventory); } catch (Exception e) { _logger.LogInformation("Failed creating snapshot: "); _logger.LogInformation(e.StackTrace); } _db.SaveChanges(); return(new ServiceResponse <ProductInventory> { Data = inventory, IsSuccess = true, Message = "Inventory Updated", Time = DateTime.UtcNow }); } catch (Exception e) { return(new ServiceResponse <ProductInventory> { Data = null, IsSuccess = false, Message = "Error updating : " + e.StackTrace, Time = DateTime.UtcNow }); } }