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-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
|
|
|
|
|
2021-12-06 02:49:27 +03:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public ActionResult Get()
|
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
var users = _dbContext.Users.ToImmutableArray();
|
|
|
|
|
|
|
|
|
|
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-04 03:59:04 +03:00
|
|
|
|
public ActionResult GetUserById(TimeSpan 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
|
|
|
|
|
2021-12-06 02:49:27 +03:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
|
|
|
[HttpPost]
|
2022-08-04 03:59:04 +03:00
|
|
|
|
public ActionResult AddUser(UserUpdateDto userUpdateDto)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2022-08-18 22:59:13 +03:00
|
|
|
|
var user = new User(userUpdateDto, DateTime.Now.TimeOfDay, new List<FinanceOp>());
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
[HttpPut("{userId}")]
|
|
|
|
|
public ActionResult UpdateUser(int userId, UserUpdateDto userUpdate)
|
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
|
|
|
|
|
return new NotFoundResult();
|
2021-12-06 02:49:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
[HttpDelete("{id}")]
|
2022-08-04 03:59:04 +03:00
|
|
|
|
public ActionResult DeleteUser(TimeSpan 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
|
|
|
|
}
|
|
|
|
|
}
|