public HttpResponseMessage DeleteUser(int id)
        {
            var dbContext = new RecipesContext();
            using (dbContext)
            {
                var user = GetCurrentUser(dbContext);
                if (user == null || user.Role != Role.Admin)
                {
                    throw new InvalidOperationException("Only an authorized admin can delete users.");
                }

                var userToDelete = dbContext.Users.FirstOrDefault(u => u.UserId == id);

                if (userToDelete != null)
                {
                    dbContext.Users.Remove(userToDelete);
                    dbContext.SaveChanges();
                }

                var response = this.Request.CreateResponse(HttpStatusCode.OK, string.Empty);
                return response;
            }
        }
        public HttpResponseMessage PostLoginUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var dbContext = new RecipesContext();
                using (dbContext)
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateAuthCode(model.AuthCode);

                    var usernameToLower = model.Username.ToLower();

                    var user = dbContext.Users.FirstOrDefault(
                        usr => usr.Username == usernameToLower
                        && usr.AuthCode == model.AuthCode);

                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid username or password");
                    }

                    if (user.SessionKey == null)
                    {
                        user.SessionKey = this.GenerateSessionKey(user.UserId);
                        dbContext.SaveChanges();
                    }

                    var loggedModel = new LoggedUserModel()
                    {
                        Username = user.Username,
                        SessionKey = user.SessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel);
                    return response;
                }
            });

            return responseMsg;
        }
        public HttpResponseMessage PutLogoutUser()
        {
            var dbContext = new RecipesContext();
            using (dbContext)
            {
                var user =  GetCurrentUser(dbContext);
                if (user != null)
                {
                    user.SessionKey = null;
                    dbContext.SaveChanges();
                }

                var response = this.Request.CreateResponse(HttpStatusCode.NoContent);
                return response;
            }
        }
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var dbContext = new RecipesContext();
                using (dbContext)
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateAuthCode(model.AuthCode);

                    var usernameToLower = model.Username.ToLower();

                    var user = dbContext.Users.FirstOrDefault(
                        usr => usr.Username == usernameToLower);

                    if (user != null)
                    {
                        throw new InvalidOperationException("User exists");
                    }

                    user = new User()
                    {
                        Username = model.Username.ToLower(),
                        AuthCode = model.AuthCode,
                        Role = Role.Client
                    };

                    dbContext.Users.Add(user);
                    dbContext.SaveChanges();

                    user.SessionKey = this.GenerateSessionKey(user.UserId);
                    dbContext.SaveChanges();

                    var loggedModel = new LoggedUserModel()
                    {
                        Username = user.Username,
                        SessionKey = user.SessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel);
                    return response;
                }
            });

            return responseMsg;
        }
        public HttpResponseMessage Add(RecipeDetails recipe)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new RecipesContext();

                var user = GetCurrentUser(context);

                var category = context.Categories.FirstOrDefault(x => x.Title == recipe.CategoryName);

                if (category == null)
                {
                    throw new ArgumentException("No such category");
                }

                var addedRecipe = new Recipe()
                    {
                        Category = category,
                        Content = recipe.Content,
                        Creator = user,
                        PublishDate = DateTime.Now,
                        Title = recipe.Title
                    };

                foreach (var product in recipe.Products)
                {
                    var productToAdd = context.Products.FirstOrDefault(x => x.Title == product.Name);

                    if (productToAdd == null)
                    {
                        productToAdd = new Product { Title = product.Name };
                    }

                    addedRecipe.Products.Add(new Ingredient()
                    {
                        Product = productToAdd,
                        Quantity = product.Quantity,
                        Mesaurement = (Measurement)Enum.Parse(typeof(Measurement), product.Measurement)

                    });
                }

                context.Recipes.Add(addedRecipe);
                context.SaveChanges();

                var response = this.Request.CreateResponse(HttpStatusCode.Created, addedRecipe);
                return response;
            });

            return responseMsg;
        }
        public HttpResponseMessage Delete(int id)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new RecipesContext();

                var user = GetCurrentUser(context);

                var recipe = context.Recipes.FirstOrDefault(x => x.Id == id);

                if (recipe == null)
                {
                    throw new ArgumentException("No such recipe.");
                }

                if (recipe.Creator.UserId != user.UserId && user.Role != Role.Admin)
                {
                    throw new Exception("This user doesn't have the permissions to delete this recipe.");
                }

                context.Recipes.Remove(recipe);
                user.MyRecipes.Remove(recipe);

                context.SaveChanges();
                var response = this.Request.CreateResponse(HttpStatusCode.OK);
                return response;
            });

            return responseMsg;
        }
        public HttpResponseMessage Like(int id)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new RecipesContext();

                var user = GetCurrentUser(context);

                var recipe = context.Recipes.FirstOrDefault(x => x.Id == id);

                if (recipe == null)
                {
                    throw new ArgumentException("No such recipe.");
                }

                if (!user.Favorites.Contains(recipe))
                {
                    user.Favorites.Add(recipe);
                    context.SaveChanges();
                }
                var response = this.Request.CreateResponse(HttpStatusCode.NoContent);
                return response;
            });

            return responseMsg;
        }