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 SettingsAccountCategoryController : BaseApiController { private readonly ILogger _logger; private readonly IMapper _mapper; private readonly AccountService _accountService; private readonly IContextProvider _contextProvider; public SettingsAccountCategoryController( 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 OkResponse(_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()); } } }