2022-08-01 00:09:39 +03:00
|
|
|
|
using System;
|
2022-08-04 03:59:04 +03:00
|
|
|
|
using System.Collections.Generic;
|
2022-08-01 00:09:39 +03:00
|
|
|
|
using System.Collections.Immutable;
|
2022-08-04 03:59:04 +03:00
|
|
|
|
using System.Linq;
|
2021-12-26 20:43:27 +03:00
|
|
|
|
using BlueWest.Collections;
|
2021-12-10 03:04:48 +03:00
|
|
|
|
using BlueWest.Data;
|
2022-08-04 03:59:04 +03:00
|
|
|
|
using BlueWest.WebApi.MySQL;
|
2021-12-06 02:49:27 +03:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace BlueWest.WebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("[controller]")]
|
|
|
|
|
public class UserController : ControllerBase
|
|
|
|
|
{
|
2021-12-10 03:04:48 +03:00
|
|
|
|
|
2022-08-04 03:59:04 +03:00
|
|
|
|
private readonly MysqlDbContext _dbContext;
|
|
|
|
|
|
|
|
|
|
public UserController(MysqlDbContext dbContext)
|
|
|
|
|
{
|
|
|
|
|
_dbContext = dbContext;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 02:49:27 +03:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public ActionResult Get()
|
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
var users = _dbContext.Users.ToImmutableArray();
|
|
|
|
|
|
|
|
|
|
return Ok(users);
|
2021-12-06 02:49:27 +03:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
|
[HttpGet("{userId}", Name = nameof(GetUserById))]
|
2022-08-04 03:59:04 +03:00
|
|
|
|
public ActionResult GetUserById(TimeSpan userId)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
var user = _dbContext.Users.FirstOrDefault(x => x.Id == userId);
|
|
|
|
|
|
|
|
|
|
if (user != null)
|
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
|
|
|
|
}
|
2022-08-04 03:59:04 +03:00
|
|
|
|
|
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)
|
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
return Ok();
|
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
|
|
|
|
{
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
|
|
|
[HttpPost]
|
2022-08-04 03:59:04 +03:00
|
|
|
|
public ActionResult AddUser(UserUpdateDto userUpdateDto)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
var user = new User(userUpdateDto, DateTime.Now.TimeOfDay, new List<FinanceTransaction>());
|
|
|
|
|
_dbContext.Users.Add(user);
|
|
|
|
|
_dbContext.SaveChanges();
|
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)
|
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
|
|
|
|
|
return new NotFoundResult();
|
2021-12-06 02:49:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
[HttpDelete("{id}")]
|
2022-08-04 03:59:04 +03:00
|
|
|
|
public ActionResult DeleteUser(TimeSpan id)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
var user = _dbContext.Users.FirstOrDefault(u => u.Id == id);
|
|
|
|
|
if (user == null)
|
2021-12-06 02:49:27 +03:00
|
|
|
|
{
|
2022-08-04 03:59:04 +03:00
|
|
|
|
return new NotFoundResult();
|
2021-12-06 02:49:27 +03:00
|
|
|
|
}
|
2022-08-04 03:59:04 +03:00
|
|
|
|
_dbContext.Users.Remove(user);
|
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
|
return Ok();
|
2021-12-06 02:49:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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
|
|
|
|
{
|
|
|
|
|
return new BadRequestResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|