public async Task <ProductReturnDto> Register(ProductRegisterDto product) { var newProduct = new Product() { Guid = Guid.NewGuid().ToString(), Barcode = product.Barcode, Name = product.Name, Description = product.Description, Count = product.Count, Mass = product.Mass, Price = product.Price }; await _context.Products.AddAsync(newProduct); var result = await _context.SaveChangesAsync(); if (result > 0) { var returnValue = await Product(newProduct.Guid); return(returnValue); } return(null); }
public async Task <(UserReturnDto userCredentials, string token)> Register(UserRegisterDto userCreds) { var emailExists = await EmailExists(userCreds.Email); if (!emailExists) { User newUser = new User() { Guid = Guid.NewGuid().ToString(), Email = userCreds.Email, NormalizedEmail = userCreds.Email.ToLower(), Username = userCreds.Username, NormalizedUsername = userCreds.Username.ToLower(), PasswordHash = Helper.ComputeHash(userCreds.Password) }; await _context.Users.AddAsync(newUser); await _context.SaveChangesAsync(); var loginResult = await Login(new UserLoginDto() { Email = userCreds.Email, Password = userCreds.Password }); return(loginResult); } return(null, null); }
public async Task <(TransactionReturnDto transaction, List <ProductReturnDto> unableProducts)> Create(string uGuid, List <ProductBuyDto> wantedProducts) { // Create transaction: var transaction = new Transaction() { Guid = Guid.NewGuid().ToString(), IssueDate = DateTime.Now, IssuerGuid = uGuid, Status = TransactionStatus.Pending, ApprovalDate = null, CancellationDate = null }; // Create SoldProducts(Content field of Transaction) & calc total amount: decimal amount = 0; var content = new List <SoldProduct>(); // variable to store products which user couln't buy: var unableToBuyProducts = new List <ProductReturnDto>(); for (int i = 0; i < wantedProducts.Count; i++) { // get product with wantedProducts' Guid field: var product = await _context.Products .Select(p => new { StockCount = p.Count, SoldProduct = new SoldProduct() { Guid = Guid.NewGuid().ToString(), ProductGuid = p.Guid, SoldCount = wantedProducts[i].Count, TransactionGuid = transaction.Guid } }) .FirstOrDefaultAsync( p => p.SoldProduct.ProductGuid == wantedProducts[i].Guid && p.StockCount >= wantedProducts[i].Count ); if (product != null) { // If product is found add it to transaction content // and add price to amount: var p = await _context.Products .FirstOrDefaultAsync(p => p.Guid == wantedProducts[i].Guid); amount += p.Price * product.SoldProduct.SoldCount; content.Add(product.SoldProduct); } else { // To return products which user couldn't buy add them to 'unableToBuyProducts' list: var unableProduct = await _productsRepo .Product(wantedProducts[i].Guid); unableToBuyProducts.Add(unableProduct); } } transaction.Content = content; transaction.Amount = amount; await _context.Transactions.AddAsync(transaction); var result = await _context.SaveChangesAsync(); // Return transaction: if (result > 0) { var transactionReturn = await GetTransaction(transaction.Guid); return(transactionReturn, unableToBuyProducts); } return(null, null); }