public Order CreateOrder(OrderModel order) { var user = _context.Users.Single(u => u.ID == order.User.ID); Order newOrder = new() { ID = Guid.NewGuid(), Status = "Mottagen", OrderDate = DateTime.Now, ShipmentChoice = order.ShipmentChoice, PaymentChoice = order.PaymentChoice, Address = $"{order.ShippingAddress.Street}, {order.ShippingAddress.PostalCode}, {order.ShippingAddress.City}" }; newOrder.OrderNumber = $"{newOrder.ID}{newOrder.OrderDate}".GetHashCode().ToString(); if (newOrder.OrderNumber.First() == '-') { newOrder.OrderNumber = newOrder.OrderNumber.Remove(0, 1); } user.Orders.Add(newOrder); var returnOrder = _context.Orders.Add(newOrder); _context.SaveChanges(); return(returnOrder.Entity); }
public void UpdateAddress(Address updatedAddress, Guid AddressID) { var addressToUpdate = _context.Addresses.Single(a => a.ID == AddressID); addressToUpdate.City = updatedAddress.City; addressToUpdate.Country = updatedAddress.Country; addressToUpdate.PostalCode = updatedAddress.PostalCode; addressToUpdate.State = updatedAddress.State; addressToUpdate.Street = updatedAddress.Street; _context.SaveChanges(); }
public void CreateUser(UserRegisterModel model) { var salt = PasswordHasher.SaltGenerator(); User user = new User { Role = "Customer", FirstName = model.FirstName, LastName = model.LastName, EmailAddress = model.Email, Salt = salt, Password = PasswordHasher.HashPassword(model.Password, salt), RegisterDate = DateTime.Now }; _context.Users.Add(user); _context.SaveChanges(); }
public void CommitTransaction() { if (_transaction != null) { _context.SaveChanges(); _transaction.Commit(); } _transaction = null; }
public TEntity Add(TEntity entity, bool autoSaveChanges = true) { TEntity _entity = Entities.Add(entity); if (autoSaveChanges) { _context.SaveChanges(); } return(_entity); }
public void UpdateSaldo(OrderModel orderModel) { foreach (var item in orderModel.ProductList) { item.Product.Sales += item.Quantity; item.Product.StockBalance -= item.Quantity; _context.Products.Attach(item.Product).State = EntityState.Modified; } _context.SaveChanges(); }
public List <CartItem> CreateCartItem(List <CartItemModel> productList) { List <CartItem> cartItems = new(); foreach (var product in productList) { CartItem cartItem = new CartItem { ID = Guid.NewGuid(), Product = product.Product, Quantity = product.Quantity }; cartItems.Add(cartItem); } _context.CartItem.AddRange(cartItems); _context.SaveChanges(); return(cartItems); }
public void Save() { db.SaveChanges(); }