using System; 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) { var user = MemoryData.GetUserById(userId); if (user.Id != TimeSpan.Zero) { return Ok(user); } 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, TimeSpan transactionId) { var user = MemoryData.UserList.GetUserById(userId); if(user.Id == TimeSpan.Zero) return new NotFoundResult(); var transactions = user.FinanceTransactions; FinanceTransaction? transaction = transactions?.Values.ToImmutableArray().FirstOrNull(t => t.CreationDate == 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.UserId != TimeSpan.Zero) return Ok(result); return new BadRequestResult(); } } }