srumboard_backend/ScrumTaskboard/Controllers/StatusController.cs

155 lines
4.8 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2020-06-04 15:32:56 +00:00
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace ScrumTaskboard.Controllers
{
2020-07-08 11:42:42 +00:00
/// <summary>
/// This is a controller of Status class includes all API's related to the Status.
/// </summary>
2020-06-04 15:32:56 +00:00
[Route("[controller]")]
[ApiController]
public class StatusController : ControllerBase
{
private readonly TaskContext _context;
2020-07-08 11:42:42 +00:00
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
/// <param name="context"></param>
2020-06-04 15:32:56 +00:00
public StatusController(TaskContext context)
{
_context = context;
}
// GET: api/status
2020-07-15 14:21:43 +00:00
/// <summary>
/// Retrieve all Status in DB.
2020-07-19 11:09:48 +00:00
/// Result can be filtered using the Params.
2020-07-15 14:21:43 +00:00
/// </summary>
2020-07-19 11:09:48 +00:00
/// <param name="title">string value</param>
/// <returns>JSON list of all matching status</returns>
2020-07-15 14:21:43 +00:00
#nullable enable
2020-06-04 15:32:56 +00:00
[HttpGet]
public async Task<ActionResult<IEnumerable<ScrumStatus>>> GetStatus([FromQuery]string? title)
2020-06-04 15:32:56 +00:00
{
var filtered = _context.Status.AsQueryable();
if (title != null)
{
2020-07-08 16:10:58 +00:00
filtered = filtered.Where<ScrumStatus>(s => s.Title.Contains(title));
}
return await filtered.ToListAsync();
2020-06-04 15:32:56 +00:00
}
2020-07-15 14:21:43 +00:00
#nullable enable
2020-06-04 15:32:56 +00:00
// GET: api/status/1
2020-07-15 14:21:43 +00:00
/// <summary>
/// Retrieve the Status by it's ID.
/// </summary>
2020-07-19 11:09:48 +00:00
/// <param name="id">ID of searched Status</param>
/// <returns>JSON object</returns>
2020-06-04 15:32:56 +00:00
[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
2020-07-15 14:21:43 +00:00
/// <summary>
/// Update the Status identified by it's ID.
/// </summary>
2020-07-19 15:46:41 +00:00
/// <param name="id">to edit Status' ID</param>
2020-07-19 11:09:48 +00:00
/// <param name="userstory">modified Userstory</param>
2020-07-19 15:46:41 +00:00
/// <returns>the proper status code</returns>
2020-06-04 15:32:56 +00:00
[HttpPut("{id}")]
public async Task<IActionResult> PutStatus(int id, ScrumStatus userstory)
{
2020-07-08 11:42:42 +00:00
// The status' ID must not be changed
if (id != userstory.Id)
2020-06-04 15:32:56 +00:00
{
return BadRequest();
}
2020-07-08 11:42:42 +00:00
// Save the changed status in the context
2020-06-04 15:32:56 +00:00
_context.Entry(userstory).State = EntityState.Modified;
try
{
2020-07-08 11:42:42 +00:00
// Apply the changes to the database
2020-06-04 15:32:56 +00:00
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
2020-07-08 11:42:42 +00:00
// If the status does not exist, return status code 404
2020-06-04 15:32:56 +00:00
if (!StatusExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/status
2020-07-15 14:21:43 +00:00
/// <summary>
/// Create a new Status.
/// </summary>
2020-07-19 15:46:41 +00:00
/// <param name="userstory">new Userstory</param>
/// <returns>the proper status code</returns>
2020-06-04 15:32:56 +00:00
[HttpPost]
public async Task<ActionResult<ScrumStatus>> PostTask(ScrumStatus userstory)
{
_context.Status.Add(userstory);
await _context.SaveChangesAsync();
2020-07-08 11:42:42 +00:00
// The new status has been created and can be called up using the GetStatus method
return CreatedAtAction("GetStatus", new { id = userstory.Id }, userstory);
2020-06-04 15:32:56 +00:00
}
// DELETE: api/status/1
2020-07-15 14:21:43 +00:00
/// <summary>
/// Create a new Status.
/// </summary>
2020-07-19 15:46:41 +00:00
/// <param name="id">to delete Status' ID</param>
/// <returns>the proper status code</returns>
2020-06-04 15:32:56 +00:00
[HttpDelete("{id}")]
public async Task<ActionResult<ScrumStatus>> DeleteStatus(int id)
{
var userstory = await _context.Status.FindAsync(id);
if (userstory == null)
{
return NotFound();
}
2020-07-08 11:42:42 +00:00
// Remove the status from the context
2020-06-04 15:32:56 +00:00
_context.Status.Remove(userstory);
2020-07-08 11:42:42 +00:00
// Save the changes in the database
2020-06-04 15:32:56 +00:00
await _context.SaveChangesAsync();
return userstory;
}
/// <summary>
2020-07-08 11:42:42 +00:00
/// Checks whether a status with the specified ID already exists.
2020-06-04 15:32:56 +00:00
/// </summary>
private bool StatusExists(int id)
{
2020-07-08 16:10:58 +00:00
return _context.Status.Any(e => e.Id == id);
2020-06-04 15:32:56 +00:00
}
}
}