175 lines
4.4 KiB
C#
175 lines
4.4 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.Item;
|
|
using Models.Motion;
|
|
using Services.Account;
|
|
using Services.Account.Domain;
|
|
using Services.Item;
|
|
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class AccountController : BaseApiController
|
|
{
|
|
private readonly ILogger<AccountController> _logger;
|
|
private readonly AccountService _accountService;
|
|
private readonly ItemService _itemService;
|
|
|
|
public AccountController(
|
|
ILogger<AccountController> logger,
|
|
AccountService accountService,
|
|
ItemService itemService
|
|
)
|
|
{
|
|
_logger = logger;
|
|
_accountService = accountService;
|
|
_itemService = itemService;
|
|
}
|
|
|
|
[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 MotionsGet(string id, [FromQuery] MotionsGetModel 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 MotionsPost(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());
|
|
}
|
|
}
|
|
|
|
[HttpPut("~/api/accounts/{id}/motions/{motionId}")]
|
|
public object MotionsPut(string id, string motionId, MotionRequest motion)
|
|
{
|
|
var exec = _accountService.MotionUpdate(UserId, id.AsGuid(), motionId.AsGuid(), motion.FromModel());
|
|
|
|
switch (exec.Status)
|
|
{
|
|
case MotionUpdateResult.not_found:
|
|
return ProblemBadRequest("Motion not found.");
|
|
case MotionUpdateResult.failure:
|
|
return ProblemBadRequest("Updating motion failed.");
|
|
case MotionUpdateResult.success:
|
|
return exec.Result!.ToModel();
|
|
|
|
default:
|
|
throw new NotSupportedException(exec.Status.ToString());
|
|
}
|
|
}
|
|
|
|
[HttpDelete("~/api/accounts/{id}/motions/{motionId}")]
|
|
public object MotionsDelete(string id, string motionId)
|
|
{
|
|
var exec = _accountService.MotionRemove(UserId, id.AsGuid(), motionId.AsGuid());
|
|
|
|
switch (exec.Status)
|
|
{
|
|
case MotionDeleteResult.not_found:
|
|
return ProblemBadRequest("Motion not found.");
|
|
case MotionDeleteResult.failure:
|
|
return ProblemBadRequest("Deliting motion failed.");
|
|
case MotionDeleteResult.success:
|
|
return exec.Result!.ToModel();
|
|
|
|
default:
|
|
throw new NotSupportedException(exec.Status.ToString());
|
|
}
|
|
}
|
|
|
|
[HttpGet("~/api/items")]
|
|
public object FindItems(string term)
|
|
{
|
|
var items = _itemService
|
|
.FindItems(UserId, term)
|
|
.Select(x => x.ToModel())
|
|
.ToList();
|
|
|
|
if (term.StartsWith("+") && term.Length > 1)
|
|
{
|
|
var accounts = _accountService.FindAccounts(UserId, term.Substring(1));
|
|
foreach (var account in accounts)
|
|
{
|
|
var accountName = $"+{account.Name}";
|
|
var item = items.FirstOrDefault(x => x.Name == accountName);
|
|
|
|
if (item == null)
|
|
{
|
|
items.Add(new ItemViewModel
|
|
{
|
|
Name = accountName,
|
|
AccountId = account.Id.ToShort(),
|
|
});
|
|
}
|
|
else
|
|
{
|
|
item.AccountId = account.Id.ToShort();
|
|
}
|
|
}
|
|
}
|
|
|
|
return items
|
|
.OrderBy(x => x.AccountId)
|
|
.ThenBy(x => x.Name);
|
|
}
|
|
} |