Ejemplo n.º 1
0
        public void TestViewHistory()
        {
            var sut = new UserViewHistory()
            {
                HistoryId = 3,
                ViewDate  = DateTime.Today,
                RecipeId  = 34,
                Recipe    = new Recipe(),
                User      = new User(),
                UserId    = 32,
            };

            var expected = DateTime.Today;
            var result   = sut.ViewDate;

            Assert.Equal(expected, result);
        }
Ejemplo n.º 2
0
        public async Task <Recipe> AddNewPrepare(int recipeId, string sub)
        {
            var dbrecipe = await _context.Recipes.Where(r => r.RecipeId == recipeId).FirstOrDefaultAsync();

            if (dbrecipe == null)
            {
                return(null);
            }

            var dbuser = await _context.Users.Where(u => u.Auth0 == sub)
                         .Include(u => u.UserSearchHistories)
                         .FirstOrDefaultAsync();

            if (dbuser == null)
            {
                return(null);
            }

            if (await _context.UserViewHistories.AnyAsync(h => h.RecipeId == recipeId && dbuser.Auth0 == sub))
            {
                System.Console.WriteLine("user viewed before");
                return(null);
            }

            dbrecipe.NumTimesPrepared++;
            if (_context.SaveChanges() <= 0)
            {
                System.Console.WriteLine("couldn't save prepare");
                return(null);
            }

            var history = new UserViewHistory();

            history.ViewDate = DateTime.Now;
            history.RecipeId = dbrecipe.RecipeId;
            history.UserId   = dbuser.UserId;
            _context.UserViewHistories.Add(history);
            if (_context.SaveChanges() <= 0)
            {
                System.Console.WriteLine("couldn't save view history");
                return(null);
            }
            return(dbrecipe);
        }