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 Sprints class includes all API's related to the Sprints. /// [Route("[controller]")] [ApiController] public class SprintsController : ControllerBase { private readonly TaskContext _context; /// /// Initializes a new instance of the class. /// /// public SprintsController(TaskContext context) { _context = context; } // GET: api/sprint #nullable enable [HttpGet] public async Task>> GetSprint([FromQuery]string? title, [FromQuery]int? projectid, [FromQuery]DateTime? startDate, [FromQuery]DateTime? endDate) { var filtered = _context.Sprints.AsQueryable(); if (title != null) { filtered = filtered.Where(s => s.Title.Contains(title)); } if (projectid != null) { filtered = filtered.Where(s => s.ProjectId == projectid); } if (startDate != null) { filtered = filtered.Where(s => s.StartDate == startDate); } if (endDate != null) { filtered = filtered.Where(s => s.EndDate == endDate); } return await filtered.ToListAsync(); } #nullable disable // GET: api/sprint/5 [HttpGet("{id}")] public async Task> GetSprint(int id) { var sprint = await _context.Sprints.FindAsync(id); if (sprint == null) { return NotFound(); } return sprint; } // PUT: api/sprint/5 [HttpPut("{id}")] public async Task PutSprint(int id, ScrumSprint sprint) { // The sprint's ID must not be changed if (id != sprint.Id) { return BadRequest(); } // Save the changed sprint in the context _context.Entry(sprint).State = EntityState.Modified; try { // Apply the changes to the database await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { // If the sprint does not exist, return status code 404 if (!SprintExists(id)) { return NotFound(); } else { throw; } } return NoContent(); } // POST: api/Sprint [HttpPost] public async Task> PostSprint(ScrumSprint sprint) { _context.Sprints.Add(sprint); await _context.SaveChangesAsync(); // The new sprint has been created and can be called up using the GetSprint method return CreatedAtAction("GetSprint", new { id = sprint.Id }, sprint); } // DELETE: api/Sprint/5 [HttpDelete("{id}")] public async Task> DeleteSprint(int id) { var scrumSprint = await _context.Sprints.FindAsync(id); if (scrumSprint == null) { return NotFound(); } // Remove the sprint from the context _context.Sprints.Remove(scrumSprint); // Save the changes in the database await _context.SaveChangesAsync(); return scrumSprint; } /// /// Checks whether a sprint with the specified ID already exists. /// private bool SprintExists(int id) { return _context.Sprints.Any(e => e.Id == id); } } }