forked from CodelyTV/java-ddd-example
-
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.
- Loading branch information
1 parent
ef008f3
commit 64bdb21
Showing
15 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
apps/main/tv/codely/apps/mooc/backend/controller/courses/CourseGetController.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,44 @@ | ||
package tv.codely.apps.mooc.backend.controller.courses; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import tv.codely.mooc.courses.application.CourseResponse; | ||
import tv.codely.mooc.courses.application.find.FindCourseQuery; | ||
import tv.codely.shared.domain.DomainError; | ||
import tv.codely.shared.domain.bus.command.CommandBus; | ||
import tv.codely.shared.domain.bus.query.QueryBus; | ||
import tv.codely.shared.domain.bus.query.QueryHandlerExecutionError; | ||
import tv.codely.shared.domain.bus.query.QueryNotRegisteredError; | ||
import tv.codely.shared.infrastructure.spring.ApiController; | ||
|
||
import java.io.Serializable; | ||
import java.util.HashMap; | ||
|
||
@RestController | ||
public final class CourseGetController extends ApiController { | ||
public CourseGetController( | ||
QueryBus queryBus, | ||
CommandBus commandBus | ||
) { | ||
super(queryBus, commandBus); | ||
} | ||
|
||
@GetMapping("/courses/{id}") | ||
public ResponseEntity<HashMap<String, Serializable>> index(@PathVariable String id) throws QueryHandlerExecutionError, QueryNotRegisteredError { | ||
CourseResponse course = ask(new FindCourseQuery(id)); | ||
|
||
return ResponseEntity.ok().body(new HashMap<String, Serializable>() {{ | ||
put("id", course.id()); | ||
put("name", course.name()); | ||
put("duration", course.duration()); | ||
}}); | ||
} | ||
|
||
@Override | ||
protected HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() { | ||
return null; | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
apps/test/tv/codely/apps/mooc/backend/controller/courses/CourseGetControllerShould.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,20 @@ | ||
package tv.codely.apps.mooc.backend.controller.courses; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import tv.codely.apps.mooc.MoocApplicationTestCase; | ||
|
||
final class CourseGetControllerShould extends MoocApplicationTestCase { | ||
@Test | ||
void find_an_existing_course() throws Exception { | ||
String id = "99ad55f5-6eab-4d73-b383-c63268e251e8"; | ||
String body = "{\"name\": \"The best course\", \"duration\": \"5 hours\"}"; | ||
|
||
givenThereIsACourse(id, body); | ||
|
||
assertResponse(String.format("/courses/%s", id), 200, body); | ||
} | ||
|
||
private void givenThereIsACourse(String id, String body) throws Exception { | ||
assertRequestWithBody("PUT", String.format("/courses/%s", id), body, 201); | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
src/mooc/main/tv/codely/mooc/courses/application/find/CourseFinder.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,22 @@ | ||
package tv.codely.mooc.courses.application.find; | ||
|
||
import tv.codely.mooc.courses.application.CourseResponse; | ||
import tv.codely.mooc.courses.domain.CourseId; | ||
import tv.codely.mooc.courses.domain.CourseNotExist; | ||
import tv.codely.mooc.courses.domain.CourseRepository; | ||
import tv.codely.shared.domain.Service; | ||
|
||
@Service | ||
public final class CourseFinder { | ||
private final CourseRepository repository; | ||
|
||
public CourseFinder(CourseRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
public CourseResponse find(CourseId id) throws CourseNotExist { | ||
return repository.search(id) | ||
.map(CourseResponse::fromAggregate) | ||
.orElseThrow(() -> new CourseNotExist(id)); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/mooc/main/tv/codely/mooc/courses/application/find/FindCourseQuery.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,15 @@ | ||
package tv.codely.mooc.courses.application.find; | ||
|
||
import tv.codely.shared.domain.bus.query.Query; | ||
|
||
public final class FindCourseQuery implements Query { | ||
private final String id; | ||
|
||
public FindCourseQuery(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String id() { | ||
return id; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/mooc/main/tv/codely/mooc/courses/application/find/FindCourseQueryHandler.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,25 @@ | ||
package tv.codely.mooc.courses.application.find; | ||
|
||
import tv.codely.mooc.courses.application.CourseResponse; | ||
import tv.codely.mooc.courses.domain.CourseId; | ||
import tv.codely.mooc.courses.domain.CourseNotExist; | ||
import tv.codely.shared.domain.Service; | ||
import tv.codely.shared.domain.bus.query.QueryHandler; | ||
|
||
@Service | ||
public final class FindCourseQueryHandler implements QueryHandler<FindCourseQuery, CourseResponse> { | ||
private final CourseFinder finder; | ||
|
||
public FindCourseQueryHandler(CourseFinder finder) { | ||
this.finder = finder; | ||
} | ||
|
||
@Override | ||
public CourseResponse handle(FindCourseQuery query) { | ||
try { | ||
return finder.find(new CourseId(query.id())); | ||
} catch (CourseNotExist error) { | ||
return null; | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/mooc/main/tv/codely/mooc/courses/domain/CourseNotExist.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,9 @@ | ||
package tv.codely.mooc.courses.domain; | ||
|
||
import tv.codely.shared.domain.DomainError; | ||
|
||
public final class CourseNotExist extends DomainError { | ||
public CourseNotExist(CourseId id) { | ||
super("course_not_exist", String.format("The course <%s> doesn't exist", id.value())); | ||
} | ||
} |
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,21 @@ | ||
package tv.codely.shared.domain; | ||
|
||
public abstract class DomainError extends Throwable { | ||
private final String errorCode; | ||
private final String errorMessage; | ||
|
||
public DomainError(String errorCode, String errorMessage) { | ||
super(errorMessage); | ||
|
||
this.errorCode = errorCode; | ||
this.errorMessage = errorMessage; | ||
} | ||
|
||
public String getErrorCode() { | ||
return errorCode; | ||
} | ||
|
||
public String getErrorMessage() { | ||
return errorMessage; | ||
} | ||
} |
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