public async Task <string> Edit(string newsId, NewsCreateEditModel newsModified) { if (string.IsNullOrWhiteSpace(newsModified.Content) || string.IsNullOrWhiteSpace(newsModified.ImageUrl) || string.IsNullOrWhiteSpace(newsModified.Title)) { throw new ArgumentException(ErrorMessages.InvalidNewsParameters); } if (!this.db.News.Any(n => n.Id == newsId)) { throw new ArgumentException(ErrorMessages.InvalidNewsId); } News news = await this.db.News .Where(n => n.Id == newsId) .FirstOrDefaultAsync(); news.Title = newsModified.Title; news.Content = newsModified.Content; news.ImageUrl = newsModified.ImageUrl; await this.db.SaveChangesAsync(); return(news.Id); }
public async Task <string> Create(NewsCreateEditModel data) { if (string.IsNullOrWhiteSpace(data.Title) || string.IsNullOrWhiteSpace(data.Content) || string.IsNullOrWhiteSpace(data.ImageUrl)) { throw new ArgumentException(ErrorMessages.InvalidNewsParameters); } DateTime creationDate = this.GetSofiaLocalTime(); News news = new News { Title = data.Title, Content = data.Content, ImageUrl = data.ImageUrl, CreationDate = creationDate }; try { await this.db.News.AddAsync(news); await this.db.SaveChangesAsync(); } catch { throw new InvalidOperationException(ErrorMessages.UnableToWriteToDb); } return(news.Id); }
public async Task <IActionResult> Edit(string id, [FromBody] NewsCreateEditModel news) { return(await this.Execute(true, true, async() => { await this.news.Edit(id, news); return this.Ok(new { newsId = id }); })); }
public async Task <IActionResult> Create([FromBody] NewsCreateEditModel news) { return(await this.Execute(true, true, async() => { string newsId = await this.news.Create(news); return this.Ok(new { newsId = newsId }); })); }