Exemplo n.º 1
0
        public async Task <Boolean> UpdateArticleAsync(ArticleUploadDTO articleDTO)
        {
            try
            {
                HttpResponseMessage response = await _client.PutAsJsonAsync(API_ARTICLES, articleDTO);

                return(response.IsSuccessStatusCode);
            }
            catch (Exception ex)
            {
                throw new PersistenceUnavailableException(ex);
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Post([FromBody] ArticleUploadDTO articleDTO)
        {
            try
            {
                if (articleDTO.Leading && articleDTO.NewImages.Length < 1)
                {
                    ModelState.AddModelError("", "At least one image is required for a leading article");
                }
                if (ModelState.IsValid)
                {
                    Article article = new Article()
                    {
                        Content     = articleDTO.Content,
                        Leading     = articleDTO.Leading,
                        Title       = articleDTO.Title,
                        Description = articleDTO.Description
                    };
                    ArticleImage[] images = TryCreateImages(articleDTO.NewImages, article);
                    if (images == null)
                    {
                        ModelState.AddModelError("", "An image was invalid format");
                    }
                    else
                    {
                        article.Author = await GetCurrentAuthor();

                        var result = await newsContext.Articles.AddAsync(article);

                        await newsContext.Images.AddRangeAsync(images);

                        await newsContext.SaveChangesAsync();

                        return(Ok(new ArticleDTO()
                        {
                            Content = result.Entity.Content,
                            Description = result.Entity.Description,
                            Id = result.Entity.Id,
                            Images = null,
                            Title = result.Entity.Title,
                            Leading = result.Entity.Leading
                        }));
                    }
                }
                return(BadRequest());
            }
            catch (Exception ex)
            {
                return(StatusCode(500));
            }
        }
Exemplo n.º 3
0
 public async Task <Boolean> CreateArticleAsync(ArticleUploadDTO articleDTO)
 {
     try
     {
         using (HttpResponseMessage response = await _client.PostAsJsonAsync(API_ARTICLES, articleDTO))
         {                                                                           // az értékeket azonnal JSON formátumra alakítjuk
             articleDTO.Id = (await response.Content.ReadAsAsync <ArticleDTO>()).Id; // a válaszüzenetben megkapjuk a végleges azonosítót
             return(response.IsSuccessStatusCode);
         }
     }
     catch (Exception ex)
     {
         throw new PersistenceUnavailableException(ex);
     }
 }
Exemplo n.º 4
0
        public async Task <IActionResult> Put([FromBody] ArticleUploadDTO articleDTO)
        {
            try
            {
                Author author = await GetCurrentAuthor();

                Article original = newsContext.Articles
                                   .Include(article => article.Author)
                                   .Include(article => article.Images)
                                   .Where(article => article.Author == author && article.Id == articleDTO.Id)
                                   .FirstOrDefault();
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }
                if (original == null)
                {
                    ModelState.AddModelError("", "No article by given author by given id.");
                    return(BadRequest());
                }
                if (articleDTO.Leading)
                {
                    if (articleDTO.DeleteImages)
                    {
                        if (articleDTO.NewImages.Length < 1)
                        {
                            ModelState.AddModelError("", "At least one image is required for a leading article");
                            return(BadRequest());
                        }
                    }
                    else
                    {
                        if (articleDTO.NewImages.Length + original.Images.ToArray().Length < 1)
                        {
                            ModelState.AddModelError("", "At least one image is required for a leading article");
                            return(BadRequest());
                        }
                    }
                }
                ArticleImage[] newImages = TryCreateImages(articleDTO.NewImages, original);
                if (newImages == null)
                {
                    ModelState.AddModelError("", "An image was invalid format");
                    return(BadRequest());
                }

                if (articleDTO.DeleteImages)
                {
                    foreach (var image in original.Images)
                    {
                        newsContext.Images.Remove(image);
                    }
                    original.Images.Clear();
                }
                newsContext.Images.AddRange(newImages);
                original.Title       = articleDTO.Title;
                original.Leading     = articleDTO.Leading;
                original.Description = articleDTO.Description;
                original.Content     = articleDTO.Content;
                newsContext.Articles.Update(original);
                await newsContext.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode(500));
            }
        }
Exemplo n.º 5
0
 public async Task <bool> UpdateArticleAsync(ArticleUploadDTO article)
 {
     return(true);
 }
Exemplo n.º 6
0
 public async Task <bool> CreateArticleAsync(ArticleUploadDTO article)
 {
     article.Id = 10;
     return(true);
 }