srumboard_backend/ScrumTaskboard/Controllers/UsersController.cs

154 lines
4.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 Users class includes all API's related to the Users.
/// </summary>
[Route("[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly TaskContext _context;
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
/// <param name="context"></param>
public UsersController(TaskContext context)
{
_context = context;
}
// GET: api/sprint
/// <summary>
/// Retrieve all Users in DB.
/// Result can be filtered using the Params.
/// </summary>
/// <param name="name">string value</param>
/// <returns>JSON list of all matching Users</returns>
#nullable enable
[HttpGet]
public async Task<ActionResult<IEnumerable<ScrumUser>>> GetUser([FromQuery]string? name)
{
var filtered = _context.Users.AsQueryable();
if (name != null)
{
filtered = filtered.Where<ScrumUser>(u => u.Name.Contains(name));
}
return await filtered.ToListAsync();
}
#nullable disable
// GET: api/sprint/5
/// <summary>
/// Retrieve the User by it's ID.
/// </summary>
/// <param name="id">ID of searched User</param>
/// <returns>JSON object</returns>
[HttpGet("{id}")]
public async Task<ActionResult<ScrumUser>> GetUser(int id)
{
var user = await _context.Users.FindAsync(id);
if (user == null)
{
return NotFound();
}
return user;
}
// PUT: api/sprint/5
/// <summary>
/// Update the User identified by it's ID.
/// </summary>
/// <param name="id">to edit User's ID</param>
/// <param name="sprint">modified Sprint</param>
/// <returns>the proper status code</returns>
[HttpPut("{id}")]
public async Task<IActionResult> PutUser(int id, ScrumUser sprint)
{
// The user's ID must not be changed
if (id != sprint.Id)
{
return BadRequest();
}
// Save the changed user in the context
_context.Entry(sprint).State = EntityState.Modified;
try
{
// Apply the changes to the database
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
// If the user does not exist, return status code 404
if (!UserExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/User
/// <summary>
/// Create a new User.
/// </summary>
/// <param name="sprint">new Sprint</param>
/// <returns>the proper status code</returns>
[HttpPost]
public async Task<ActionResult<ScrumUser>> PostUser(ScrumUser sprint)
{
_context.Users.Add(sprint);
await _context.SaveChangesAsync();
// The new user has been created and can be called up using the GetUser method
return CreatedAtAction("GetUser", new { id = sprint.Id }, sprint);
}
// DELETE: api/User/5
/// <summary>
/// Delete a User identified by it's ID.
/// </summary>
/// <param name="id">to delete User's ID</param>
/// <returns>the proper status code</returns>
[HttpDelete("{id}")]
public async Task<ActionResult<ScrumUser>> DeleteUser(int id)
{
var scrumUser = await _context.Users.FindAsync(id);
if (scrumUser == null)
{
return NotFound();
}
// Remove the user from the context
_context.Users.Remove(scrumUser);
// Save the changes in the database
await _context.SaveChangesAsync();
return scrumUser;
}
/// <summary>
/// Checks whether a user with the specified ID already exists.
/// </summary>
private bool UserExists(int id)
{
return _context.Users.Any(e => e.Id == id);
}
}
}