Skip to content

Commit ef008f3

Browse files
committed
Add base api controller
1 parent 80c4617 commit ef008f3

File tree

7 files changed

+80
-26
lines changed

7 files changed

+80
-26
lines changed

apps/main/tv/codely/apps/backoffice/backend/controller/courses/CoursesGetController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import org.springframework.web.bind.annotation.*;
44
import tv.codely.backoffice.courses.application.BackofficeCoursesResponse;
55
import tv.codely.backoffice.courses.application.search_by_criteria.SearchBackofficeCoursesByCriteriaQuery;
6+
import tv.codely.shared.domain.bus.command.CommandBus;
67
import tv.codely.shared.domain.bus.query.QueryBus;
78
import tv.codely.shared.domain.bus.query.QueryHandlerExecutionError;
89
import tv.codely.shared.domain.bus.query.QueryNotRegisteredError;
10+
import tv.codely.shared.infrastructure.spring.ApiController;
911

1012
import java.io.Serializable;
1113
import java.util.ArrayList;
@@ -16,18 +18,16 @@
1618

1719
@RestController
1820
@CrossOrigin(origins = "*", methods = {RequestMethod.GET})
19-
public final class CoursesGetController {
20-
private final QueryBus bus;
21-
22-
public CoursesGetController(QueryBus bus) {
23-
this.bus = bus;
21+
public final class CoursesGetController extends ApiController {
22+
public CoursesGetController(QueryBus queryBus, CommandBus commandBus) {
23+
super(queryBus, commandBus);
2424
}
2525

2626
@GetMapping("/courses")
2727
public List<HashMap<String, String>> index(
2828
@RequestParam HashMap<String, Serializable> params
2929
) throws QueryNotRegisteredError, QueryHandlerExecutionError {
30-
BackofficeCoursesResponse courses = bus.ask(
30+
BackofficeCoursesResponse courses = ask(
3131
new SearchBackofficeCoursesByCriteriaQuery(
3232
parseFilters(params),
3333
Optional.ofNullable((String) params.get("order_by")),

apps/main/tv/codely/apps/backoffice/backend/controller/health_check/HealthCheckGetController.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22

33
import org.springframework.web.bind.annotation.GetMapping;
44
import org.springframework.web.bind.annotation.RestController;
5+
import tv.codely.shared.domain.bus.command.CommandBus;
6+
import tv.codely.shared.domain.bus.query.QueryBus;
7+
import tv.codely.shared.infrastructure.spring.ApiController;
58

69
import java.util.HashMap;
710

811
@RestController
9-
public final class HealthCheckGetController {
12+
public final class HealthCheckGetController extends ApiController {
13+
public HealthCheckGetController(
14+
QueryBus queryBus,
15+
CommandBus commandBus
16+
) {
17+
super(queryBus, commandBus);
18+
}
19+
1020
@GetMapping("/health-check")
1121
public HashMap<String, String> index() {
1222
HashMap<String, String> status = new HashMap<>();

apps/main/tv/codely/apps/mooc/backend/controller/courses/CoursesPutController.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@
1010
import tv.codely.shared.domain.bus.command.CommandBus;
1111
import tv.codely.shared.domain.bus.command.CommandHandlerExecutionError;
1212
import tv.codely.shared.domain.bus.command.CommandNotRegisteredError;
13+
import tv.codely.shared.domain.bus.query.QueryBus;
14+
import tv.codely.shared.infrastructure.spring.ApiController;
1315

1416
@RestController
15-
public final class CoursesPutController {
16-
private final CommandBus bus;
17-
18-
public CoursesPutController(CommandBus bus) {
19-
this.bus = bus;
17+
public final class CoursesPutController extends ApiController {
18+
public CoursesPutController(
19+
QueryBus queryBus,
20+
CommandBus commandBus
21+
) {
22+
super(queryBus, commandBus);
2023
}
2124

2225
@PutMapping(value = "/courses/{id}")
2326
public ResponseEntity<String> index(
2427
@PathVariable String id,
2528
@RequestBody Request request
2629
) throws CommandNotRegisteredError, CommandHandlerExecutionError {
27-
bus.dispatch(new CreateCourseCommand(id, request.name(), request.duration()));
30+
dispatch(new CreateCourseCommand(id, request.name(), request.duration()));
2831

2932
return new ResponseEntity<>(HttpStatus.CREATED);
3033
}

apps/main/tv/codely/apps/mooc/backend/controller/courses_counter/CoursesCounterGetController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44
import org.springframework.web.bind.annotation.RestController;
55
import tv.codely.mooc.courses_counter.application.find.CoursesCounterResponse;
66
import tv.codely.mooc.courses_counter.application.find.FindCoursesCounterQuery;
7+
import tv.codely.shared.domain.bus.command.CommandBus;
78
import tv.codely.shared.domain.bus.query.QueryBus;
89
import tv.codely.shared.domain.bus.query.QueryHandlerExecutionError;
910
import tv.codely.shared.domain.bus.query.QueryNotRegisteredError;
11+
import tv.codely.shared.infrastructure.spring.ApiController;
1012

1113
import java.util.HashMap;
1214

1315
@RestController
14-
public final class CoursesCounterGetController {
15-
QueryBus bus;
16-
17-
public CoursesCounterGetController(QueryBus bus) {
18-
this.bus = bus;
16+
public final class CoursesCounterGetController extends ApiController {
17+
public CoursesCounterGetController(QueryBus queryBus, CommandBus commandBus) {
18+
super(queryBus, commandBus);
1919
}
2020

2121
@GetMapping("/courses-counter")
2222
public HashMap<String, Integer> index() throws QueryNotRegisteredError, QueryHandlerExecutionError {
23-
CoursesCounterResponse response = bus.ask(new FindCoursesCounterQuery());
23+
CoursesCounterResponse response = ask(new FindCoursesCounterQuery());
2424

2525
return new HashMap<String, Integer>() {{
2626
put("total", response.total());

apps/main/tv/codely/apps/mooc/backend/controller/health_check/HealthCheckGetController.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22

33
import org.springframework.web.bind.annotation.GetMapping;
44
import org.springframework.web.bind.annotation.RestController;
5+
import tv.codely.shared.domain.bus.command.CommandBus;
6+
import tv.codely.shared.domain.bus.query.QueryBus;
7+
import tv.codely.shared.infrastructure.spring.ApiController;
58

69
import java.util.HashMap;
710

811
@RestController
9-
public final class HealthCheckGetController {
12+
public final class HealthCheckGetController extends ApiController {
13+
public HealthCheckGetController(
14+
QueryBus queryBus,
15+
CommandBus commandBus
16+
) {
17+
super(queryBus, commandBus);
18+
}
19+
1020
@GetMapping("/health-check")
1121
public HashMap<String, String> index() {
1222
HashMap<String, String> status = new HashMap<>();

apps/main/tv/codely/apps/mooc/backend/controller/notifications/NewsletterNotificationPutController.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@
99
import tv.codely.shared.domain.bus.command.CommandBus;
1010
import tv.codely.shared.domain.bus.command.CommandHandlerExecutionError;
1111
import tv.codely.shared.domain.bus.command.CommandNotRegisteredError;
12+
import tv.codely.shared.domain.bus.query.QueryBus;
13+
import tv.codely.shared.infrastructure.spring.ApiController;
1214

1315
@RestController
14-
public final class NewsletterNotificationPutController {
15-
private final CommandBus bus;
16-
17-
public NewsletterNotificationPutController(CommandBus bus) {
18-
this.bus = bus;
16+
public final class NewsletterNotificationPutController extends ApiController {
17+
public NewsletterNotificationPutController(
18+
QueryBus queryBus,
19+
CommandBus commandBus
20+
) {
21+
super(queryBus, commandBus);
1922
}
2023

2124
@PutMapping(value = "/newsletter/{id}")
2225
public ResponseEntity<String> index(
2326
@PathVariable String id
2427
) throws CommandNotRegisteredError, CommandHandlerExecutionError {
25-
bus.dispatch(new SendNewCoursesNewsletterCommand(id));
28+
dispatch(new SendNewCoursesNewsletterCommand(id));
2629

2730
return new ResponseEntity<>(HttpStatus.CREATED);
2831
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tv.codely.shared.infrastructure.spring;
2+
3+
import tv.codely.shared.domain.bus.command.Command;
4+
import tv.codely.shared.domain.bus.command.CommandBus;
5+
import tv.codely.shared.domain.bus.command.CommandHandlerExecutionError;
6+
import tv.codely.shared.domain.bus.command.CommandNotRegisteredError;
7+
import tv.codely.shared.domain.bus.query.Query;
8+
import tv.codely.shared.domain.bus.query.QueryBus;
9+
import tv.codely.shared.domain.bus.query.QueryHandlerExecutionError;
10+
import tv.codely.shared.domain.bus.query.QueryNotRegisteredError;
11+
12+
public abstract class ApiController {
13+
private final QueryBus queryBus;
14+
private final CommandBus commandBus;
15+
16+
public ApiController(QueryBus queryBus, CommandBus commandBus) {
17+
this.queryBus = queryBus;
18+
this.commandBus = commandBus;
19+
}
20+
21+
protected void dispatch(Command command) throws CommandNotRegisteredError, CommandHandlerExecutionError {
22+
commandBus.dispatch(command);
23+
}
24+
25+
protected <R> R ask(Query query) throws QueryNotRegisteredError, QueryHandlerExecutionError {
26+
return queryBus.ask(query);
27+
}
28+
}

0 commit comments

Comments
 (0)