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.
[backoffice-course] List all existing courses
- Loading branch information
1 parent
d0160de
commit 5247595
Showing
10 changed files
with
182 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
44 changes: 44 additions & 0 deletions
44
apps/main/resources/backoffice_frontend/templates/pages/courses/partials/list_courses.ftl
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 @@ | ||
<h3 class="font-sans text-gray-800 text-center text-3xl mb-10">Cursos existentes</h3> | ||
|
||
<table class="text-left w-full border-collapse"> | ||
<thead> | ||
<tr> | ||
<th class="py-4 px-6 bg-grey-lightest font-bold uppercase text-sm text-grey-dark border-b border-grey-light">Id</th> | ||
<th class="py-4 px-6 bg-grey-lightest font-bold uppercase text-sm text-grey-dark border-b border-grey-light">Nombre</th> | ||
<th class="py-4 px-6 bg-grey-lightest font-bold uppercase text-sm text-grey-dark border-b border-grey-light">Duración</th> | ||
</tr> | ||
</thead> | ||
<tbody id="courses-list"> | ||
|
||
</tbody> | ||
</table> | ||
|
||
<script> | ||
const tableBody = document.getElementById("courses-list"); | ||
fetch("/api/courses") | ||
.then(function (response) { | ||
return response.json(); | ||
}) | ||
.then(function (courses) { | ||
tableBody.innerHTML = ""; | ||
courses.forEach(function (course) { | ||
let courseRow = document.createElement("tr"); | ||
courseRow.appendChild(tableCellFor(course.id)); | ||
courseRow.appendChild(tableCellFor(course.name)); | ||
courseRow.appendChild(tableCellFor(course.duration)); | ||
tableBody.appendChild(courseRow); | ||
}) | ||
}); | ||
function tableCellFor(text) { | ||
const cell = document.createElement("td"); | ||
cell.appendChild(document.createTextNode(text)); | ||
return cell; | ||
} | ||
</script> |
32 changes: 32 additions & 0 deletions
32
apps/main/tv/codely/apps/backoffice/frontend/controller/courses/ApiCoursesGetController.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,32 @@ | ||
package tv.codely.apps.backoffice.frontend.controller.courses; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import tv.codely.backoffice.courses.application.BackofficeCoursesResponse; | ||
import tv.codely.backoffice.courses.application.search_all.SearchAllBackofficeCoursesQuery; | ||
import tv.codely.shared.domain.bus.query.QueryBus; | ||
import tv.codely.shared.domain.bus.query.QueryNotRegisteredError; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@RestController | ||
public final class ApiCoursesGetController { | ||
private final QueryBus bus; | ||
|
||
public ApiCoursesGetController(QueryBus bus) { | ||
this.bus = bus; | ||
} | ||
|
||
@GetMapping("/api/courses") | ||
public List<HashMap<String, String>> index() throws QueryNotRegisteredError { | ||
BackofficeCoursesResponse courses = bus.ask(new SearchAllBackofficeCoursesQuery()); | ||
|
||
return courses.courses().stream().map(response -> new HashMap<String, String>() {{ | ||
put("id", response.id()); | ||
put("name", response.name()); | ||
put("duration", response.duration()); | ||
}}).collect(Collectors.toList()); | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
src/backoffice/main/tv/codely/backoffice/courses/application/BackofficeCourseResponse.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,32 @@ | ||
package tv.codely.backoffice.courses.application; | ||
|
||
import tv.codely.backoffice.courses.domain.BackofficeCourse; | ||
import tv.codely.shared.domain.bus.query.Response; | ||
|
||
public final class BackofficeCourseResponse implements Response { | ||
private final String id; | ||
private final String name; | ||
private final String duration; | ||
|
||
public BackofficeCourseResponse(String id, String name, String duration) { | ||
this.id = id; | ||
this.name = name; | ||
this.duration = duration; | ||
} | ||
|
||
public static BackofficeCourseResponse fromAggregate(BackofficeCourse course) { | ||
return new BackofficeCourseResponse(course.id(), course.name(), course.duration()); | ||
} | ||
|
||
public String id() { | ||
return id; | ||
} | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
public String duration() { | ||
return duration; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/backoffice/main/tv/codely/backoffice/courses/application/BackofficeCoursesResponse.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,17 @@ | ||
package tv.codely.backoffice.courses.application; | ||
|
||
import tv.codely.shared.domain.bus.query.Response; | ||
|
||
import java.util.List; | ||
|
||
public final class BackofficeCoursesResponse implements Response { | ||
private final List<BackofficeCourseResponse> courses; | ||
|
||
public BackofficeCoursesResponse(List<BackofficeCourseResponse> courses) { | ||
this.courses = courses; | ||
} | ||
|
||
public List<BackofficeCourseResponse> courses() { | ||
return courses; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ain/tv/codely/backoffice/courses/application/search_all/AllBackofficeCoursesSearcher.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,23 @@ | ||
package tv.codely.backoffice.courses.application.search_all; | ||
|
||
import tv.codely.backoffice.courses.application.BackofficeCourseResponse; | ||
import tv.codely.backoffice.courses.application.BackofficeCoursesResponse; | ||
import tv.codely.backoffice.courses.domain.BackofficeCourseRepository; | ||
import tv.codely.shared.domain.Service; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public final class AllBackofficeCoursesSearcher { | ||
private final BackofficeCourseRepository repository; | ||
|
||
public AllBackofficeCoursesSearcher(BackofficeCourseRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
public BackofficeCoursesResponse search() { | ||
return new BackofficeCoursesResponse( | ||
repository.searchAll().stream().map(BackofficeCourseResponse::fromAggregate).collect(Collectors.toList()) | ||
); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
.../tv/codely/backoffice/courses/application/search_all/SearchAllBackofficeCoursesQuery.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,6 @@ | ||
package tv.codely.backoffice.courses.application.search_all; | ||
|
||
import tv.codely.shared.domain.bus.query.Query; | ||
|
||
public final class SearchAllBackofficeCoursesQuery implements Query { | ||
} |
19 changes: 19 additions & 0 deletions
19
...ely/backoffice/courses/application/search_all/SearchAllBackofficeCoursesQueryHandler.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,19 @@ | ||
package tv.codely.backoffice.courses.application.search_all; | ||
|
||
import tv.codely.backoffice.courses.application.BackofficeCoursesResponse; | ||
import tv.codely.shared.domain.Service; | ||
import tv.codely.shared.domain.bus.query.QueryHandler; | ||
|
||
@Service | ||
public final class SearchAllBackofficeCoursesQueryHandler implements QueryHandler<SearchAllBackofficeCoursesQuery, BackofficeCoursesResponse> { | ||
private final AllBackofficeCoursesSearcher searcher; | ||
|
||
public SearchAllBackofficeCoursesQueryHandler(AllBackofficeCoursesSearcher searcher) { | ||
this.searcher = searcher; | ||
} | ||
|
||
@Override | ||
public BackofficeCoursesResponse handle(SearchAllBackofficeCoursesQuery query) { | ||
return searcher.search(); | ||
} | ||
} |
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