Skip to content

Commit

Permalink
[backoffice-course] List all existing courses
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas committed Nov 18, 2019
1 parent d0160de commit 5247595
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
</div>

<#include "partials/new_course_form.ftl">
<div class="clearfix mb-10"></div>
<hr class="mb-10">
<#include "partials/list_courses.ftl">
</#macro>
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>
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

@RestController
public final class HealthCheckGetController {

@GetMapping("/health-check")
public HashMap<String, String> index() {
HashMap<String, String> status = new HashMap<>();
Expand Down
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;
}
}
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;
}
}
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())
);
}
}
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 {
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ public final class BackofficeCourse {
private final String name;
private final String duration;

public BackofficeCourse() {
id = null;
name = null;
duration = null;
}

public BackofficeCourse(String id, String name, String duration) {
this.id = id;
this.name = name;
Expand Down

0 comments on commit 5247595

Please sign in to comment.