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