CodeLiturgy.Dashboard/BlueWest.WebApi/Controllers/UserController.cs

125 lines
3.9 KiB
C#
Raw Normal View History

2021-12-10 03:04:48 +03:00
using BlueWest.Core.Tests;
using BlueWest.Data;
2021-12-06 02:49:27 +03:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PerformanceSolution.Data;
namespace BlueWest.WebApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
2021-12-10 03:04:48 +03:00
private UserList _userList => MemoryData.UserList;
2021-12-06 02:49:27 +03:00
[ProducesResponseType(StatusCodes.Status200OK)]
[HttpGet]
public ActionResult Get()
{
2021-12-10 03:04:48 +03:00
if (_userList != null)
2021-12-06 02:49:27 +03:00
{
2021-12-10 03:04:48 +03:00
return Ok(_userList);
2021-12-06 02:49:27 +03:00
}
return new NotFoundResult();
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{userId}", Name = nameof(GetUserById))]
public ActionResult GetUserById(int userId)
{
2021-12-10 03:04:48 +03:00
User? user = MemoryData.GetUserById(userId);
2021-12-06 02:49:27 +03:00
if (user != null)
{
2021-12-10 03:04:48 +03:00
return Ok(user.Value);
2021-12-06 02:49:27 +03:00
}
return new NotFoundResult();
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet("{userId}/transactions")]
public ActionResult GetTransactions(int userId)
{
2021-12-10 03:04:48 +03:00
var transactions = MemoryData.GetUserTransactions(userId);
return OK(transactions.ToDictionary());
2021-12-06 02:49:27 +03:00
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet("{userId}/transactions/{transactionId}")]
public ActionResult GetTransactionsById(int userId, int transactionId)
{
2021-12-10 03:04:48 +03:00
var user = MemoryData.UserList.GetUserById(userId);
2021-12-06 02:49:27 +03:00
var transactions = user?.FinanceTransactions;
FinanceTransaction? transaction = transactions?.FirstOrNull(t => t.Id == transactionId);
if (transaction != null)
{
return Ok(transaction);
}
return new NotFoundResult();
}
[ProducesResponseType(StatusCodes.Status201Created)]
[HttpPost]
public ActionResult AddOrModifyUser(UserUpdateDto userUpdateDto)
{
2021-12-08 02:57:27 +03:00
var user = MemoryData.AddOrModifyUser(userUpdateDto);
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)
{
2021-12-10 03:04:48 +03:00
var result = MemoryData.UserList.UpdateUser(userId, userUpdate);
2021-12-06 02:49:27 +03:00
if (result != null) return Ok(result);
return BadRequest();
}
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpDelete("{id}")]
public ActionResult DeleteUser(int id)
{
2021-12-10 03:04:48 +03:00
bool result = MemoryData.UserList.RemoveUser(id);
2021-12-06 02:49:27 +03:00
if (result)
{
return NoContent();
}
return new BadRequestResult();
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPost("{userId}/transactions")]
2021-12-10 03:04:48 +03:00
public ActionResult PostTransaction(int userId, FinanceTransactionInsertDto financeTransaction)
2021-12-06 02:49:27 +03:00
{
2021-12-10 03:04:48 +03:00
FinanceTransaction? result = MemoryData.UserList.AddFinanceTransaction(userId, financeTransaction);
2021-12-06 02:49:27 +03:00
if(result != null) return Ok(result);
return new BadRequestResult();
}
}
}