using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace ScrumTaskboard.Controllers { /// /// This is a controller of Userstories class includes all API's related to the Userstories. /// [Route("[controller]")] [ApiController] public class UserstoriesController : ControllerBase { private readonly TaskContext _context; /// /// Initializes a new instance of the class. /// /// public UserstoriesController(TaskContext context) { _context = context; } // GET: api/userstories #nullable enable [HttpGet] public async Task>> GetUserstory([FromQuery]string? title, [FromQuery]int? statusid, [FromQuery]int? categoryid, [FromQuery]int? createdbyid, [FromQuery]int? projectid, [FromQuery]int? sprintid, [FromQuery]ScrumPrio? priority) { var filtered = _context.Userstories.AsQueryable(); if (title != null) { filtered = filtered.Where(t => t.Title.Contains(title)); } if (statusid != null) { filtered = filtered.Where(t => t.StatusId == statusid); } if (categoryid != null) { filtered = filtered.Where(t => t.CategoryId == categoryid); } if (createdbyid != null) { filtered = filtered.Where(t => t.CreatedById == createdbyid); } if (projectid != null) { filtered = filtered.Where(t => t.ProjectId == projectid); } if (sprintid != null) { filtered = filtered.Where(t => t.SprintId == sprintid); } if (priority != null) { filtered = filtered.Where(t => t.Priority == priority); } return await filtered.ToListAsync(); } #nullable disable // GET: api/userstories/1 [HttpGet("{id}")] public async Task> GetUserstory(int id) { var userstory = await _context.Userstories.FindAsync(id); if (userstory == null) { return NotFound(); } return userstory; } // PUT: api/userstories/1 [HttpPut("{id}")] public async Task PutUserstory(int id, ScrumUserstory userstory) { // The userstory's ID must not be changed if (id != userstory.Id) { return BadRequest(); } // Save the changed userstory in the context _context.Entry(userstory).State = EntityState.Modified; try { // Apply the changes to the database await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { // If the userstory does not exist, return status code 404 if (!UserstoryExists(id)) { return NotFound(); } else { throw; } } return NoContent(); } // POST: api/userstories [HttpPost] public async Task> PostTask(ScrumUserstory userstory) { _context.Userstories.Add(userstory); await _context.SaveChangesAsync(); // The new userstory has been created and can be called up using the GetUserstory method return CreatedAtAction("GetUserstory", new { id = userstory.Id }, userstory); } // DELETE: api/userstories/1 [HttpDelete("{id}")] public async Task> DeleteUserstory(int id) { var userstory = await _context.Userstories.FindAsync(id); if (userstory == null) { return NotFound(); } // Remove the userstory from the context _context.Userstories.Remove(userstory); // Save the changes in the database await _context.SaveChangesAsync(); return userstory; } /// /// Checks whether a userstory with the specified ID already exists. /// private bool UserstoryExists(int id) { return _context.Userstories.Any(e => e.Id == id); } } }