2022-08-01 00:09:39 +03:00
|
|
|
|
using System;
|
2022-08-04 03:59:04 +03:00
|
|
|
|
using System.Collections.Generic;
|
2022-08-01 00:09:39 +03:00
|
|
|
|
using System.Collections.Immutable;
|
2022-08-04 03:59:04 +03:00
|
|
|
|
using System.Linq;
|
2021-12-10 03:04:48 +03:00
|
|
|
|
using BlueWest.Data;
|
2022-08-22 00:14:50 +03:00
|
|
|
|
using BlueWest.WebApi.EF;
|
2022-09-10 07:12:03 +03:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2022-09-17 22:13:35 +03:00
|
|
|
|
using Microsoft.AspNetCore.Cors;
|
2021-12-06 02:49:27 +03:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace BlueWest.WebApi.Controllers
|
|
|
|
|
{
|
2022-08-22 02:51:45 +03:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Api Controller for handling users data
|
|
|
|
|
/// </summary>
|
2021-12-06 02:49:27 +03:00
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("[controller]")]
|
2022-09-10 07:12:03 +03:00
|
|
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
2022-09-11 01:22:04 +03:00
|
|
|
|
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
|
2022-09-17 22:13:35 +03:00
|
|
|
|
//[Authorize(Roles = "Administrator")]
|
|
|
|
|
[EnableCors(Constants.CorsPolicyName)]
|
|
|
|
|
|
2022-08-22 02:51:45 +03:00
|
|
|
|
public class UserController : ControllerBase
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2021-12-10 03:04:48 +03:00
|
|
|
|
|
2022-08-13 06:35:36 +03:00
|
|
|
|
private readonly UserDbContext _dbContext;
|
2022-08-04 03:59:04 +03:00
|
|
|
|
|
2022-08-19 06:18:50 +03:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Controller responsible to handle user data
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dbContext"></param>
|
2022-08-13 06:35:36 +03:00
|
|
|
|
public UserController(UserDbContext dbContext)
|
2022-08-04 03:59:04 +03:00
|
|
|
|
{
|
|
|
|
|
_dbContext = dbContext;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 06:35:36 +03:00
|
|
|
|
|
2022-08-19 06:18:50 +03:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all the users in the user table12312
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2021-12-06 02:49:27 +03:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public ActionResult Get()
|
|
|
|
|
{
|
2022-08-19 06:18:50 +03:00
|
|
|
|
var users = _dbContext.Users.ToArray();
|
2022-08-04 03:59:04 +03:00
|
|
|
|
return Ok(users);
|
2021-12-06 02:49:27 +03:00
|
|
|
|
|
|
|
|
|
}
|
2022-08-13 06:35:36 +03:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get User by Id
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userId"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-12-06 02:49:27 +03:00
|
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
|
[HttpGet("{userId}", Name = nameof(GetUserById))]
|
2022-08-19 06:18:50 +03:00
|
|
|
|
public ActionResult GetUserById(int userId)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
var user = _dbContext.Users.FirstOrDefault(x => x.Id == userId);
|
|
|
|
|
|
|
|
|
|
if (user != null)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2022-08-04 01:01:10 +03:00
|
|
|
|
return Ok(user);
|
2021-12-06 02:49:27 +03:00
|
|
|
|
}
|
2022-08-04 03:59:04 +03:00
|
|
|
|
|
2021-12-06 02:49:27 +03:00
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 06:35:36 +03:00
|
|
|
|
|
2022-08-19 06:18:50 +03:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a user to the database
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userCreate">User to add </param>
|
|
|
|
|
/// <returns></returns>
|
2021-12-06 02:49:27 +03:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
|
|
|
[HttpPost]
|
2022-08-19 06:18:50 +03:00
|
|
|
|
public ActionResult AddUser(UserCreate userCreate)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2022-08-22 00:14:50 +03:00
|
|
|
|
var user = new User(userCreate);
|
2022-08-04 03:59:04 +03:00
|
|
|
|
_dbContext.Users.Add(user);
|
|
|
|
|
_dbContext.SaveChanges();
|
2021-12-06 02:49:27 +03:00
|
|
|
|
return CreatedAtRoute(nameof(GetUserById), new {userId = user.Id}, user);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 06:18:50 +03:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Updates user data
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userId">User id</param>
|
|
|
|
|
/// <param name="userCreate"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-12-06 02:49:27 +03:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2022-08-19 06:18:50 +03:00
|
|
|
|
[HttpPut($"{{userId:int}}")]
|
|
|
|
|
public ActionResult UpdateUser(int userId, UserCreate userCreate)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
|
|
|
|
|
return new NotFoundResult();
|
2021-12-06 02:49:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 06:18:50 +03:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes a user from the database
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-12-06 02:49:27 +03:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2022-08-19 06:18:50 +03:00
|
|
|
|
[HttpDelete("{id:int}")]
|
|
|
|
|
public ActionResult DeleteUser(int id)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
var user = _dbContext.Users.FirstOrDefault(u => u.Id == id);
|
|
|
|
|
if (user == null)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
return new NotFoundResult();
|
2021-12-06 02:49:27 +03:00
|
|
|
|
}
|
2022-08-04 03:59:04 +03:00
|
|
|
|
_dbContext.Users.Remove(user);
|
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
|
return Ok();
|
2021-12-06 02:49:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 06:35:36 +03:00
|
|
|
|
|
2021-12-06 02:49:27 +03:00
|
|
|
|
}
|
|
|
|
|
}
|