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