srumboard_backend/ScrumTaskboard/Controllers/StatusController.cs

155 lines
4.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace ScrumTaskboard.Controllers
{
/// <summary>
/// This is a controller of Status class includes all API's related to the Status.
/// </summary>
[Route("[controller]")]
[ApiController]
public class StatusController : ControllerBase
{
private readonly TaskContext _context;
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
/// <param name="context"></param>
public StatusController(TaskContext context)
{
_context = context;
}
// GET: api/status
/// <summary>
/// Retrieve all Status in DB.
/// Result can be filtered using the Params.
/// </summary>
/// <param name="title">string value</param>
/// <returns>JSON list of all matching status</returns>
#nullable enable
[HttpGet]
public async Task<ActionResult<IEnumerable<ScrumStatus>>> GetStatus([FromQuery]string? title)
{
var filtered = _context.Status.AsQueryable();
if (title != null)
{
filtered = filtered.Where<ScrumStatus>(s => s.Title.Contains(title));
}
return await filtered.ToListAsync();
}
#nullable enable
// GET: api/status/1
/// <summary>
/// Retrieve the Status by it's ID.
/// </summary>
/// <param name="id">ID of searched Status</param>
/// <returns>JSON object</returns>
[HttpGet("{id}")]
public async Task<ActionResult<ScrumStatus>> GetStatus(int id)
{
var userstory = await _context.Status.FindAsync(id);
if (userstory == null)
{
return NotFound();
}
return userstory;
}
// PUT: api/status/1
/// <summary>
/// Update the Status identified by it's ID.
/// </summary>
/// <param name="id">to edit Status' ID</param>
/// <param name="userstory">modified Userstory</param>
/// <returns>the proper status code</returns>
[HttpPut("{id}")]
public async Task<IActionResult> 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
/// <summary>
/// Create a new Status.
/// </summary>
/// <param name="userstory">new Userstory</param>
/// <returns>the proper status code</returns>
[HttpPost]
public async Task<ActionResult<ScrumStatus>> 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
/// <summary>
/// Create a new Status.
/// </summary>
/// <param name="id">to delete Status' ID</param>
/// <returns>the proper status code</returns>
[HttpDelete("{id}")]
public async Task<ActionResult<ScrumStatus>> 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;
}
/// <summary>
/// Checks whether a status with the specified ID already exists.
/// </summary>
private bool StatusExists(int id)
{
return _context.Status.Any(e => e.Id == id);
}
}
}