public async Task AddRatingAsync(int recipeId, int rating, int userId, string text) { if (rating < 0) { rating = 0; } if (rating > 5) { rating = 5; } var recipe = await this.context.Recipes.FirstOrDefaultAsync(x => x.Id.Equals(recipeId)); if (recipe == null) { throw new NotFoundException(); } var user = await this.context.Users.FirstOrDefaultAsync(x => x.Id.Equals(userId)); if (user == null) { throw new NotFoundException(); } recipe.Ratings.Add(new Rating { User = user, UserId = user.Id, Recipe = recipe, RecipeId = recipe.Id, Value = rating, Comment = text }); if (rating > 3) { user.FavouriteRecipes.Add(new UserRecipe() { Recipe = recipe, RecipeId = recipe.Id, User = user, UserId = user.Id }); } await context.SaveChangesAsync(); }
public async Task <Userz> Register(Userz user, string password) { byte[] passwordHash, passwordSalt; CreatePasswordHash(password, out passwordHash, out passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; await _dbContext.Userz.AddAsync(user); await _dbContext.SaveChangesAsync(); return(user); }