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-04 03:59:04 +03:00
|
|
|
|
using BlueWest.WebApi.MySQL;
|
2021-12-06 02:49:27 +03:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace BlueWest.WebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("[controller]")]
|
|
|
|
|
public class UserController : ControllerBase
|
|
|
|
|
{
|
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-19 19:47:35 +03:00
|
|
|
|
var user = new User(userCreate, 0, new List<FinanceOp>(), null, -1);
|
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
|
|
|
|
}
|
|
|
|
|
}
|