Created
August 24, 2024 11:49
-
-
Save dpr-dev/2a67e91845476d5404502ad047758b0e to your computer and use it in GitHub Desktop.
example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // usecase | |
| class UseCaseX | |
| { | |
| private readonly UserCreator _userCreator; | |
| private readonly UserRoleAssigner _userRoleAssigner; | |
| public Task<ErrorOr<UserDto>> Execute(Input input) | |
| { | |
| var trx = beginTransaction(); | |
| var creationResult = await _userCreator.Create(new {...}); | |
| if(creationResult.isError) | |
| return ... | |
| var assignationResult = await _userRoleAssigner.Assign(creationResult.User.Id, [...roles...]); | |
| if(assignationResult.IsError) | |
| return ... | |
| orm.saveChanges(); | |
| trx.commit(); | |
| } | |
| } | |
| // api handler | |
| public async Task<IActionResult> CreateTemplate( | |
| [FromBody] CreateTemplateRequest request, | |
| [FromServices] IValidator<CreateTemplateRequest> validator, | |
| [FromServices] CreateTemplateUseCase useCase) | |
| { | |
| if (Validate(validator, request) | |
| is { IsValid: false } failedModelState) | |
| { | |
| return ValidationProblem(failedModelState); | |
| } | |
| ErrorOr<TemplateDto> result = await useCase.Execute(new CreateTemplateData | |
| { | |
| CreateTemplate = ...transform request data... | |
| }); | |
| if (result.IsError) | |
| { | |
| return new ErrorResult(result.FirstError); | |
| } | |
| return Json(new CreateTemplateResponse { Data = ...transform response data... }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment