public async Task <int> AddProduct(Product newProduct) { if (newProduct.OwnerId <= 0) { var strUserId = _signinManager.Context.User.FindFirstValue(ClaimTypes.NameIdentifier); if (Int32.TryParse(strUserId, out var userId)) { newProduct.OwnerId = userId; } else { return(0); } } newProduct.BorrowsDays = (int)(newProduct.AvailableUntil - newProduct.AvailableFrom).TotalDays; _context.Add(newProduct); var result = await _context.SaveChangesAsync(); if (result > 0) { await _ml.PredictById(newProduct.Id); } return(result); }
public async Task <int> AddBorrow(Borrow newBorrow, int productId) { var strUserId = _signinManager.Context.User.FindFirstValue(ClaimTypes.NameIdentifier); var product = await _context.Product.FirstOrDefaultAsync(pro => pro.Id == productId); // Check if the user is the owner of this products var user = await _userManager.FindByIdAsync(strUserId); if (user.MyProducts.Contains(product)) { return(0); } newBorrow.ProductId = productId; newBorrow.BorrowerId = product.OwnerId; if (Int32.TryParse(strUserId, out var userId)) { newBorrow.BorrowerId = userId; } else { return(0); } newBorrow.StartDate = product.AvailableFrom; newBorrow.EndDate = product.AvailableUntil; newBorrow.Fine = product.Price; _context.Add(newBorrow); return(await _context.SaveChangesAsync()); }
public async Task <IActionResult> Create(Branch branch) { if (ModelState.IsValid) { _context.Add(branch); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(branch)); }
public async Task <int> AddCategory(Category newCategory) { _context.Add(newCategory); return(await _context.SaveChangesAsync()); }