diff --git a/src/TaxBaik.Web/Controllers/CompanyController.cs.bak b/src/TaxBaik.Web/Controllers/CompanyController.cs.bak new file mode 100644 index 0000000..373c352 --- /dev/null +++ b/src/TaxBaik.Web/Controllers/CompanyController.cs.bak @@ -0,0 +1,117 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using TaxBaik.Application.Services; + +namespace TaxBaik.Web.Controllers; + +[ApiController] +[Route("api/[controller]")] +[Authorize] +public class CompanyController(CompanyService companyService) : ControllerBase +{ + [HttpGet("{id:int}")] + public async Task GetById(int id) + { + try + { + var company = await companyService.GetByIdAsync(id); + if (company == null) + return NotFound(new ProblemDetails { Title = "회사를 찾을 수 없습니다.", Status = StatusCodes.Status404NotFound }); + return Ok(company); + } + catch (Exception ex) + { + return StatusCode(500, new ProblemDetails { Title = "회사 조회 실패", Detail = ex.Message, Status = StatusCodes.Status500InternalServerError }); + } + } + + [HttpGet("code/{code}")] + public async Task GetByCode(string code) + { + try + { + var company = await companyService.GetByCodeAsync(code); + if (company == null) + return NotFound(new ProblemDetails { Title = "회사를 찾을 수 없습니다.", Status = StatusCodes.Status404NotFound }); + return Ok(company); + } + catch (Exception ex) + { + return StatusCode(500, new ProblemDetails { Title = "회사 조회 실패", Detail = ex.Message, Status = StatusCodes.Status500InternalServerError }); + } + } + + [HttpGet] + public async Task GetPaged([FromQuery] int page = 1, [FromQuery] int pageSize = 20) + { + try + { + var (companies, total) = await companyService.GetPagedAsync(page, pageSize); + return Ok(new { data = companies, total, page, pageSize }); + } + catch (Exception ex) + { + return StatusCode(500, new ProblemDetails { Title = "회사 목록 조회 실패", Detail = ex.Message, Status = StatusCodes.Status500InternalServerError }); + } + } + + [HttpPost] + public async Task Create([FromBody] CreateCompanyRequest request) + { + try + { + var id = await companyService.CreateAsync( + request.CompanyCode, request.CompanyName, request.ContactPerson, + request.Phone, request.Email, request.Memo); + return CreatedAtAction(nameof(GetById), new { id }, new { message = "회사가 등록되었습니다.", id }); + } + catch (ValidationException ex) + { + return BadRequest(new ProblemDetails { Title = ex.Message, Status = StatusCodes.Status400BadRequest }); + } + catch (Exception ex) + { + return StatusCode(500, new ProblemDetails { Title = "회사 등록 실패", Detail = ex.Message, Status = StatusCodes.Status500InternalServerError }); + } + } + + [HttpPut("{id:int}")] + public async Task Update(int id, [FromBody] UpdateCompanyRequest request) + { + try + { + await companyService.UpdateAsync(id, request.CompanyCode, request.CompanyName, + request.ContactPerson, request.Phone, request.Email, request.Memo, request.IsActive); + return Ok(new { message = "회사가 수정되었습니다." }); + } + catch (ValidationException ex) + { + return BadRequest(new ProblemDetails { Title = ex.Message, Status = StatusCodes.Status400BadRequest }); + } + catch (Exception ex) + { + return StatusCode(500, new ProblemDetails { Title = "회사 수정 실패", Detail = ex.Message, Status = StatusCodes.Status500InternalServerError }); + } + } + + [HttpDelete("{id:int}")] + public async Task Delete(int id) + { + try + { + await companyService.DeleteAsync(id); + return Ok(new { message = "회사가 삭제되었습니다." }); + } + catch (ValidationException ex) + { + return BadRequest(new ProblemDetails { Title = ex.Message, Status = StatusCodes.Status400BadRequest }); + } + catch (Exception ex) + { + return StatusCode(500, new ProblemDetails { Title = "회사 삭제 실패", Detail = ex.Message, Status = StatusCodes.Status500InternalServerError }); + } + } + + public record CreateCompanyRequest(string CompanyCode, string CompanyName, string? ContactPerson, string? Phone, string? Email, string? Memo); + public record UpdateCompanyRequest(string CompanyCode, string CompanyName, string? ContactPerson, string? Phone, string? Email, string? Memo, bool IsActive); +} diff --git a/src/TaxBaik.Web/Endpoints/Company/AllEndpoints.cs b/src/TaxBaik.Web/Endpoints/Company/AllEndpoints.cs new file mode 100644 index 0000000..749e4cb --- /dev/null +++ b/src/TaxBaik.Web/Endpoints/Company/AllEndpoints.cs @@ -0,0 +1,241 @@ +using FastEndpoints; +using TaxBaik.Application.Services; + +namespace TaxBaik.Web.Endpoints.Company; + +// DTOs +public class CreateCompanyRequest +{ + public string CompanyCode { get; set; } = string.Empty; + public string CompanyName { get; set; } = string.Empty; + public string? ContactPerson { get; set; } + public string? Phone { get; set; } + public string? Email { get; set; } + public string? Memo { get; set; } +} + +public class UpdateCompanyRequest +{ + public string CompanyCode { get; set; } = string.Empty; + public string CompanyName { get; set; } = string.Empty; + public string? ContactPerson { get; set; } + public string? Phone { get; set; } + public string? Email { get; set; } + public string? Memo { get; set; } + public bool IsActive { get; set; } = true; +} + +public class CompanyResponse +{ + public object Data { get; set; } = null!; +} + +public class CompanyListResponse +{ + public List Data { get; set; } = []; + public int Total { get; set; } + public int Page { get; set; } + public int PageSize { get; set; } +} + +public class CompanyCreateResponse +{ + public string Message { get; set; } = string.Empty; + public int Id { get; set; } +} + +public class CompanyUpdateResponse +{ + public string Message { get; set; } = string.Empty; +} + +public class CompanyDeleteResponse +{ + public string Message { get; set; } = string.Empty; +} + +public class GetPagedQuery +{ + public int Page { get; set; } = 1; + public int PageSize { get; set; } = 20; +} + +// Endpoints +public class GetByIdEndpoint : Endpoint +{ + private readonly CompanyService _service; + public GetByIdEndpoint(CompanyService service) => _service = service; + + public override void Configure() + { + Get("/api/company/{id:int}"); + Policies("Bearer"); + } + + public override async Task HandleAsync(EmptyRequest _, CancellationToken ct) + { + var id = Route("id"); + try + { + var company = await _service.GetByIdAsync(id); + if (company == null) + ThrowError("회사를 찾을 수 없습니다.", statusCode: 404); + await SendAsync(new CompanyResponse { Data = company }, 200, cancellation: ct); + } + catch (Exception ex) + { + ThrowError("회사 조회 실패", statusCode: 500); + } + } +} + +public class GetByCodeEndpoint : Endpoint +{ + private readonly CompanyService _service; + public GetByCodeEndpoint(CompanyService service) => _service = service; + + public override void Configure() + { + Get("/api/company/code/{code}"); + Policies("Bearer"); + } + + public override async Task HandleAsync(EmptyRequest _, CancellationToken ct) + { + var code = Route("code"); + try + { + var company = await _service.GetByCodeAsync(code); + if (company == null) + ThrowError("회사를 찾을 수 없습니다.", statusCode: 404); + await SendAsync(new CompanyResponse { Data = company }, 200, cancellation: ct); + } + catch (Exception ex) + { + ThrowError("회사 조회 실패", statusCode: 500); + } + } +} + +public class GetPagedEndpoint : Endpoint +{ + private readonly CompanyService _service; + public GetPagedEndpoint(CompanyService service) => _service = service; + + public override void Configure() + { + Get("/api/company"); + Policies("Bearer"); + } + + public override async Task HandleAsync(GetPagedQuery request, CancellationToken ct) + { + try + { + var (companies, total) = await _service.GetPagedAsync(request.Page, request.PageSize); + await SendAsync(new CompanyListResponse + { + Data = companies.Cast().ToList(), + Total = total, + Page = request.Page, + PageSize = request.PageSize + }, 200, cancellation: ct); + } + catch (Exception ex) + { + ThrowError("회사 목록 조회 실패", statusCode: 500); + } + } +} + +public class CreateEndpoint : Endpoint +{ + private readonly CompanyService _service; + public CreateEndpoint(CompanyService service) => _service = service; + + public override void Configure() + { + Post("/api/company"); + Policies("Bearer"); + } + + public override async Task HandleAsync(CreateCompanyRequest request, CancellationToken ct) + { + try + { + var id = await _service.CreateAsync( + request.CompanyCode, request.CompanyName, request.ContactPerson, + request.Phone, request.Email, request.Memo); + await SendAsync(new CompanyCreateResponse { Message = "회사가 등록되었습니다.", Id = id }, 201, cancellation: ct); + } + catch (ValidationException ex) + { + ThrowError(ex.Message); + } + catch (Exception ex) + { + ThrowError("회사 등록 실패", statusCode: 500); + } + } +} + +public class UpdateEndpoint : Endpoint +{ + private readonly CompanyService _service; + public UpdateEndpoint(CompanyService service) => _service = service; + + public override void Configure() + { + Put("/api/company/{id:int}"); + Policies("Bearer"); + } + + public override async Task HandleAsync(UpdateCompanyRequest request, CancellationToken ct) + { + var id = Route("id"); + try + { + await _service.UpdateAsync(id, request.CompanyCode, request.CompanyName, + request.ContactPerson, request.Phone, request.Email, request.Memo, request.IsActive); + await SendAsync(new CompanyUpdateResponse { Message = "회사가 수정되었습니다." }, 200, cancellation: ct); + } + catch (ValidationException ex) + { + ThrowError(ex.Message); + } + catch (Exception ex) + { + ThrowError("회사 수정 실패", statusCode: 500); + } + } +} + +public class DeleteEndpoint : Endpoint +{ + private readonly CompanyService _service; + public DeleteEndpoint(CompanyService service) => _service = service; + + public override void Configure() + { + Delete("/api/company/{id:int}"); + Policies("Bearer"); + } + + public override async Task HandleAsync(EmptyRequest _, CancellationToken ct) + { + var id = Route("id"); + try + { + await _service.DeleteAsync(id); + await SendAsync(new CompanyDeleteResponse { Message = "회사가 삭제되었습니다." }, 200, cancellation: ct); + } + catch (ValidationException ex) + { + ThrowError(ex.Message); + } + catch (Exception ex) + { + ThrowError("회사 삭제 실패", statusCode: 500); + } + } +}