using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Mvc; using mongo1.Data; using mongo1.Models; using System.Linq; using System.Collections.Generic; namespace mongo1.Controllers { [Route("[controller]")] [ApiController] public class ArticleController : Controller { private readonly ApplicationDbContext _context; public ArticleController(ApplicationDbContext context) { _context = context; } [HttpGet] public List
GetArticles() { // var articles = _context.Articles.ToList(); var articles = _context.Articles.Include(a => a.Comments).ToList(); return articles; } [HttpPost] public List
PostArtikkel([FromBody] Article artikkel) { _context.Articles.Add(artikkel); _context.SaveChanges(); return _context.Articles.ToList(); } [HttpDelete("{id}")] public List
DeleteArtikkel(int id) { var artikkel = _context.Articles.Find(id); if (artikkel == null) { return _context.Articles.ToList(); } _context.Articles.Remove(artikkel); _context.SaveChanges(); return _context.Articles.ToList(); } [HttpGet("{id}")] public ActionResult
GetArtikkel(int id) { var artikkel = _context.Articles.Find(id); if (artikkel == null) { return NotFound(); } return artikkel; } [HttpPut("{id}")] public ActionResult> PutArtikkel(int id, [FromBody] Article updatedArtikkel) { var artikkel = _context.Articles.Find(id); if (artikkel == null) { return NotFound(); } artikkel.Header = updatedArtikkel.Header; artikkel.Content = updatedArtikkel.Content; _context.Articles.Update(artikkel); _context.SaveChanges(); return Ok(_context.Articles); } [HttpGet("article/{articleId}")] public ActionResult> GetCommentsForArticle(int articleId) { var comments = _context.Comments.Where(c => c.ArticleId == articleId).ToList(); if (comments.Count == 0) { return NotFound(); } return comments; } } }