98 lines
2.5 KiB
C#
98 lines
2.5 KiB
C#
namespace MyOffice.Web.Controllers;
|
|
|
|
using Core;
|
|
using Core.Extensions;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Models.Account;
|
|
using Models.Motion;
|
|
using MyOffice.Data.Models.Accounts;
|
|
using MyOffice.Web.Models.AccountMotion;
|
|
using Services.Account;
|
|
using Services.Account.Domain;
|
|
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class AccountController : BaseApiController
|
|
{
|
|
private readonly ILogger<AccountController> _logger;
|
|
private readonly AccountService _accountService;
|
|
|
|
public AccountController(
|
|
ILogger<AccountController> logger,
|
|
AccountService accountService
|
|
)
|
|
{
|
|
_logger = logger;
|
|
_accountService = accountService;
|
|
}
|
|
|
|
[HttpGet("~/api/accounts")]
|
|
public object AccountsGet(string category)
|
|
{
|
|
var list = _accountService.GetByCategoryDetailed(UserId, category!.AsGuid());
|
|
|
|
return list
|
|
.OrderBy(x => x.Account.Name)
|
|
.Select(x => x.ToModel());
|
|
}
|
|
|
|
[HttpGet("~/api/accounts/{id}")]
|
|
public object AccountGet(string id)
|
|
{
|
|
var exec = _accountService.GetByIdDetailed(UserId, id!.AsGuid());
|
|
|
|
switch (exec.Status)
|
|
{
|
|
case GeneralExecStatus.not_found:
|
|
case GeneralExecStatus.failure:
|
|
return ProblemBadRequest("Account not found.");
|
|
|
|
case GeneralExecStatus.success:
|
|
return exec.Result!.ToModel();
|
|
|
|
default:
|
|
throw new NotSupportedException(exec.Status.ToString());
|
|
}
|
|
}
|
|
|
|
[HttpGet("~/api/accounts/{id}/motions")]
|
|
public object AccountMotionsGet(string id, [FromQuery] AccountMotionsGetModel request)
|
|
{
|
|
var exec = _accountService.GetMotions(UserId, id!.AsGuid(), request.From.StartOfDay(), request.To.EndOfDay());
|
|
|
|
switch (exec.Status)
|
|
{
|
|
case GeneralExecStatus.not_found:
|
|
case GeneralExecStatus.failure:
|
|
return ProblemBadRequest("Account not found.");
|
|
|
|
case GeneralExecStatus.success:
|
|
return exec.Result!.Select(x => x.ToModel());
|
|
|
|
default:
|
|
throw new NotSupportedException(exec.Status.ToString());
|
|
}
|
|
}
|
|
|
|
[HttpPost("~/api/accounts/{id}/motions")]
|
|
public object AccountsMotionsPost(string id, MotionRequest motion)
|
|
{
|
|
var exec = _accountService.MotionAdd(UserId, id.AsGuid(), motion.FromModel());
|
|
|
|
switch (exec.Status)
|
|
{
|
|
case MotionAddResult.account_not_found:
|
|
return ProblemBadRequest("Account not found.");
|
|
case MotionAddResult.failure:
|
|
return ProblemBadRequest("Adding motion failed.");
|
|
case MotionAddResult.success:
|
|
return exec.Result!.ToModel();
|
|
|
|
default:
|
|
throw new NotSupportedException(exec.Status.ToString());
|
|
}
|
|
}
|
|
} |