128 lines
4.1 KiB
C#
128 lines
4.1 KiB
C#
using System.Collections.Immutable;
|
|
using BlueWest.Collections;
|
|
using BlueWest.Data;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using PerformanceSolution.Data;
|
|
|
|
namespace BlueWest.WebApi.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class UserController : ControllerBase
|
|
{
|
|
private UserList _userList => MemoryData.UserList;
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[HttpGet]
|
|
public ActionResult Get()
|
|
{
|
|
if (_userList != null)
|
|
{
|
|
return Ok(_userList.Users.Values.ToImmutableArray());
|
|
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet("{userId}", Name = nameof(GetUserById))]
|
|
public ActionResult GetUserById(int userId)
|
|
{
|
|
DataQueryResult<User> user = MemoryData.GetUserById(userId);
|
|
|
|
if (user.HasValue)
|
|
{
|
|
return Ok(user.Value);
|
|
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[HttpGet("{userId}/transactions")]
|
|
public ActionResult GetTransactions(int userId)
|
|
{
|
|
var transactions = MemoryData.GetUserTransactions(userId);
|
|
return Ok(transactions);
|
|
}
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[HttpGet("{userId}/transactions/{transactionId}")]
|
|
public ActionResult GetTransactionsById(int userId, int transactionId)
|
|
{
|
|
var user = MemoryData.UserList.GetUserById(userId);
|
|
|
|
if(!user.HasValue) return new NotFoundResult();
|
|
|
|
var transactions = user?.Value.FinanceTransactions;
|
|
|
|
FinanceTransaction? transaction = transactions?.Values.ToImmutableArray().FirstOrNull(t => t.Id == transactionId);
|
|
|
|
if (transaction != null)
|
|
{
|
|
return Ok(transaction);
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[HttpPost]
|
|
public ActionResult AddOrModifyUser(UserUpdateDto userUpdateDto)
|
|
{
|
|
var user = MemoryData.AddOrModifyUser(userUpdateDto);
|
|
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.UserList.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.UserList.RemoveUser(id);
|
|
|
|
if (result)
|
|
{
|
|
return NoContent();
|
|
}
|
|
|
|
return new BadRequestResult();
|
|
}
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[HttpPost("{userId}/transactions")]
|
|
public ActionResult PostTransaction(int userId, FinanceTransactionInsertDto financeTransaction)
|
|
{
|
|
DataQueryResult<FinanceTransaction> result = MemoryData.UserList.AddFinanceTransaction(userId, financeTransaction);
|
|
|
|
if(result.HasValue) return Ok(result.Value);
|
|
|
|
return new BadRequestResult();
|
|
}
|
|
|
|
|
|
}
|
|
} |