2022-08-01 00:09:39 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Immutable;
|
2021-12-26 20:43:27 +03:00
|
|
|
|
using BlueWest.Collections;
|
2021-12-10 03:04:48 +03:00
|
|
|
|
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-26 20:43:27 +03:00
|
|
|
|
return Ok(_userList.Users.Values.ToImmutableArray());
|
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)
|
|
|
|
|
{
|
2022-08-04 01:01:10 +03:00
|
|
|
|
var user = MemoryData.GetUserById(userId);
|
2021-12-06 02:49:27 +03:00
|
|
|
|
|
2022-08-04 01:01:10 +03:00
|
|
|
|
if (user.Id != TimeSpan.Zero)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2022-08-04 01:01:10 +03:00
|
|
|
|
return Ok(user);
|
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);
|
2021-12-26 20:43:27 +03:00
|
|
|
|
return Ok(transactions);
|
2021-12-06 02:49:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
[HttpGet("{userId}/transactions/{transactionId}")]
|
2022-08-01 00:09:39 +03:00
|
|
|
|
public ActionResult GetTransactionsById(int userId, TimeSpan transactionId)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2021-12-10 03:04:48 +03:00
|
|
|
|
var user = MemoryData.UserList.GetUserById(userId);
|
2021-12-06 02:49:27 +03:00
|
|
|
|
|
2022-08-04 01:01:10 +03:00
|
|
|
|
if(user.Id == TimeSpan.Zero) return new NotFoundResult();
|
2021-12-26 20:43:27 +03:00
|
|
|
|
|
2022-08-04 01:01:10 +03:00
|
|
|
|
var transactions = user.FinanceTransactions;
|
2021-12-06 02:49:27 +03:00
|
|
|
|
|
2022-08-01 00:09:39 +03:00
|
|
|
|
FinanceTransaction? transaction = transactions?.Values.ToImmutableArray().FirstOrNull(t => t.CreationDate == transactionId);
|
2021-12-06 02:49:27 +03:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2022-08-04 01:01:10 +03:00
|
|
|
|
FinanceTransaction result = MemoryData.UserList.AddFinanceTransaction(userId, financeTransaction);
|
2021-12-06 02:49:27 +03:00
|
|
|
|
|
2022-08-04 01:01:10 +03:00
|
|
|
|
if(result.UserId != TimeSpan.Zero) return Ok(result);
|
2021-12-06 02:49:27 +03:00
|
|
|
|
|
|
|
|
|
return new BadRequestResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|