Esempio n. 1
0
        public async Task <RecipesDTO> Get(int id)
        {
            var result = new RecipesDTO();

            try
            {
                var response = await client.GetAsync($"{RecipesUri}/{id}");

                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();

                    result = JsonConvert.DeserializeObject <RecipesDTO>(content);
                }
                else
                {
                    throw new Exception(response.StatusCode.ToString());
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return(result);
        }
        public ActionResult Put([FromBody] RecipesDTO recipesDto)
        {
            if (RecipesDAO.Update(recipesDto))
            {
                return(Ok());
            }

            return(BadRequest());
        }
Esempio n. 3
0
        private static async Task ConvertToBase64Async(RecipesDTO recipesDTO)
        {
            var path = System.IO.Path.Combine(
                Directory.GetCurrentDirectory(), "wwwroot/images",
                recipesDTO.ImageFile.FileName);

            using (var stream = new FileStream(path, FileMode.Create))
            {
                await recipesDTO.ImageFile.CopyToAsync(stream);
            }
            var byteArray = await System.IO.File.ReadAllBytesAsync(path);

            recipesDTO.Image = Convert.ToBase64String(byteArray);
        }
Esempio n. 4
0
        public async Task <IActionResult> Create([Bind("ID,Name,Image,ImageFile,Body,CategoryId")] RecipesDTO recipesDTO)
        {
            if (recipesDTO.ImageFile == null || recipesDTO.ImageFile.Length == 0)
            {
                return(View(recipesDTO));
            }
            await ConvertToBase64Async(recipesDTO);

            if (ModelState.IsValid)
            {
                await recipesRepo.Add(recipesDTO);

                return(RedirectToAction(nameof(Index)));
            }
            // ViewData["CategoryId"] = new SelectList(_context.Set<CategoryDTO>(), "ID", "ID", recipesDTO.CategoryId);
            return(View(recipesDTO));
        }
Esempio n. 5
0
        public async Task <RecipesDTO> Add(RecipesDTO item)
        {
            try
            {
                var response = await client.PostAsJsonAsync(RecipesUri, item);

                if (response.IsSuccessStatusCode)
                {
                    return(item);
                }
                return(null);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Esempio n. 6
0
        public async Task <bool> Update(RecipesDTO item)
        {
            try
            {
                var response = await client.PutAsJsonAsync($"{RecipesUri}/{item.ID}", item);

                if (response.IsSuccessStatusCode)
                {
                    return(true);
                }
                return(false);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Esempio n. 7
0
 public static RecipesDTO Post(RecipesDTO recipe)
 {
     using (SqlConnection connection = DataBase.GetConnection())
     {
         connection.Open();
         SqlCommand command = connection.CreateCommand();
         command.CommandText = REQ_POST;
         command.Parameters.AddWithValue($@"{FIELD_ID_USER}", recipe.IdUser);
         command.Parameters.AddWithValue($@"{FIELD_NAME_RECIPE}", recipe.NameRecipe);
         command.Parameters.AddWithValue($@"{FIELD_POSTDATE}", recipe.PostDate);
         command.Parameters.AddWithValue($@"{FIELD_SUMMARY}", recipe.Summary);
         command.Parameters.AddWithValue($@"{FIELD_PERSONS}", recipe.Persons);
         command.Parameters.AddWithValue($@"{FIELD_PREPTIME}", recipe.PrepTime);
         command.Parameters.AddWithValue($@"{FIELD_SPICES_RATE}", recipe.SpiceRate);
         command.Parameters.AddWithValue($@"{FIELD_RECIPE_TYPE}", recipe.RecipeType);
         recipe.IdRecipe = (int)command.ExecuteScalar();
     }
     return(recipe);
 }
Esempio n. 8
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,Name,Image,Body,CategoryId")] RecipesDTO recipesDTO)
        {
            if (id != recipesDTO.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    await recipesRepo.Update(recipesDTO);
                }
                catch (DbUpdateConcurrencyException)
                {
                    return(NotFound());
                }
                return(RedirectToAction(nameof(Index)));
            }
            // ViewData["CategoryId"] = new SelectList(_context.Set<CategoryDTO>(), "ID", "ID", recipesDTO.CategoryId);
            return(View(recipesDTO));
        }
Esempio n. 9
0
        public static bool Update(RecipesDTO recipe)
        {
            bool hasBeenChanged = false;

            using (SqlConnection connection = DataBase.GetConnection())
            {
                connection.Open();
                SqlCommand command = connection.CreateCommand();
                command.CommandText = REQ_UPDATE;
                command.Parameters.AddWithValue($@"{FIELD_ID_RECIPE}", recipe.IdRecipe);
                command.Parameters.AddWithValue($@"{FIELD_ID_USER}", recipe.IdUser);
                command.Parameters.AddWithValue($@"{FIELD_PERSONS}", recipe.Persons);
                command.Parameters.AddWithValue($@"{FIELD_SUMMARY}", recipe.Summary);
                command.Parameters.AddWithValue($@"{FIELD_POSTDATE}", recipe.PostDate);
                command.Parameters.AddWithValue($@"{FIELD_PREPTIME}", recipe.PrepTime);
                command.Parameters.AddWithValue($@"{FIELD_NAME_RECIPE}", recipe.NameRecipe);
                command.Parameters.AddWithValue($@"{FIELD_RECIPE_TYPE}", recipe.RecipeType);
                command.Parameters.AddWithValue($@"{FIELD_SPICES_RATE}", recipe.SpiceRate);
                hasBeenChanged = command.ExecuteNonQuery() == 1;
            }

            return(hasBeenChanged);
        }
 public RecipesDTO Post([FromBody] RecipesDTO recipesDto)
 {
     return(RecipesDAO.Post(recipesDto));
 }
Esempio n. 11
0
 public RecipesDTO Post([FromBody] RecipesDTO recipesDto)
 {
     Console.WriteLine(recipesDto);
     return(RecipesDAO.Post(recipesDto));
 }