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 Status class includes all API's related to the Status. /// [Route("[controller]")] [ApiController] public class StatusController : ControllerBase { private readonly TaskContext _context; /// /// Initializes a new instance of the class. /// /// public StatusController(TaskContext context) { _context = context; } // GET: api/status /// /// Retrieve all Status in DB. /// Result can be filtered using the Params. /// /// string value /// JSON list of all matching status #nullable enable [HttpGet] public async Task>> GetStatus([FromQuery]string? title) { var filtered = _context.Status.AsQueryable(); if (title != null) { filtered = filtered.Where(s => s.Title.Contains(title)); } return await filtered.ToListAsync(); } #nullable enable // GET: api/status/1 /// /// Retrieve the Status by it's ID. /// /// ID of searched Status /// JSON object [HttpGet("{id}")] public async Task> GetStatus(int id) { var userstory = await _context.Status.FindAsync(id); if (userstory == null) { return NotFound(); } return userstory; } // PUT: api/status/1 /// /// Update the Status identified by it's ID. /// /// to edit Status' ID /// modified Userstory /// the proper status code [HttpPut("{id}")] public async Task PutStatus(int id, ScrumStatus userstory) { // The status' ID must not be changed if (id != userstory.Id) { return BadRequest(); } // Save the changed status in the context _context.Entry(userstory).State = EntityState.Modified; try { // Apply the changes to the database await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { // If the status does not exist, return status code 404 if (!StatusExists(id)) { return NotFound(); } else { throw; } } return NoContent(); } // POST: api/status /// /// Create a new Status. /// /// new Userstory /// the proper status code [HttpPost] public async Task> PostTask(ScrumStatus userstory) { _context.Status.Add(userstory); await _context.SaveChangesAsync(); // The new status has been created and can be called up using the GetStatus method return CreatedAtAction("GetStatus", new { id = userstory.Id }, userstory); } // DELETE: api/status/1 /// /// Create a new Status. /// /// to delete Status' ID /// the proper status code [HttpDelete("{id}")] public async Task> DeleteStatus(int id) { var userstory = await _context.Status.FindAsync(id); if (userstory == null) { return NotFound(); } // Remove the status from the context _context.Status.Remove(userstory); // Save the changes in the database await _context.SaveChangesAsync(); return userstory; } /// /// Checks whether a status with the specified ID already exists. /// private bool StatusExists(int id) { return _context.Status.Any(e => e.Id == id); } } }