Exemplo n.º 1
0
 public static void addVote(string siteName)
 {
     using (RecipezeEntities db = new RecipezeEntities())
     {
         try
         {
             var numOfVotes = db.Votes.Where(a => a.siteName == siteName).ToList();
             if (numOfVotes.Count == 0)
             {
                 VotesDTO voting = new VotesDTO()
                 {
                     siteName = siteName, voteNumbers = 1
                 };
                 db.Votes.Add(CONVERTERS.VotesConverter.ConvertVoteToDAL(voting));
                 db.SaveChanges();
             }
             else
             {
                 var voteUpdate = db.Votes.Where
                                  (
                     p => siteName.Contains(p.siteName)
                                  );
                 foreach (Vote p in voteUpdate)
                 {
                     p.voteNumbers++;
                 }
                 db.SaveChanges();
             }
         }
         catch (Exception e)
         {
             Console.WriteLine(e);
         }
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// adds new user to database
 /// </summary>
 /// <param name="user"> UserDTO </param>
 public static void AddUser(UserDTO user)
 {
     using (RecipezeEntities db = new RecipezeEntities())
     {
         db.Users.Add(CONVERTERS.UserConverter.ConvertUserToDAL(user));
         db.SaveChanges();
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// adds a recipe to database
 /// </summary>
 /// <param name="userId"> int </param>
 /// <param name="recipe"> RecipeDTO </param>
 public static void AddRecipeToCookbook(int userId, RecipeDTO recipe)
 {
     using (RecipezeEntities db = new RecipezeEntities())
     {
         db.CookbookRecipes.Add(CONVERTERS.RecipeConverter.ConvertRecipeToDAL(recipe, userId));
         db.SaveChanges();
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// method removes a recipe from database
 /// </summary>
 /// <param name="recipeId"> int </param>
 /// <param name="userId"> int </param>
 public static void DeleteRecipeFromCookbook(int recipeId, int userId)
 {
     using (RecipezeEntities db = new RecipezeEntities())
     {
         //gets the user and recipe from database
         var user = db.Users.Where(a => a.UserId == userId).ToList();
         var recp = user[0].CookbookRecipes.FirstOrDefault(c => c.recipeId == recipeId);
         //remove from database
         db.Ingredients.RemoveRange(recp.Ingredients);
         db.Instructions.RemoveRange(recp.Instructions);
         db.CookbookRecipes.Remove(recp);
         db.SaveChanges();
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// define allergies for current user, add new ones and delete old ones
 /// </summary>
 /// <param name="userId"></param>
 /// <param name="allergies"></param>
 /// <returns> true, if allergies added successfully </returns>
 public static bool DefineAllergiesForUser(int userId, List <int> allergies)
 {
     using (RecipezeEntities db = new RecipezeEntities())
     {
         //get user from database by Id
         User user = db.Users.FirstOrDefault(u => u.UserId == userId);
         user.Allergies.Clear();
         //update selected allergies the user chose
         allergies.ForEach(
             a =>
         {
             user.Allergies.Add(db.Allergies.FirstOrDefault(al => al.AllergyCode == a));
         });
         db.SaveChanges();
         return(true);
     }
 }