public async Task <ActionResult <int> > Create(OrderSubmitDTO newOrder) { Order order = new Order { CustomerId = newOrder.CustomerId, Payment = newOrder.Payment, Status = OrderStatus.PENDING, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, DiscountPercentage = newOrder.DiscountPercentage }; _context.Order.Add(order); await _context.SaveChangesAsync(); List <OrderProduct> orderProducts = new List <OrderProduct>(); foreach (var item in newOrder.OrderProduct) { orderProducts.Add(new OrderProduct { Quantity = item.Quantity, Value = item.Value, OrderId = order.Id, ProductId = item.ProductId }); } _context.OrderProduct.AddRange(orderProducts); await _context.SaveChangesAsync(); return(Ok(order.Id)); }
public async Task <ActionResult <int> > Add(CreateProductDTO newProduct) { var product = _mapper.Map <Product>(newProduct); product.CategoryId = await _categoryRepository.CreateIfNotExists(newProduct.CategoryName); _context.Product.Add(product); await _context.SaveChangesAsync(); return(Ok(product.Id)); }
public async Task <HttpResponse <int> > Register(CreateUserDTO userInfo) { HttpResponse <int> response = new HttpResponse <int>(); User newUser = _mapper.Map <User>(userInfo); if (await UserExists(userInfo.Username)) { response.Success = false; response.Data = -1; return(response); } CreatePasswordHash(userInfo.Password, out byte[] passwordHash, out byte[] passwordSalt); newUser.PasswordSalt = passwordSalt; newUser.PasswordHash = passwordHash; await _context.User.AddAsync(newUser); await _context.SaveChangesAsync(); response.Data = newUser.Id; return(response); }
public async Task <ActionResult <int> > UpdateSingle([FromRoute] int id, UpdateUserDTO userToUpdate) { var existingUser = await _repository.GetSingleWithAddress(id); existingUser.Name = userToUpdate.Name; existingUser.Phone = userToUpdate.Phone; existingUser.Document = userToUpdate.Document; existingUser.Email = userToUpdate.Email; if (existingUser.Address == null) { existingUser.Address = new Address(); } existingUser.Address.Name = userToUpdate.Address.Name; existingUser.Address.City = userToUpdate.Address.City; existingUser.Address.State = userToUpdate.Address.State; existingUser.Address.ZipCode = userToUpdate.Address.ZipCode; _context.Update(existingUser); await _context.SaveChangesAsync(); return(Ok(existingUser.Id)); }