public async Task DelNews(int id) { var news = await _dbContext.News.AsNoTracking().SingleOrDefaultAsync(m => m.NewsID == id); if (news != null) { _dbContext.News.Remove(news); await _dbContext.SaveChangesAsync(); } }
public async Task <List <NewsTag> > InsertNewsTags(string[] tags, string newsId = null) { string tagId; List <NewsTag> newsTags = new List <NewsTag>(); var allTags = _context.Tags.ToList(); newsTags.AddRange(allTags.Where(n => tags.Contains(n.TagName)).Select(c => new NewsTag { TagId = c.TagId, NewsId = newsId }).ToList()); var newTags = tags.Where(n => !allTags.Select(t => t.TagName).Contains(n)).ToList(); foreach (var item in newTags) { tagId = StringExtensions.GenerateId(10); _context.Tags.Add(new Tag { TagName = item, TagId = tagId }); newsTags.Add(new NewsTag { TagId = tagId, NewsId = newsId }); } await _context.SaveChangesAsync(); return(newsTags); }
public async Task <IActionResult> PutNews([FromRoute] int id, [FromBody] News news) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != news.NewsId) { return(BadRequest()); } //I need to get the date from the context //Datetime needs to be fixed. DateTime today = new DateTime(); today = DateTime.Now; news.UpdatedDate = today; _context.Entry(news).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!NewsExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <News> GetNews(int newsid) { var news = await _dbContext.News.AsNoTracking().FirstOrDefaultAsync(n => n.NewsID == newsid); if (news != null) { news.Hit = news.Hit + 1; _dbContext.Update(news); await _dbContext.SaveChangesAsync(); return(news); } else { return(null); } }