-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose REST API for create an expense category
- Loading branch information
Showing
11 changed files
with
406 additions
and
5 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
server/src/main/java/io/myfinbox/expense/adapter/web/ExpenseCategoryController.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.myfinbox.expense.adapter.web; | ||
|
||
import io.myfinbox.expense.application.CategoryService; | ||
import io.myfinbox.expense.domain.Category; | ||
import io.myfinbox.shared.ApiFailureHandler; | ||
import io.myfinbox.shared.ExpenseCategoryResource; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import static io.myfinbox.expense.application.CategoryService.CategoryCommand; | ||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; | ||
import static org.springframework.http.ResponseEntity.created; | ||
import static org.springframework.web.servlet.support.ServletUriComponentsBuilder.fromCurrentRequest; | ||
|
||
@RestController | ||
@RequestMapping(path = "/expenses/categories") | ||
@RequiredArgsConstructor | ||
final class ExpenseCategoryController implements ExpenseCategoryControllerApi { | ||
|
||
private final CategoryService categoryService; | ||
private final ApiFailureHandler apiFailureHandler; | ||
|
||
@PostMapping(consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE) | ||
public ResponseEntity<?> create(@RequestBody ExpenseCategoryResource resource) { | ||
return categoryService.create(new CategoryCommand(resource.getName(), resource.getAccountId())) | ||
.fold(apiFailureHandler::handle, | ||
category -> created(fromCurrentRequest().path("/{id}").build(category.getId().id())) | ||
.body(toResource(category))); | ||
} | ||
|
||
private ExpenseCategoryResource toResource(Category category) { | ||
return new ExpenseCategoryResource() | ||
.categoryId(category.getId().id()) | ||
.accountId(category.getAccount().id()) | ||
.name(category.getName()) | ||
.creationTimestamp(category.getCreationTimestamp().toString()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
server/src/main/java/io/myfinbox/expense/adapter/web/ExpenseCategoryControllerApi.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.myfinbox.expense.adapter.web; | ||
|
||
import io.myfinbox.shared.ApiErrorResponse; | ||
import io.myfinbox.shared.ExpenseCategoryResource; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.headers.Header; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.parameters.RequestBody; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import java.net.URI; | ||
|
||
import static io.myfinbox.expense.adapter.web.ExpenseControllerApi.TAG; | ||
import static org.springframework.http.HttpHeaders.LOCATION; | ||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; | ||
|
||
public interface ExpenseCategoryControllerApi { | ||
|
||
@Operation(summary = "Add a new expense category in the MyFinBox", description = "Add a new expense category in the MyFinBox", | ||
security = {@SecurityRequirement(name = "openId")}, | ||
tags = {TAG}) | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "201", description = "Successful Operation", | ||
headers = @Header(name = LOCATION, description = "Created expense category URI location", schema = @Schema(implementation = URI.class)), | ||
content = @Content(mediaType = APPLICATION_JSON_VALUE, schema = @Schema(implementation = ExpenseCategoryResource.class))), | ||
@ApiResponse(responseCode = "400", description = "Malformed JSON or Type Mismatch Failure", | ||
content = @Content(mediaType = APPLICATION_JSON_VALUE, schema = @Schema(implementation = ApiErrorResponse.class))), | ||
@ApiResponse(responseCode = "409", description = "Category name already exists", | ||
content = @Content(mediaType = APPLICATION_JSON_VALUE, schema = @Schema(implementation = ApiErrorResponse.class))), | ||
@ApiResponse(responseCode = "422", description = "Request Schema Validation Failure", | ||
content = @Content(mediaType = APPLICATION_JSON_VALUE, schema = @Schema(implementation = ApiErrorResponse.class))), | ||
@ApiResponse(responseCode = "500", description = "Internal Server Error", | ||
content = @Content(mediaType = APPLICATION_JSON_VALUE, schema = @Schema(implementation = ApiErrorResponse.class))) | ||
}) | ||
ResponseEntity<?> create(@RequestBody(description = "Expense Category Resource to be created", required = true) ExpenseCategoryResource resource); | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.