示例#1
0
        public ActionResult SubmitCreate(RecipeModel rm)
        {
            RecipeCaller rc = new RecipeCaller(ConfigurationManager.AppSettings["RecipeFinderApiBaseUrl"]);

            RecipeDTO recipeToBeCreated = new RecipeDTO();

            recipeToBeCreated.Title = rm.Title;
            recipeToBeCreated.User  = new UserDTO()
            {
                Id = 1
            };
            recipeToBeCreated.Instruction     = rm.Instruction;
            recipeToBeCreated.IngredientLines = new List <IngredientLineDTO>();
            recipeToBeCreated.Images          = new List <ImageDTO>();

            if (rm.IngredientLines != null)
            {
                foreach (var item in rm.IngredientLines)
                {
                    IngredientLineDTO lineDTO = new IngredientLineDTO();
                    lineDTO.Ingredient = new IngredientDTO()
                    {
                        Name = item.Ingredient.Name
                    };
                    lineDTO.Amount          = item.Amount;
                    lineDTO.MeasureUnitText = item.MeasureUnit;

                    recipeToBeCreated.IngredientLines.Add(lineDTO);
                }
            }
            if (rm.Images != null)
            {
                foreach (var item in rm.Images)
                {
                    ImageDTO image = new ImageDTO();
                    image.FileName = item.FileName;

                    recipeToBeCreated.Images.Add(image);
                }
            }

            RFApiResult response = rc.CreateRecipe(recipeToBeCreated);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                SetFlash(FlashMessageType.Success, response.Message);
            }
            else
            {
                SetFlash(FlashMessageType.Danger, response.Message);
            }

            return(Redirect(Url.Action("Index", "Home")));
        }
示例#2
0
        public async Task <RecipeDTO> GetByIdAsync(int id)
        {
            var getRecipe = await dbAccess.Recipes.GetByIdAsync(id);

            //Check if recipe exists
            if (getRecipe == null)
            {
                throw new ArgumentNullException("The recipe could not be found!");
            }

            //Convert user info to DTO
            var user = await dbAccess.Users.GetByIdAsync(getRecipe.UserId);

            UserDTO uResult = new UserDTO();

            uResult.Id       = user.Id;
            uResult.Username = user.Username;

            //Create list for ingredientlines
            List <IngredientLineDTO> ilResults = new List <IngredientLineDTO>();
            //Fetch ingredientlines in DB based on recipeId
            var ingredientLines = (await dbAccess.IngredientLines.FindByCondition(nameof(IngredientLine.RecipeId), getRecipe.Id)).ToList();

            //Loop over ingredientlines in DB, convert to DTO and add to new list of ingredientlines
            foreach (var il in ingredientLines)
            {
                IngredientLineDTO ingredientLineDTO = new IngredientLineDTO();
                var ingredient = await dbAccess.Ingredients.GetByIdAsync(il.IngredientId);

                IngredientDTO ingredientDTO = new IngredientDTO();
                ingredientDTO.Id                 = ingredient.Id;
                ingredientDTO.Name               = ingredient.Name;
                ingredientLineDTO.Ingredient     = ingredientDTO;
                ingredientLineDTO.Amount         = il.Amount;
                ingredientLineDTO.MeasureUnit    = il.MeasureUnit;
                ingredientLineDTO.MeasureUnitInt = (int)il.MeasureUnit;

                ilResults.Add(ingredientLineDTO);
            }

            //Create list for images
            List <ImageDTO> imResults = new List <ImageDTO>();
            //Fetch images in DB based on recipeId
            var images = (await dbAccess.Images.FindByCondition(nameof(Image.RecipeId), getRecipe.Id)).ToList();

            //Loop over images in DB, convert to DTO and add to new list of images
            foreach (var im in images)
            {
                ImageDTO imageDTO = new ImageDTO();
                imageDTO.Id       = im.Id;
                imageDTO.FileName = im.FileName;

                imResults.Add(imageDTO);
            }

            //Convert recipe to DTO and return result
            RecipeDTO result = new RecipeDTO();

            result.User            = uResult;
            result.Id              = getRecipe.Id;
            result.Title           = getRecipe.Title;
            result.Slug            = getRecipe.Slug;
            result.Instruction     = getRecipe.Instruction;
            result.CreatedAt       = getRecipe.CreatedAt;
            result.IngredientLines = ilResults;
            result.RowVer          = getRecipe.RowVer;
            result.Images          = imResults;

            return(result);
        }
示例#3
0
        public async Task <IEnumerable <RecipeDTO> > GetAllAsync()
        {
            List <RecipeDTO> recipeList = new List <RecipeDTO>();
            var resultSet = (await dbAccess.Recipes.GetAllAsync()).ToList();

            if (resultSet == null)
            {
                throw new ArgumentNullException("No recipes were found!");
            }

            foreach (var item in resultSet)
            {
                //Convert user info to DTO
                var user = await dbAccess.Users.GetByIdAsync(item.UserId);

                UserDTO uResult = new UserDTO();
                uResult.Id       = user.Id;
                uResult.Username = user.Username;

                //Create list for ingredientlines
                List <IngredientLineDTO> ilResults = new List <IngredientLineDTO>();
                //Fetch ingredientlines in DB based on recipeId
                var ingredientLines = (await dbAccess.IngredientLines.FindByCondition(nameof(IngredientLine.RecipeId), item.Id)).ToList();
                //Loop over ingredientlines in DB, convert to DTO and add to new list of ingredientlines
                foreach (var il in ingredientLines)
                {
                    IngredientLineDTO ingredientLineDTO = new IngredientLineDTO();
                    var ingredient = await dbAccess.Ingredients.GetByIdAsync(il.IngredientId);

                    IngredientDTO ingredientDTO = new IngredientDTO();
                    ingredientDTO.Name            = ingredient.Name;
                    ingredientDTO.Id              = ingredient.Id;
                    ingredientLineDTO.Id          = il.Id;
                    ingredientLineDTO.Ingredient  = ingredientDTO;
                    ingredientLineDTO.Amount      = il.Amount;
                    ingredientLineDTO.MeasureUnit = il.MeasureUnit;

                    ilResults.Add(ingredientLineDTO);
                }

                //Create list for images
                List <ImageDTO> imResults = new List <ImageDTO>();
                //Fetch images in DB based on recipeId
                var images = (await dbAccess.Images.FindByCondition(nameof(Image.RecipeId), item.Id)).ToList();
                //Loop over images in DB, convert to DTO and add to new list of images
                foreach (var im in images)
                {
                    ImageDTO imageDTO = new ImageDTO();
                    imageDTO.Id       = im.Id;
                    imageDTO.FileName = im.FileName;

                    imResults.Add(imageDTO);
                }

                //Convert recipe to DTO and return result
                RecipeDTO result = new RecipeDTO();
                result.User            = uResult;
                result.Id              = item.Id;
                result.Title           = item.Title;
                result.Slug            = item.Slug;
                result.Instruction     = item.Instruction;
                result.CreatedAt       = item.CreatedAt;
                result.IngredientLines = ilResults;
                result.Images          = imResults;
                result.RowVer          = item.RowVer;

                recipeList.Add(result);
            }
            return(recipeList);
        }