namespace MyOffice.Web.Controllers; using AutoMapper; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using Core; using Core.Extensions; using Models.Account; using Services.Account; using Services.Account.Domain; using Infrastructure.Attributes; using Services.Identity; [Authorize] [ApiController] [Route("api/[controller]")] public class SettingsAccountController : BaseApiController { private readonly ILogger _logger; private readonly IMapper _mapper; private readonly AccountService _accountService; private readonly IContextProvider _contextProvider; public SettingsAccountController( ILogger logger, IMapper mapper, AccountService accountService, IContextProvider contextProvider ) { _logger = logger; _mapper = mapper; _accountService = accountService; _contextProvider = contextProvider; } [HttpGet("~/api/settings/account-categories")] public object AccountCategories() { var list = _accountService.GetAllCategories(UserId); return _mapper.Map(list).OrderBy(x => x.Name); } [HttpGet("~/api/settings/account-categories/{id}")] public object AccountCategory([AsGuid] string id) { var exec = _accountService.GetCategory(UserId, id.AsGuid()); switch (exec.Status) { case GeneralExecStatus.not_found: case GeneralExecStatus.failure: return ProblemBadResponse("Account category not found."); case GeneralExecStatus.success: return _mapper.Map(exec.Result!); default: throw new NotSupportedException(exec.Status.ToString()); } } [HttpPost("~/api/settings/account-categories")] public object AccountCategoriesAdd(AccountCategoryViewModel request) { var exec = _accountService.CategoryAdd(UserId, new AccountCategoryDto { Name = request.Name! }); switch (exec.Status) { case GeneralExecStatus.success: return exec.Result!; case GeneralExecStatus.failure: case GeneralExecStatus.not_found: return ProblemBadResponse("Adding account category failed."); default: throw new NotSupportedException(exec.Status.ToString()); } } [HttpPut("~/api/settings/account-categories/{id}")] public object AccountCategoriesEdit([AsGuid] string id, AccountCategoryViewModel request) { var exec = _accountService.CategoryUpdate(UserId, id.AsGuid(), new AccountCategoryDto { Name = request.Name! }); switch (exec.Status) { case GeneralExecStatus.success: return exec.Result!; case GeneralExecStatus.failure: case GeneralExecStatus.not_found: return ProblemBadResponse("Update account category failed."); default: throw new NotSupportedException(exec.Status.ToString()); } } [HttpDelete("~/api/settings/account-categories/{id}")] public object AccountCategoriesDelete([AsGuid] string id) { var exec = _accountService.CategoryRemove(UserId, id.AsGuid()); switch (exec.Status) { case AccountCategoryRemoveResult.success: return exec.Result!; case AccountCategoryRemoveResult.failure: return ProblemBadResponse("Remove account category failed."); case AccountCategoryRemoveResult.not_found: return ProblemBadResponse("Account category not found."); case AccountCategoryRemoveResult.accounts_exists: return ProblemBadResponse("Account category have accounts."); default: throw new NotSupportedException(exec.Status.ToString()); } } [HttpGet("~/api/settings/accounts")] public List AccountsGet([AsGuid(true)] string? category) { var list = category.IsPresent() ? _accountService.GetByCategory(UserId, category!.AsGuid()) : _accountService.GetAllAccounts(UserId); return _mapper.Map>(list.OrderBy(x => x.Name)); } [HttpPost("~/api/settings/accounts")] public object AccountsAdd(AccountViewModel request) { var exec = _accountService.AccountAdd(UserId, new AccountAdd { Name = request.Name, CurrencyId = request.CurrencyId, CategoryId = request.CategoryId.AsGuid(), Type = request.Type, }); switch (exec.Status) { case AccountAddStatus.category_not_found: return ProblemBadResponse("Category not found."); case AccountAddStatus.currency_not_found: return ProblemBadResponse("Currency not found."); case AccountAddStatus.failure: return ProblemBadResponse("Adding account failed."); case AccountAddStatus.success: return exec.Result!; default: throw new NotSupportedException(exec.Status.ToString()); } } [HttpPut("~/api/settings/accounts/{id}")] public object AccountsAdd([AsGuid] string id, AccountEditRequestModel request) { var exec = _accountService.AccountUpdate(UserId, id.AsGuid(), new AccountEdit { Name = request.Name, CurrencyId = request.CurrencyId, CategoryId = request.CategoryId?.AsGuidNull(), UserId = request.UserId?.AsGuidNull(), Type = request.Type, }); switch (exec.Status) { case AccountEditStatus.not_found: return ProblemBadResponse("Account not found."); case AccountEditStatus.category_not_found: return ProblemBadResponse("Category not found."); case AccountEditStatus.currency_not_found: return ProblemBadResponse("Currency not found."); case AccountEditStatus.failure: return ProblemBadResponse("Adding account failed."); case AccountEditStatus.success: return exec.Result!; default: throw new NotSupportedException(exec.Status.ToString()); } } [HttpDelete("~/api/settings/accounts/{id}")] public object AccountsDelete([AsGuid] string id) { var exec = _accountService.AccountDelete(UserId, id); switch (exec.Status) { case GeneralExecStatus.not_found: return ProblemBadResponse("Account not found."); case GeneralExecStatus.success: return exec.Result!; default: throw new NotSupportedException(exec.Status.ToString()); } } [HttpDelete("~/api/settings/accounts/{id}/category/{categoryId}")] public object AccountsCategoryRemove([AsGuid] string id, [AsGuid] string categoryId) { var exec = _accountService.AccountCategoryRemove(UserId, id.AsGuid(), categoryId.AsGuid()); switch (exec.Status) { case GeneralExecStatus.not_found: case GeneralExecStatus.failure: return ProblemBadResponse("Category not found."); case GeneralExecStatus.success: return exec.Result!; default: throw new NotSupportedException(exec.Status.ToString()); } } [HttpPost("~/api/settings/accounts/{id}/access")] public object AccountsAdd([AsGuid] string id, AccountAccessViewModel request) { var model = _mapper.Map>(request.Accesses); var exec = _accountService.AccessUpdate(UserId, id.AsGuid(), model); switch (exec.Status) { case GeneralExecStatus.not_found: case GeneralExecStatus.failure: return ProblemBadResponse("Account not found."); case GeneralExecStatus.success: break; default: throw new NotSupportedException(exec.Status.ToString()); } if (!request.Email.IsPresent()) { return exec.Result!; } var inviteExec = _accountService.AccessInvite(UserId, id.AsGuid(), request.Email!, request.AllowWrite); switch (inviteExec.Status) { case AccessInviteStatus.account_not_found: return ProblemBadResponse("Account not found."); case AccessInviteStatus.access_exists: case AccessInviteStatus.invite_exists: case AccessInviteStatus.success: return exec.Result!; default: throw new NotSupportedException(exec.Status.ToString()); } } [HttpDelete("~/api/settings/accounts/{id}/access/{userId}")] public object AccountsAdd([AsGuid] string id, [AsGuid] string userId) { var exec = _accountService.AccessDelete(UserId, id.AsGuid(), userId.AsGuid()); switch (exec.Status) { case GeneralExecStatus.not_found: case GeneralExecStatus.failure: return ProblemBadResponse("Account not found."); case GeneralExecStatus.success: return exec.Result!; default: throw new NotSupportedException(exec.Status.ToString()); } } [HttpGet("~/api/settings/accounts/invites")] public List AccountInvites() { var invites = _accountService.InvitesGet(_contextProvider.User.Email); return _mapper.Map>(invites); } [HttpPost("~/api/settings/accounts/invites/{id}/accept")] public object AccountInviteAccept([AsGuid]string id, AccountInviteAcceptRequest request) { var exec = _accountService.InviteAccept(UserId, id.AsGuid(), request.Name); switch (exec.Status) { case InviteAcceptStatus.invite_not_found: return ProblemBadResponse("Invite not found."); case InviteAcceptStatus.account_not_found: return ProblemBadResponse("Account not found."); case InviteAcceptStatus.already_accepted: case InviteAcceptStatus.success: return _mapper.Map(exec.Result!); default: throw new NotSupportedException(exec.Status.ToString()); } } [HttpPost("~/api/settings/accounts/invites/{id}/reject")] public object AccountInviteReject([AsGuid] string id) { var exec = _accountService.InviteReject(id.AsGuid()); switch (exec.Status) { case GeneralExecStatus.not_found: case GeneralExecStatus.failure: return ProblemBadResponse("Invite not found."); case GeneralExecStatus.success: return _mapper.Map(exec.Result!); default: throw new NotSupportedException(exec.Status.ToString()); } } }