Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create a command to update the name for a course #87

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
.gradle
build/

.idea/

# Ignore Gradle GUI config
gradle-app.setting

Expand All @@ -10,3 +12,5 @@ gradle-app.setting

.env.local
.env.*.local


Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,26 +31,35 @@ public CourseGetController(QueryBus queryBus, CommandBus commandBus) {
public ResponseEntity<HashMap<String, Serializable>> index(@PathVariable String id)
throws QueryHandlerExecutionError {
CourseResponse course = ask(new FindCourseQuery(id));
HashMap<String, Serializable> 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<HashMap<String, String>> courses()
throws QueryHandlerExecutionError {
CoursesResponse courses = ask(new FindCoursesQuery());
HashMap<String, String> 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<String, Serializable>() {
{
put("id", course.id());
put("name", course.name());
put("duration", course.duration());
}
}
);
.body(response);
}

@Override
public HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
return new HashMap<Class<? extends DomainError>, HttpStatus>() {
{
put(CourseNotExist.class, HttpStatus.NOT_FOUND);
}
};
HashMap<Class<? extends DomainError>, HttpStatus> errorMap = new HashMap<>();
errorMap.put(CourseNotExist.class, HttpStatus.NOT_FOUND);
return errorMap;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public HashMap<String, Integer> index() throws QueryHandlerExecutionError {

return new HashMap<String, Integer>() {
{
put("total", response.total());
put("testas total", response.total());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}


}
Original file line number Diff line number Diff line change
@@ -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<FindCoursesQuery, CoursesResponse> {

private final CoursesFinder finder;

public FindAllCoursesQueryHandler(CoursesFinder finder) {
this.finder = finder;
}

@Override
public CoursesResponse handle(FindCoursesQuery query) {
return finder.findAll();
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public interface CourseRepository {

Optional<Course> search(CourseId id);

List<Course> findAll();

List<Course> matching(Criteria criteria);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +22,12 @@ public Optional<Course> search(CourseId id) {
return Optional.ofNullable(courses.get(id.value()));
}

@Override
@Override
public List<Course> findAll() {
return new ArrayList<>(courses.values());
}

@Override
public List<Course> matching(Criteria criteria) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ public Optional<Course> search(CourseId id) {
return byId(id);
}

@Override
@Override
public List<Course> findAll() {
return all();
}

@Override
public List<Course> matching(Criteria criteria) {
return byCriteria(criteria);
}
Expand Down