using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using BlueWest.Data;
using BlueWest.WebApi.MySQL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace BlueWest.WebApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
private readonly UserDbContext _dbContext;
public UserController(UserDbContext dbContext)
{
_dbContext = dbContext;
}
#region Users
[ProducesResponseType(StatusCodes.Status200OK)]
[HttpGet]
public ActionResult Get()
{
var users = _dbContext.Users.ToImmutableArray();
return Ok(users);
}
///
/// Get User by Id
///
///
///
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{userId}", Name = nameof(GetUserById))]
public ActionResult GetUserById(TimeSpan userId)
{
var user = _dbContext.Users.FirstOrDefault(x => x.Id == userId);
if (user != null)
{
return Ok(user);
}
return new NotFoundResult();
}
[ProducesResponseType(StatusCodes.Status201Created)]
[HttpPost]
public ActionResult AddUser(UserUpdateDto userUpdateDto)
{
var user = new User(userUpdateDto, DateTime.Now.TimeOfDay, new List());
_dbContext.Users.Add(user);
_dbContext.SaveChanges();
return CreatedAtRoute(nameof(GetUserById), new {userId = user.Id}, user);
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPut("{userId}")]
public ActionResult UpdateUser(int userId, UserUpdateDto userUpdate)
{
return new NotFoundResult();
}
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpDelete("{id}")]
public ActionResult DeleteUser(TimeSpan id)
{
var user = _dbContext.Users.FirstOrDefault(u => u.Id == id);
if (user == null)
{
return new NotFoundResult();
}
_dbContext.Users.Remove(user);
_dbContext.SaveChanges();
return Ok();
}
#endregion
#region Transactions
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet("{userId}/transactions/{transactionId}")]
public ActionResult GetTransactionsById(int userId, TimeSpan transactionId)
{
return new NotFoundResult();
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPost("{userId}/transactions")]
public ActionResult PostTransaction(int userId, FinanceTransactionInsertDto financeTransaction)
{
return new BadRequestResult();
}
///
/// Get Transactions
///
///
///
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet("{userId}/transactions")]
public ActionResult GetTransactions(int userId)
{
return Ok();
}
#endregion
}
}