Skip to content

Commit f370ef6

Browse files
committed
create a command to update the name for a course
1 parent 06404d0 commit f370ef6

10 files changed

Lines changed: 109 additions & 27 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
.gradle
33
build/
44

5+
.idea/
6+
57
# Ignore Gradle GUI config
68
gradle-app.setting
79

@@ -11,5 +13,4 @@ gradle-app.setting
1113
.env.local
1214
.env.*.local
1315

14-
.idea
1516

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import org.springframework.web.bind.annotation.RestController;
1111

1212
import tv.codely.mooc.courses.application.CourseResponse;
13+
import tv.codely.mooc.courses.application.CoursesResponse;
1314
import tv.codely.mooc.courses.application.find.FindCourseQuery;
15+
import tv.codely.mooc.courses.application.find.FindCoursesQuery;
1416
import tv.codely.mooc.courses.domain.CourseNotExist;
1517
import tv.codely.shared.domain.DomainError;
1618
import tv.codely.shared.domain.bus.command.CommandBus;
@@ -29,26 +31,35 @@ public CourseGetController(QueryBus queryBus, CommandBus commandBus) {
2931
public ResponseEntity<HashMap<String, Serializable>> index(@PathVariable String id)
3032
throws QueryHandlerExecutionError {
3133
CourseResponse course = ask(new FindCourseQuery(id));
34+
HashMap<String, Serializable> response = new HashMap<>();
35+
response.put("id", course.id());
36+
response.put("name", course.name());
37+
response.put("duration", course.duration());
38+
return ResponseEntity
39+
.ok()
40+
.body(response);
41+
}
3242

43+
@GetMapping("/courses")
44+
public ResponseEntity<HashMap<String, String>> courses()
45+
throws QueryHandlerExecutionError {
46+
CoursesResponse courses = ask(new FindCoursesQuery());
47+
HashMap<String, String> response = new HashMap<>();
48+
courses.courses().forEach(course -> {
49+
response.put("id", course.id());
50+
response.put("name", course.name());
51+
response.put("duration", course.duration());
52+
});
3353
return ResponseEntity
3454
.ok()
35-
.body(
36-
new HashMap<String, Serializable>() {
37-
{
38-
put("id", course.id());
39-
put("name", course.name());
40-
put("duration", course.duration());
41-
}
42-
}
43-
);
55+
.body(response);
4456
}
4557

4658
@Override
4759
public HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
48-
return new HashMap<Class<? extends DomainError>, HttpStatus>() {
49-
{
50-
put(CourseNotExist.class, HttpStatus.NOT_FOUND);
51-
}
52-
};
60+
HashMap<Class<? extends DomainError>, HttpStatus> errorMap = new HashMap<>();
61+
errorMap.put(CourseNotExist.class, HttpStatus.NOT_FOUND);
62+
return errorMap;
63+
5364
}
5465
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public HashMap<String, Integer> index() throws QueryHandlerExecutionError {
2727

2828
return new HashMap<String, Integer>() {
2929
{
30-
put("total", response.total());
30+
put("testas total", response.total());
3131
}
3232
};
3333
}

src/mooc/main/tv/codely/mooc/courses/application/find/CourseFinder.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88

99
@Service
1010
public final class CourseFinder {
11-
private final CourseRepository repository;
11+
private final CourseRepository repository;
1212

13-
public CourseFinder(CourseRepository repository) {
14-
this.repository = repository;
15-
}
13+
public CourseFinder(CourseRepository repository) {
14+
this.repository = repository;
15+
}
16+
17+
public CourseResponse find(CourseId id) throws CourseNotExist {
18+
return repository.search(id)
19+
.map(CourseResponse::fromAggregate)
20+
.orElseThrow(() -> new CourseNotExist(id));
21+
}
1622

17-
public CourseResponse find(CourseId id) throws CourseNotExist {
18-
return repository.search(id)
19-
.map(CourseResponse::fromAggregate)
20-
.orElseThrow(() -> new CourseNotExist(id));
21-
}
2223
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package tv.codely.mooc.courses.application.find;
2+
3+
import tv.codely.mooc.courses.application.CourseResponse;
4+
import tv.codely.mooc.courses.application.CoursesResponse;
5+
import tv.codely.mooc.courses.domain.CourseNotExist;
6+
import tv.codely.mooc.courses.domain.CourseRepository;
7+
import tv.codely.shared.domain.Service;
8+
9+
import java.util.stream.Collectors;
10+
11+
@Service
12+
public final class CoursesFinder {
13+
14+
private final CourseRepository repository;
15+
16+
17+
public CoursesFinder(CourseRepository repository) {
18+
this.repository = repository;
19+
}
20+
21+
public CoursesResponse findAll() throws CourseNotExist {
22+
return new CoursesResponse(
23+
repository.findAll()
24+
.stream()
25+
.map(CourseResponse::fromAggregate)
26+
.collect(Collectors.toList()));
27+
}
28+
29+
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tv.codely.mooc.courses.application.find;
2+
3+
import tv.codely.shared.domain.Service;
4+
import tv.codely.shared.domain.bus.query.QueryHandler;
5+
import tv.codely.mooc.courses.application.CoursesResponse;
6+
7+
@Service
8+
public final class FindAllCoursesQueryHandler implements QueryHandler<FindCoursesQuery, CoursesResponse> {
9+
10+
private final CoursesFinder finder;
11+
12+
public FindAllCoursesQueryHandler(CoursesFinder finder) {
13+
this.finder = finder;
14+
}
15+
16+
@Override
17+
public CoursesResponse handle(FindCoursesQuery query) {
18+
return finder.findAll();
19+
}
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package tv.codely.mooc.courses.application.find;
2+
3+
import tv.codely.shared.domain.bus.query.Query;
4+
5+
public final class FindCoursesQuery implements Query {
6+
}

src/mooc/main/tv/codely/mooc/courses/domain/CourseRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ public interface CourseRepository {
1010

1111
Optional<Course> search(CourseId id);
1212

13+
List<Course> findAll();
14+
1315
List<Course> matching(Criteria criteria);
1416
}

src/mooc/main/tv/codely/mooc/courses/infrastructure/persistence/InMemoryCourseRepository.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import tv.codely.mooc.courses.domain.CourseRepository;
66
import tv.codely.shared.domain.criteria.Criteria;
77

8+
import java.util.ArrayList;
89
import java.util.HashMap;
910
import java.util.List;
1011
import java.util.Optional;
@@ -21,7 +22,12 @@ public Optional<Course> search(CourseId id) {
2122
return Optional.ofNullable(courses.get(id.value()));
2223
}
2324

24-
@Override
25+
@Override
26+
public List<Course> findAll() {
27+
return new ArrayList<>(courses.values());
28+
}
29+
30+
@Override
2531
public List<Course> matching(Criteria criteria) {
2632
return null;
2733
}

src/mooc/main/tv/codely/mooc/courses/infrastructure/persistence/MySqlCourseRepository.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ public Optional<Course> search(CourseId id) {
3030
return byId(id);
3131
}
3232

33-
@Override
33+
@Override
34+
public List<Course> findAll() {
35+
return all();
36+
}
37+
38+
@Override
3439
public List<Course> matching(Criteria criteria) {
3540
return byCriteria(criteria);
3641
}

0 commit comments

Comments
 (0)