using BlueWest.Core.Tests; 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); } 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.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.ToDictionary()); } [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [HttpGet("{userId}/transactions/{transactionId}")] public ActionResult GetTransactionsById(int userId, int transactionId) { var user = MemoryData.UserList.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) { 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) { FinanceTransaction? result = MemoryData.UserList.AddFinanceTransaction(userId, financeTransaction); if(result != null) return Ok(result); return new BadRequestResult(); } } }