public async Task <Unit> Handle(CreateMeasurementCommand request, CancellationToken cancellationToken) { var user = await _userAuth.GetUser(request.Username); if (user == null) { throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" }); } var createMeasurement = new IngredientMeasurements { Amount = request.Amount, Description = request.Description }; var success = await _measurementGenerator.Create(createMeasurement); if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }
public async Task <Unit> Handle(CreateIngredientCommand request, CancellationToken cancellationToken) { var user = await _userAuth.GetUser(request.Username); if (user == null) { throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" }); } if (await _ingredientGenerator.IsIngredientExits(request.Name, user.Id)) { throw new RestException(HttpStatusCode.BadRequest, new { Ingredient = "Already exist" }); } var createIngredient = new Ingredients { Name = request.Name, Description = request.Description, SlugUrl = request.SlugUrl }; var success = await _ingredientGenerator.Create(user.Id, createIngredient); if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }
public async Task <Unit> Handle(UpdateIngredientCommand request, CancellationToken cancellationToken) { var user = await _userAuth.GetUser(request.Username); if (user == null) { throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" }); } var ingredent = await _ingredientGenerator.GetIngredient(user.Id, request.IngredientId); if (ingredent == null) { throw new RestException(HttpStatusCode.NotFound, new { Ingredent = "Not found" }); } var updateIngredent = new IngredientDto { Name = request.Name ?? ingredent.Name, Description = request.Description ?? ingredent.Description }; var success = await _ingredientGenerator.Update(user.Id, request.IngredientId, updateIngredent); if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }
public async Task <Unit> Handle(DeleteRecipeCommand request, CancellationToken cancellationToken) { var user = await _userAuth.GetUser(request.Username); if (user == null) { throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" }); } foreach (var recipeId in request.RecipeIds) { var recipe = await _recipeGenerator.GetRecipe(recipeId, user.Id); if (recipe == null) { throw new RestException(HttpStatusCode.NotFound, new { Recipe = "Not found" }); } } var success = await _recipeGenerator.Delete(user.Id, request.RecipeIds); if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }
public async Task <RecipeDto> Handle(CreateRecipeCommand request, CancellationToken cancellationToken) { var user = await _userAuth.GetUser(request.Username); if (user == null) { throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" }); } if (await _recipeGenerator.IsRecipeExitsWithSlug(request.Title, user.Id, request.SlugUrl)) { throw new RestException(HttpStatusCode.BadRequest, new { Recipe_Slug = "Already exist" }); } var toCreateRecipe = new Recipe { Title = request.Title, Description = request.Description, SlugUrl = request.SlugUrl, Category = request.Category, }; var createdRecipe = await _recipeGenerator.Create(user.Id, toCreateRecipe); return(createdRecipe); }
public async Task <List <IngredientDto> > Handle(GetIngredientsQuery request, CancellationToken cancellationToken) { var user = await _userAuth.GetUser(request.Username); if (user == null) { throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" }); } var ingredients = await _ingredientGenerator.GetIngredients(user.Id); return(ingredients); }
public async Task <List <Dish> > Handle(GetDishesQuery request, CancellationToken cancellationToken) { string selectCommandText = @"SELECT Dish.DishId, Dish.DishTitle, Dish.DishDescription, Dish.DishSlug, DishCategory.DishCategoryId, DishCategory.DishCategoryTitle FROM Dish JOIN DishCategory ON Dish.DishCategoryId = DishCategory.DishCategoryId"; int userId = 0; if (!string.IsNullOrEmpty(request.Username)) { var user = await _userAuth.GetUser(request.Username); if (user == null) { throw new RestException(HttpStatusCode.NotFound, new { User = "******" }); } userId = user.Id; selectCommandText += @" WHERE Dish.UserId = @userId"; } selectCommandText += @" ORDER BY Dish.DishId OFFSET @offset ROWS FETCH NEXT @limit ROWS ONLY"; var dishesFromDB = await _dish.GetDishes(userId, selectCommandText, request.Offset, request.Limit); if (dishesFromDB.Count > 0) { selectCommandText = @"SELECT Ingredients.IngredientId, Ingredients.IngredientName, Ingredients.IngredientDescription, Ingredients.IngredientSlug, Recipe.Amount FROM Recipe JOIN Ingredients ON Recipe.IngredientId = Ingredients.IngredientId WHERE Recipe.DishId = @dishId"; foreach (var dish in dishesFromDB) { dish.Ingredients = await _ingredient.GetIngredientsByDishId(dish.Id, selectCommandText); } } return(dishesFromDB); }
public async Task <GoodFoodUserDto> Handle(LoginUserCommand request, CancellationToken cancellationToken) { var user = await _userAuth.GetUser(request.UserName); if (user == null) { throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" }); } // if (!verifyPasswordHash(request.Password, user.User_Password_Hash, user.User_Password_Salt)) // throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" }); var returnUser = new GoodFoodUserDto { Username = user.Username + " successfully login" }; return(returnUser); }