128 lines
3.7 KiB
C#
128 lines
3.7 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
|
|
#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
|
|
[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
|
|
[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
|
|
[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
|
|
[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);
|
|
}
|
|
}
|
|
} |