コード例 #1
0
        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);
        }
コード例 #2
0
        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());
        }
コード例 #3
0
        public async Task <IActionResult> Create(Branch branch)
        {
            if (ModelState.IsValid)
            {
                _context.Add(branch);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(branch));
        }
コード例 #4
0
 public async Task <int> AddCategory(Category newCategory)
 {
     _context.Add(newCategory);
     return(await _context.SaveChangesAsync());
 }