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

131 lines
3.9 KiB
C#
Raw Normal View History

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