public bool Update(int id, ArticleUpdate value) { var articleUpdated = _articleRepository.Get(id); if (articleUpdated == null) { throw new Exception("Brak artykółu"); } articleUpdated.Description = value.Description; articleUpdated.Title = value.Title; if (value.Images.Count > 0) { if (!articleUpdated.Images.Any(p => p.Image.Path.Equals(value.Images.First().Path))) { var destination = value.Url.Replace(" ", "-"); _fileService.Clear(destination); foreach (var item in value.Images) { var fileName = _fileService.Copy(item.Path, destination); if (fileName == null) { continue; } articleUpdated.Images.Add(new ArticleImage() { Image = new Image() { Path = fileName, CreatedAt = DateTime.Now } }); } } } var category = new List <ArticleCategory>(); foreach (var item in value.Categories) { category.Add(new ArticleCategory() { CategoryId = item }); } articleUpdated.Categories = category; return(_articleRepository.Update(articleUpdated)); }
public async Task <IActionResult> Update(Guid id, ArticleUpdate request) { var found = await Mediator.Send(request with { Id = id, UserId = RequestUserId, }); if (!found) { return(NotFound()); } await SaveChangesAsync(); return(NoContent()); }
public IActionResult Edit([FromBody] ArticleUpdate articleUpdate, int id) { if (articleUpdate == null) { throw new ApiException("Błąd danych"); } if (!ModelState.IsValid) { throw new ApiValidationException("Walidacja"); } if (!this._articleService.Update(id, articleUpdate)) { throw new ApiException("Bład wykonano akcje edycji artykułu"); } return(new ResponseObjectResult("Pomyślnie wykonano akcje edycji artykułu")); }
public async Task <IActionResult> UpdateArticle([FromBody] ArticleUpdate articleUpdate) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var accessToken = GetAccessTokenFormHeaders(); var result = await _articleService.UpdateArticleAsync(articleUpdate.Id, accessToken, articleUpdate.Title, articleUpdate.Content, articleUpdate.Picture); switch (result) { case SystemResponse.AccessDenied: return(StatusCode(401)); case SystemResponse.NotFound: return(NotFound(articleUpdate)); default: return(Ok()); } }
//-------------------------------------------------------------------------------- /// <summary> /// Adds a new article, or changes the matching existing article. /// </summary> /// <param name="article"></param> private void AddOrChangeArticle(ArticleUpdate article) { bool found = false; DateTime now = DateTime.Now; // apply changes for (int i = 0; i < m_articles.Count; i++) { ArticleUpdate item = m_articles[i]; if (item.LatestTitle.ToLower() == article.LatestTitle.ToLower()) { found = true; item.ApplyChanges(article, now, false); break; } } // if the article was not found, it must be new (or the title has changed), // so we'll add it if (!found) { article.ApplyChanges(article, now, true); m_articles.Add(article); } // remove all articles that weren't updated this time around - we need to // traverse the list in reverse order so we don't lose track of our index for (int i = m_articles.Count - 1; i == 0; i--) { ArticleUpdate item = m_articles[i]; if (item.TimeUpdated != now) { m_articles.RemoveAt(i); } } }
//-------------------------------------------------------------------------------- /// <summary> /// Processes the article data scraped from the html file we scraped /// off CodeProject. /// </summary> /// <param name="data"></param> /// <param name="articleNumber"></param> private void ProcessArticle(string data, int articleNumber) { string link = GetArticleLink(data); data = CleanData(data); string[] parts = data.Split('^'); string title = parts[0]; string description = parts[7]; string lastUpdated = GetDataField("Last Update", parts); string pageViews = GetDataField("Page Views", parts).Replace(",", ""); string rating = GetDataField("Rating", parts); string votes = GetDataField("Votes", parts).Replace(",", ""); string popularity = GetDataField("Popularity", parts); string bookmarks = GetDataField("Bookmark Count", parts); // create the AticleData item and add it to the list DateTime lastUpdatedDate; ArticleUpdate article = new ArticleUpdate(); article.LatestLink = string.Format("http://www.codeproject.com{0}", link); article.LatestTitle = title; article.LatestDescription = description; if (DateTime.TryParse(lastUpdated, out lastUpdatedDate)) { article.LatestLastUpdated = lastUpdatedDate; } else { article.LatestLastUpdated = new DateTime(1990, 1, 1); } article.LatestPageViews = Convert.ToInt32(pageViews); article.LatestRating = Convert.ToDecimal(rating); article.LatestVotes = Convert.ToInt32(votes); article.LatestPopularity = Convert.ToDecimal(popularity); article.LatestBookmarks = Convert.ToInt32(bookmarks); AddOrChangeArticle(article); }