2020-06-05 13:59:22 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-06-04 15:54:26 +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 Users class includes all API's related to the Users.
|
|
|
|
|
/// </summary>
|
2020-06-04 15:54:26 +00:00
|
|
|
|
[Route("[controller]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class UsersController : 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:54:26 +00:00
|
|
|
|
public UsersController(TaskContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: api/sprint
|
2020-07-15 14:21:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieve all Users 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="name">string value</param>
|
|
|
|
|
/// <returns>JSON list of all matching Users</returns>
|
2020-07-15 14:21:43 +00:00
|
|
|
|
#nullable enable
|
2020-06-04 15:54:26 +00:00
|
|
|
|
[HttpGet]
|
2020-06-18 14:38:14 +00:00
|
|
|
|
public async Task<ActionResult<IEnumerable<ScrumUser>>> GetUser([FromQuery]string? name)
|
2020-06-04 15:54:26 +00:00
|
|
|
|
{
|
2020-06-18 13:27:40 +00:00
|
|
|
|
var filtered = _context.Users.AsQueryable();
|
2020-06-18 14:38:14 +00:00
|
|
|
|
if (name != null)
|
2020-06-18 13:10:42 +00:00
|
|
|
|
{
|
2020-07-08 16:10:58 +00:00
|
|
|
|
filtered = filtered.Where<ScrumUser>(u => u.Name.Contains(name));
|
2020-06-18 13:10:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 13:27:40 +00:00
|
|
|
|
return await filtered.ToListAsync();
|
2020-06-04 15:54:26 +00:00
|
|
|
|
}
|
2020-07-15 14:21:43 +00:00
|
|
|
|
#nullable disable
|
2020-06-04 15:54:26 +00:00
|
|
|
|
|
|
|
|
|
// GET: api/sprint/5
|
2020-07-15 14:21:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieve the User by it's ID.
|
|
|
|
|
/// </summary>
|
2020-07-19 11:09:48 +00:00
|
|
|
|
/// <param name="id">ID of searched User</param>
|
|
|
|
|
/// <returns>JSON object</returns>
|
2020-06-04 15:54:26 +00:00
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
public async Task<ActionResult<ScrumUser>> GetUser(int id)
|
|
|
|
|
{
|
2020-07-08 16:10:58 +00:00
|
|
|
|
var user = await _context.Users.FindAsync(id);
|
2020-06-04 15:54:26 +00:00
|
|
|
|
|
2020-07-08 16:10:58 +00:00
|
|
|
|
if (user == null)
|
2020-06-04 15:54:26 +00:00
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 16:10:58 +00:00
|
|
|
|
return user;
|
2020-06-04 15:54:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT: api/sprint/5
|
2020-07-15 14:21:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update the User identified by it's ID.
|
|
|
|
|
/// </summary>
|
2020-07-19 15:46:41 +00:00
|
|
|
|
/// <param name="id">to edit User's ID</param>
|
2020-07-19 11:09:48 +00:00
|
|
|
|
/// <param name="sprint">modified Sprint</param>
|
2020-07-19 15:46:41 +00:00
|
|
|
|
/// <returns>the proper status code</returns>
|
2020-06-04 15:54:26 +00:00
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
public async Task<IActionResult> PutUser(int id, ScrumUser sprint)
|
|
|
|
|
{
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// The user's ID must not be changed
|
2020-07-14 16:10:57 +00:00
|
|
|
|
if (id != sprint.Id)
|
2020-06-04 15:54:26 +00:00
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// Save the changed user in the context
|
2020-06-04 15:54:26 +00:00
|
|
|
|
_context.Entry(sprint).State = EntityState.Modified;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// Apply the changes to the database
|
2020-06-04 15:54:26 +00:00
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (DbUpdateConcurrencyException)
|
|
|
|
|
{
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// If the user does not exist, return status code 404
|
2020-06-04 15:54:26 +00:00
|
|
|
|
if (!UserExists(id))
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: api/User
|
2020-07-15 14:21:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a new User.
|
|
|
|
|
/// </summary>
|
2020-07-19 15:46:41 +00:00
|
|
|
|
/// <param name="sprint">new Sprint</param>
|
|
|
|
|
/// <returns>the proper status code</returns>
|
2020-06-04 15:54:26 +00:00
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<ActionResult<ScrumUser>> PostUser(ScrumUser sprint)
|
|
|
|
|
{
|
|
|
|
|
_context.Users.Add(sprint);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// The new user has been created and can be called up using the GetUser method
|
2020-07-14 16:10:57 +00:00
|
|
|
|
return CreatedAtAction("GetUser", new { id = sprint.Id }, sprint);
|
2020-06-04 15:54:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DELETE: api/User/5
|
2020-07-15 14:21:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Delete a User identified by it's ID.
|
|
|
|
|
/// </summary>
|
2020-07-19 15:46:41 +00:00
|
|
|
|
/// <param name="id">to delete User's ID</param>
|
|
|
|
|
/// <returns>the proper status code</returns>
|
2020-06-04 15:54:26 +00:00
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
public async Task<ActionResult<ScrumUser>> DeleteUser(int id)
|
|
|
|
|
{
|
|
|
|
|
var scrumUser = await _context.Users.FindAsync(id);
|
|
|
|
|
if (scrumUser == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// Remove the user from the context
|
2020-06-04 15:54:26 +00:00
|
|
|
|
_context.Users.Remove(scrumUser);
|
2020-07-08 11:42:42 +00:00
|
|
|
|
// Save the changes in the database
|
2020-06-04 15:54:26 +00:00
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return scrumUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-07-08 11:42:42 +00:00
|
|
|
|
/// Checks whether a user with the specified ID already exists.
|
2020-06-04 15:54:26 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
private bool UserExists(int id)
|
|
|
|
|
{
|
2020-07-08 16:10:58 +00:00
|
|
|
|
return _context.Users.Any(e => e.Id == id);
|
2020-06-04 15:54:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|