diff --git a/.gitignore b/.gitignore index 8d89f106..43eb4e07 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ .gradle build/ +.idea/ + # Ignore Gradle GUI config gradle-app.setting @@ -10,3 +12,5 @@ gradle-app.setting .env.local .env.*.local + + diff --git a/apps/main/tv/codely/apps/mooc/backend/controller/courses/CourseGetController.java b/apps/main/tv/codely/apps/mooc/backend/controller/courses/CourseGetController.java index 838783b1..421c46b7 100644 --- a/apps/main/tv/codely/apps/mooc/backend/controller/courses/CourseGetController.java +++ b/apps/main/tv/codely/apps/mooc/backend/controller/courses/CourseGetController.java @@ -10,7 +10,9 @@ import org.springframework.web.bind.annotation.RestController; import tv.codely.mooc.courses.application.CourseResponse; +import tv.codely.mooc.courses.application.CoursesResponse; import tv.codely.mooc.courses.application.find.FindCourseQuery; +import tv.codely.mooc.courses.application.find.FindCoursesQuery; import tv.codely.mooc.courses.domain.CourseNotExist; import tv.codely.shared.domain.DomainError; import tv.codely.shared.domain.bus.command.CommandBus; @@ -29,26 +31,35 @@ public CourseGetController(QueryBus queryBus, CommandBus commandBus) { public ResponseEntity> index(@PathVariable String id) throws QueryHandlerExecutionError { CourseResponse course = ask(new FindCourseQuery(id)); + HashMap response = new HashMap<>(); + response.put("id", course.id()); + response.put("name", course.name()); + response.put("duration", course.duration()); + return ResponseEntity + .ok() + .body(response); + } + @GetMapping("/courses") + public ResponseEntity> courses() + throws QueryHandlerExecutionError { + CoursesResponse courses = ask(new FindCoursesQuery()); + HashMap response = new HashMap<>(); + courses.courses().forEach(course -> { + response.put("id", course.id()); + response.put("name", course.name()); + response.put("duration", course.duration()); + }); return ResponseEntity .ok() - .body( - new HashMap() { - { - put("id", course.id()); - put("name", course.name()); - put("duration", course.duration()); - } - } - ); + .body(response); } @Override public HashMap, HttpStatus> errorMapping() { - return new HashMap, HttpStatus>() { - { - put(CourseNotExist.class, HttpStatus.NOT_FOUND); - } - }; + HashMap, HttpStatus> errorMap = new HashMap<>(); + errorMap.put(CourseNotExist.class, HttpStatus.NOT_FOUND); + return errorMap; + } } diff --git a/apps/main/tv/codely/apps/mooc/backend/controller/courses_counter/CoursesCounterGetController.java b/apps/main/tv/codely/apps/mooc/backend/controller/courses_counter/CoursesCounterGetController.java index 88a932f8..5184a046 100644 --- a/apps/main/tv/codely/apps/mooc/backend/controller/courses_counter/CoursesCounterGetController.java +++ b/apps/main/tv/codely/apps/mooc/backend/controller/courses_counter/CoursesCounterGetController.java @@ -27,7 +27,7 @@ public HashMap index() throws QueryHandlerExecutionError { return new HashMap() { { - put("total", response.total()); + put("testas total", response.total()); } }; } diff --git a/src/mooc/main/tv/codely/mooc/courses/application/find/CourseFinder.java b/src/mooc/main/tv/codely/mooc/courses/application/find/CourseFinder.java index b912a3e6..ed1fe279 100644 --- a/src/mooc/main/tv/codely/mooc/courses/application/find/CourseFinder.java +++ b/src/mooc/main/tv/codely/mooc/courses/application/find/CourseFinder.java @@ -8,15 +8,16 @@ @Service public final class CourseFinder { - private final CourseRepository repository; + private final CourseRepository repository; - public CourseFinder(CourseRepository repository) { - this.repository = 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)); + } - public CourseResponse find(CourseId id) throws CourseNotExist { - return repository.search(id) - .map(CourseResponse::fromAggregate) - .orElseThrow(() -> new CourseNotExist(id)); - } } diff --git a/src/mooc/main/tv/codely/mooc/courses/application/find/CoursesFinder.java b/src/mooc/main/tv/codely/mooc/courses/application/find/CoursesFinder.java new file mode 100644 index 00000000..72012cc9 --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/courses/application/find/CoursesFinder.java @@ -0,0 +1,30 @@ +package tv.codely.mooc.courses.application.find; + +import tv.codely.mooc.courses.application.CourseResponse; +import tv.codely.mooc.courses.application.CoursesResponse; +import tv.codely.mooc.courses.domain.CourseNotExist; +import tv.codely.mooc.courses.domain.CourseRepository; +import tv.codely.shared.domain.Service; + +import java.util.stream.Collectors; + +@Service +public final class CoursesFinder { + + private final CourseRepository repository; + + + public CoursesFinder(CourseRepository repository) { + this.repository = repository; + } + + public CoursesResponse findAll() throws CourseNotExist { + return new CoursesResponse( + repository.findAll() + .stream() + .map(CourseResponse::fromAggregate) + .collect(Collectors.toList())); + } + + +} diff --git a/src/mooc/main/tv/codely/mooc/courses/application/find/FindAllCoursesQueryHandler.java b/src/mooc/main/tv/codely/mooc/courses/application/find/FindAllCoursesQueryHandler.java new file mode 100644 index 00000000..99277dae --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/courses/application/find/FindAllCoursesQueryHandler.java @@ -0,0 +1,20 @@ +package tv.codely.mooc.courses.application.find; + +import tv.codely.shared.domain.Service; +import tv.codely.shared.domain.bus.query.QueryHandler; +import tv.codely.mooc.courses.application.CoursesResponse; + +@Service +public final class FindAllCoursesQueryHandler implements QueryHandler { + + private final CoursesFinder finder; + + public FindAllCoursesQueryHandler(CoursesFinder finder) { + this.finder = finder; + } + + @Override + public CoursesResponse handle(FindCoursesQuery query) { + return finder.findAll(); + } +} diff --git a/src/mooc/main/tv/codely/mooc/courses/application/find/FindCoursesQuery.java b/src/mooc/main/tv/codely/mooc/courses/application/find/FindCoursesQuery.java new file mode 100644 index 00000000..9e05dbbd --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/courses/application/find/FindCoursesQuery.java @@ -0,0 +1,6 @@ +package tv.codely.mooc.courses.application.find; + +import tv.codely.shared.domain.bus.query.Query; + +public final class FindCoursesQuery implements Query { +} diff --git a/src/mooc/main/tv/codely/mooc/courses/application/update/UpdateCourseNameCommand.java b/src/mooc/main/tv/codely/mooc/courses/application/update/UpdateCourseNameCommand.java new file mode 100644 index 00000000..a1bec315 --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/courses/application/update/UpdateCourseNameCommand.java @@ -0,0 +1,24 @@ +package tv.codely.mooc.courses.application.update; + +import tv.codely.shared.domain.bus.command.Command; + +public final class UpdateCourseNameCommand implements Command { + + private final String id; + + private final String newName; + + public UpdateCourseNameCommand(String id, String newName) { + this.id = id; + this.newName = newName; + } + + public String id() { + return id; + } + + public String newName() { + return newName; + } + +} diff --git a/src/mooc/main/tv/codely/mooc/courses/domain/CourseRepository.java b/src/mooc/main/tv/codely/mooc/courses/domain/CourseRepository.java index 4d2e696d..aa84711c 100644 --- a/src/mooc/main/tv/codely/mooc/courses/domain/CourseRepository.java +++ b/src/mooc/main/tv/codely/mooc/courses/domain/CourseRepository.java @@ -10,5 +10,7 @@ public interface CourseRepository { Optional search(CourseId id); + List findAll(); + List matching(Criteria criteria); } diff --git a/src/mooc/main/tv/codely/mooc/courses/infrastructure/persistence/InMemoryCourseRepository.java b/src/mooc/main/tv/codely/mooc/courses/infrastructure/persistence/InMemoryCourseRepository.java index 622d60a0..faccfc9b 100644 --- a/src/mooc/main/tv/codely/mooc/courses/infrastructure/persistence/InMemoryCourseRepository.java +++ b/src/mooc/main/tv/codely/mooc/courses/infrastructure/persistence/InMemoryCourseRepository.java @@ -5,6 +5,7 @@ import tv.codely.mooc.courses.domain.CourseRepository; import tv.codely.shared.domain.criteria.Criteria; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Optional; @@ -21,7 +22,12 @@ public Optional search(CourseId id) { return Optional.ofNullable(courses.get(id.value())); } - @Override + @Override + public List findAll() { + return new ArrayList<>(courses.values()); + } + + @Override public List matching(Criteria criteria) { return null; } diff --git a/src/mooc/main/tv/codely/mooc/courses/infrastructure/persistence/MySqlCourseRepository.java b/src/mooc/main/tv/codely/mooc/courses/infrastructure/persistence/MySqlCourseRepository.java index b90c0cf2..78f8d5b4 100644 --- a/src/mooc/main/tv/codely/mooc/courses/infrastructure/persistence/MySqlCourseRepository.java +++ b/src/mooc/main/tv/codely/mooc/courses/infrastructure/persistence/MySqlCourseRepository.java @@ -30,7 +30,12 @@ public Optional search(CourseId id) { return byId(id); } - @Override + @Override + public List findAll() { + return all(); + } + + @Override public List matching(Criteria criteria) { return byCriteria(criteria); }