Add school management REST API and architecture checks
This commit is contained in:
@@ -5,6 +5,7 @@ import nl.agilecc.agiledancers.model.specifications.DanceGroupStyle;
|
||||
import nl.agilecc.agiledancers.model.specifications.DanceGroupType;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.LinkedHashSet;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@@ -12,17 +13,23 @@ import java.util.Set;
|
||||
@ToString(onlyExplicitlyIncluded = true)
|
||||
public class DanceGroup extends AbstractEntity {
|
||||
@ToString.Include
|
||||
private final String category;
|
||||
@Setter
|
||||
private String category;
|
||||
@ToString.Include
|
||||
private final DanceGroupType type;
|
||||
@Setter
|
||||
private DanceGroupType type;
|
||||
@ToString.Include
|
||||
private final int startAge;
|
||||
@Setter
|
||||
private int startAge;
|
||||
@ToString.Include
|
||||
private final int endAge;
|
||||
@Setter
|
||||
private int endAge;
|
||||
@ToString.Include
|
||||
private Set<Dancer> dancers;
|
||||
@Builder.Default
|
||||
private final Set<Dancer> dancers = new LinkedHashSet<>();
|
||||
@ToString.Include
|
||||
private Set<DanceGroupStyle> styles;
|
||||
@Builder.Default
|
||||
private final Set<DanceGroupStyle> styles = new LinkedHashSet<>();
|
||||
@ToString.Include
|
||||
private Coach coach;
|
||||
|
||||
@@ -41,4 +48,13 @@ public class DanceGroup extends AbstractEntity {
|
||||
public void addStyle(DanceGroupStyle style) {
|
||||
super.addNullSafe(styles, style);
|
||||
}
|
||||
|
||||
public void removeDancer(Dancer dancer) {
|
||||
dancers.remove(dancer);
|
||||
}
|
||||
|
||||
public void replaceStyles(Set<DanceGroupStyle> styles) {
|
||||
this.styles.clear();
|
||||
this.styles.addAll(styles);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,19 +2,24 @@ package nl.agilecc.agiledancers.model;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.LinkedHashSet;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@Builder
|
||||
@ToString(onlyExplicitlyIncluded = true)
|
||||
public class DanceSchool extends AbstractEntity {
|
||||
private final String name;
|
||||
private final Set<Dancer> dancers;
|
||||
private final Set<DanceGroup> danceGroups;
|
||||
private final Set<Show> shows;
|
||||
private final Set<Coach> coaches;
|
||||
@Setter
|
||||
private String name;
|
||||
@Builder.Default
|
||||
private final Set<Dancer> dancers = new LinkedHashSet<>();
|
||||
@Builder.Default
|
||||
private final Set<DanceGroup> danceGroups = new LinkedHashSet<>();
|
||||
@Builder.Default
|
||||
private final Set<Show> shows = new LinkedHashSet<>();
|
||||
@Builder.Default
|
||||
private final Set<Coach> coaches = new LinkedHashSet<>();
|
||||
|
||||
public Set<Dancer> getDancers() {
|
||||
return super.unmodifiableNullSafe(dancers);
|
||||
@@ -47,4 +52,16 @@ public class DanceSchool extends AbstractEntity {
|
||||
public void addCoach(Coach coach) {
|
||||
super.addNullSafe(coaches, coach);
|
||||
}
|
||||
|
||||
public void removeDancer(Dancer entity) {
|
||||
dancers.remove(entity);
|
||||
}
|
||||
|
||||
public void removeDanceGroup(DanceGroup entity) {
|
||||
danceGroups.remove(entity);
|
||||
}
|
||||
|
||||
public void removeShow(Show entity) {
|
||||
shows.remove(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,10 @@ import java.time.Period;
|
||||
@ToString(onlyExplicitlyIncluded = true)
|
||||
public class Dancer extends AbstractEntity {
|
||||
@ToString.Include
|
||||
private final String name;
|
||||
@Setter
|
||||
private String name;
|
||||
@ToString.Include
|
||||
@Setter
|
||||
private LocalDate birthDate;
|
||||
|
||||
public int age() {
|
||||
|
||||
@@ -3,12 +3,17 @@ package nl.agilecc.agiledancers.model;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@Builder
|
||||
@ToString(onlyExplicitlyIncluded = true)
|
||||
public class Show extends AbstractEntity {
|
||||
@Setter
|
||||
private String name;
|
||||
@Setter
|
||||
private LocalDate date;
|
||||
@ToString.Include
|
||||
private final List<DayPart> dayParts;
|
||||
}
|
||||
|
||||
@@ -24,4 +24,7 @@ public abstract class AbstractRepository<T extends AbstractEntity> {
|
||||
public T findById(long id) {
|
||||
return entities.get(id);
|
||||
}
|
||||
public void delete(long id) {
|
||||
entities.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package nl.agilecc.agiledancers.rest;
|
||||
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.*;
|
||||
import nl.agilecc.agiledancers.service.SchoolManagementService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import nl.agilecc.agiledancers.rest.apimodel.GroupRequest;
|
||||
import nl.agilecc.agiledancers.rest.apimodel.GroupView;
|
||||
|
||||
@Path("/api/dance-schools/{schoolId}/dance-groups")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class DanceGroupResource {
|
||||
@Inject
|
||||
SchoolManagementService service;
|
||||
|
||||
@PathParam("schoolId")
|
||||
long schoolId;
|
||||
|
||||
@GET
|
||||
public List<GroupView> list() {
|
||||
return service.groups(schoolId).stream().map(GroupView::of).toList();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{id}")
|
||||
public GroupView get(@PathParam("id") long id) {
|
||||
return GroupView.of(service.group(schoolId, id));
|
||||
}
|
||||
|
||||
@POST
|
||||
public Response create(@NotNull @Valid GroupRequest request, @Context UriInfo uriInfo) {
|
||||
var entity = service.createGroup(schoolId, request.category(), request.type(), request.startAge(), request.endAge(), request.styles());
|
||||
return Response.created(uriInfo.getAbsolutePathBuilder().path(entity.getId().toString()).build())
|
||||
.entity(GroupView.of(entity)).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{id}")
|
||||
public GroupView update(@PathParam("id") long id, @NotNull @Valid GroupRequest request) {
|
||||
return GroupView.of(service.updateGroup(schoolId, id, request.category(), request.type(), request.startAge(), request.endAge(), request.styles()));
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/{id}")
|
||||
public Response delete(@PathParam("id") long id) {
|
||||
service.deleteGroup(schoolId, id);
|
||||
return Response.noContent().build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{id}/dancers/{dancerId}")
|
||||
public Response addDancer(@PathParam("id") long id, @PathParam("dancerId") long dancerId) {
|
||||
service.addGroupDancer(schoolId, id, dancerId);
|
||||
return Response.noContent().build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/{id}/dancers/{dancerId}")
|
||||
public Response removeDancer(@PathParam("id") long id, @PathParam("dancerId") long dancerId) {
|
||||
service.removeGroupDancer(schoolId, id, dancerId);
|
||||
return Response.noContent().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package nl.agilecc.agiledancers.rest;
|
||||
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.*;
|
||||
import nl.agilecc.agiledancers.service.SchoolManagementService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import nl.agilecc.agiledancers.rest.apimodel.SchoolRequest;
|
||||
import nl.agilecc.agiledancers.rest.apimodel.SchoolView;
|
||||
|
||||
@Path("/api/dance-schools")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class DanceSchoolResource {
|
||||
@Inject
|
||||
SchoolManagementService service;
|
||||
|
||||
@GET
|
||||
public List<SchoolView> list() {
|
||||
return service.schools().stream().map(SchoolView::of).toList();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{id}")
|
||||
public SchoolView get(@PathParam("id") long id) {
|
||||
return SchoolView.of(service.school(id));
|
||||
}
|
||||
|
||||
@POST
|
||||
public Response create(@NotNull @Valid SchoolRequest request, @Context UriInfo uriInfo) {
|
||||
var entity = service.createSchool(request.name());
|
||||
return Response.created(uriInfo.getAbsolutePathBuilder().path(entity.getId().toString()).build())
|
||||
.entity(SchoolView.of(entity)).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{id}")
|
||||
public SchoolView update(@PathParam("id") long id, @NotNull @Valid SchoolRequest request) {
|
||||
return SchoolView.of(service.updateSchool(id, request.name()));
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/{id}")
|
||||
public Response delete(@PathParam("id") long id) {
|
||||
service.deleteSchool(id);
|
||||
return Response.noContent().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package nl.agilecc.agiledancers.rest;
|
||||
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.*;
|
||||
import nl.agilecc.agiledancers.service.SchoolManagementService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import nl.agilecc.agiledancers.rest.apimodel.DancerRequest;
|
||||
import nl.agilecc.agiledancers.rest.apimodel.DancerView;
|
||||
|
||||
@Path("/api/dance-schools/{schoolId}/dancers")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class DancerResource {
|
||||
@Inject
|
||||
SchoolManagementService service;
|
||||
|
||||
@PathParam("schoolId")
|
||||
long schoolId;
|
||||
|
||||
@GET
|
||||
public List<DancerView> list() {
|
||||
return service.dancers(schoolId).stream().map(DancerView::of).toList();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{id}")
|
||||
public DancerView get(@PathParam("id") long id) {
|
||||
return DancerView.of(service.dancer(schoolId, id));
|
||||
}
|
||||
|
||||
@POST
|
||||
public Response create(@NotNull @Valid DancerRequest request, @Context UriInfo uriInfo) {
|
||||
var entity = service.createDancer(schoolId, request.name(), request.birthDate());
|
||||
return Response.created(uriInfo.getAbsolutePathBuilder().path(entity.getId().toString()).build())
|
||||
.entity(DancerView.of(entity)).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{id}")
|
||||
public DancerView update(@PathParam("id") long id, @NotNull @Valid DancerRequest request) {
|
||||
return DancerView.of(service.updateDancer(schoolId, id, request.name(), request.birthDate()));
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/{id}")
|
||||
public Response delete(@PathParam("id") long id) {
|
||||
service.deleteDancer(schoolId, id);
|
||||
return Response.noContent().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package nl.agilecc.agiledancers.rest;
|
||||
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.ext.ExceptionMapper;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
import nl.agilecc.agiledancers.service.ManagementException;
|
||||
import nl.agilecc.agiledancers.rest.apimodel.ErrorView;
|
||||
|
||||
@Provider
|
||||
public class ManagementExceptionMapper implements ExceptionMapper<ManagementException> {
|
||||
@Override
|
||||
public Response toResponse(ManagementException exception) {
|
||||
int status = switch (exception.getKind()) {
|
||||
case NOT_FOUND -> 404;
|
||||
case INVALID_INPUT -> 400;
|
||||
};
|
||||
return Response.status(status).type(MediaType.APPLICATION_JSON_TYPE)
|
||||
.entity(new ErrorView(exception.getMessage())).build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package nl.agilecc.agiledancers.rest;
|
||||
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.*;
|
||||
import nl.agilecc.agiledancers.service.SchoolManagementService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import nl.agilecc.agiledancers.rest.apimodel.ShowRequest;
|
||||
import nl.agilecc.agiledancers.rest.apimodel.ShowView;
|
||||
|
||||
@Path("/api/dance-schools/{schoolId}/shows")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class ShowResource {
|
||||
@Inject
|
||||
SchoolManagementService service;
|
||||
|
||||
@PathParam("schoolId")
|
||||
long schoolId;
|
||||
|
||||
@GET
|
||||
public List<ShowView> list() {
|
||||
return service.shows(schoolId).stream().map(ShowView::of).toList();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{id}")
|
||||
public ShowView get(@PathParam("id") long id) {
|
||||
return ShowView.of(service.show(schoolId, id));
|
||||
}
|
||||
|
||||
@POST
|
||||
public Response create(@NotNull @Valid ShowRequest request, @Context UriInfo uriInfo) {
|
||||
var entity = service.createShow(schoolId, request.name(), request.date());
|
||||
return Response.created(uriInfo.getAbsolutePathBuilder().path(entity.getId().toString()).build())
|
||||
.entity(ShowView.of(entity)).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{id}")
|
||||
public ShowView update(@PathParam("id") long id, @NotNull @Valid ShowRequest request) {
|
||||
return ShowView.of(service.updateShow(schoolId, id, request.name(), request.date()));
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/{id}")
|
||||
public Response delete(@PathParam("id") long id) {
|
||||
service.deleteShow(schoolId, id);
|
||||
return Response.noContent().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package nl.agilecc.agiledancers.rest.apimodel;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.PastOrPresent;
|
||||
import java.time.LocalDate;
|
||||
|
||||
public record DancerRequest(@NotBlank String name, @NotNull @PastOrPresent LocalDate birthDate) {}
|
||||
@@ -0,0 +1,10 @@
|
||||
package nl.agilecc.agiledancers.rest.apimodel;
|
||||
|
||||
import nl.agilecc.agiledancers.model.Dancer;
|
||||
import java.time.LocalDate;
|
||||
|
||||
public record DancerView(long id, String name, LocalDate birthDate) {
|
||||
public static DancerView of(Dancer dancer) {
|
||||
return new DancerView(dancer.getId(), dancer.getName(), dancer.getBirthDate());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package nl.agilecc.agiledancers.rest.apimodel;
|
||||
|
||||
import nl.agilecc.agiledancers.model.specifications.DayPartType;
|
||||
import java.util.List;
|
||||
|
||||
public record DayPartView(DayPartType type, List<Long> danceGroupIds) {}
|
||||
@@ -0,0 +1,6 @@
|
||||
package nl.agilecc.agiledancers.rest.apimodel;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public record ErrorView(String message) {}
|
||||
@@ -0,0 +1,12 @@
|
||||
package nl.agilecc.agiledancers.rest.apimodel;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import nl.agilecc.agiledancers.model.specifications.DanceGroupType;
|
||||
import nl.agilecc.agiledancers.model.specifications.DanceGroupStyle;
|
||||
import java.util.Set;
|
||||
|
||||
public record GroupRequest(@NotBlank String category, @NotNull DanceGroupType type,
|
||||
@NotNull @Min(0) Integer startAge, @NotNull @Min(0) Integer endAge,
|
||||
@NotNull Set<@NotNull DanceGroupStyle> styles) {}
|
||||
@@ -0,0 +1,17 @@
|
||||
package nl.agilecc.agiledancers.rest.apimodel;
|
||||
|
||||
import nl.agilecc.agiledancers.model.DanceGroup;
|
||||
import nl.agilecc.agiledancers.model.Dancer;
|
||||
import nl.agilecc.agiledancers.model.specifications.DanceGroupType;
|
||||
import nl.agilecc.agiledancers.model.specifications.DanceGroupStyle;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public record GroupView(long id, String category, DanceGroupType type, int startAge, int endAge,
|
||||
Set<DanceGroupStyle> styles, List<Long> dancerIds) {
|
||||
public static GroupView of(DanceGroup group) {
|
||||
return new GroupView(group.getId(), group.getCategory(), group.getType(), group.getStartAge(),
|
||||
group.getEndAge(), Set.copyOf(group.getStyles()),
|
||||
group.getDancers().stream().map(Dancer::getId).toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package nl.agilecc.agiledancers.rest.apimodel;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public record SchoolRequest(@NotBlank String name) {}
|
||||
@@ -0,0 +1,17 @@
|
||||
package nl.agilecc.agiledancers.rest.apimodel;
|
||||
|
||||
import nl.agilecc.agiledancers.model.DanceSchool;
|
||||
import nl.agilecc.agiledancers.model.Dancer;
|
||||
import nl.agilecc.agiledancers.model.DanceGroup;
|
||||
import nl.agilecc.agiledancers.model.Show;
|
||||
import java.util.List;
|
||||
|
||||
public record SchoolView(long id, String name, List<Long> dancerIds,
|
||||
List<Long> danceGroupIds, List<Long> showIds) {
|
||||
public static SchoolView of(DanceSchool school) {
|
||||
return new SchoolView(school.getId(), school.getName(),
|
||||
school.getDancers().stream().map(Dancer::getId).toList(),
|
||||
school.getDanceGroups().stream().map(DanceGroup::getId).toList(),
|
||||
school.getShows().stream().map(Show::getId).toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package nl.agilecc.agiledancers.rest.apimodel;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import java.time.LocalDate;
|
||||
|
||||
public record ShowRequest(@NotBlank String name, @NotNull LocalDate date) {}
|
||||
@@ -0,0 +1,14 @@
|
||||
package nl.agilecc.agiledancers.rest.apimodel;
|
||||
|
||||
import nl.agilecc.agiledancers.model.Show;
|
||||
import nl.agilecc.agiledancers.model.DanceGroup;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
public record ShowView(long id, String name, LocalDate date, List<DayPartView> dayParts) {
|
||||
public static ShowView of(Show show) {
|
||||
return new ShowView(show.getId(), show.getName(), show.getDate(), show.getDayParts().stream()
|
||||
.map(dayPart -> new DayPartView(dayPart.getType(), dayPart.getDanceGroups().stream()
|
||||
.map(DanceGroup::getId).toList())).toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package nl.agilecc.agiledancers.service;
|
||||
|
||||
public class ManagementException extends RuntimeException {
|
||||
public enum Kind { NOT_FOUND, INVALID_INPUT }
|
||||
|
||||
private final Kind kind;
|
||||
|
||||
public ManagementException(Kind kind, String message) {
|
||||
super(message);
|
||||
this.kind = kind;
|
||||
}
|
||||
|
||||
public Kind getKind() {
|
||||
return kind;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package nl.agilecc.agiledancers.service;
|
||||
|
||||
import jakarta.inject.Singleton;
|
||||
import nl.agilecc.agiledancers.model.*;
|
||||
import nl.agilecc.agiledancers.model.specifications.*;
|
||||
import nl.agilecc.agiledancers.repository.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
|
||||
import static nl.agilecc.agiledancers.service.ManagementException.Kind.*;
|
||||
|
||||
/** Owns the in-memory school aggregates; all writes go through this shared service. */
|
||||
@Singleton
|
||||
public class SchoolManagementService {
|
||||
private final DanceSchoolRepository schools = new DanceSchoolRepository();
|
||||
private final DancerRepository dancers = new DancerRepository();
|
||||
private final DanceGroupRepository groups = new DanceGroupRepository();
|
||||
private final ShowRepository shows = new ShowRepository();
|
||||
|
||||
public synchronized List<DanceSchool> schools() {
|
||||
return schools.findAll();
|
||||
}
|
||||
|
||||
public synchronized DanceSchool school(long schoolId) {
|
||||
return required(schools.findById(schoolId), "Dance school", schoolId);
|
||||
}
|
||||
|
||||
public synchronized DanceSchool createSchool(String name) {
|
||||
return schools.add(DanceSchool.builder().name(name).build());
|
||||
}
|
||||
|
||||
public synchronized DanceSchool updateSchool(long schoolId, String name) {
|
||||
DanceSchool school = school(schoolId);
|
||||
school.setName(name);
|
||||
return school;
|
||||
}
|
||||
|
||||
public synchronized void deleteSchool(long schoolId) {
|
||||
DanceSchool school = school(schoolId);
|
||||
school.getDanceGroups().forEach(group -> groups.delete(group.getId()));
|
||||
school.getDancers().forEach(dancer -> dancers.delete(dancer.getId()));
|
||||
school.getShows().forEach(show -> shows.delete(show.getId()));
|
||||
schools.delete(schoolId);
|
||||
}
|
||||
|
||||
public synchronized List<Dancer> dancers(long schoolId) {
|
||||
return List.copyOf(school(schoolId).getDancers());
|
||||
}
|
||||
|
||||
public synchronized Dancer dancer(long schoolId, long dancerId) {
|
||||
return member(school(schoolId).getDancers(), dancerId, "Dancer");
|
||||
}
|
||||
|
||||
public synchronized Dancer createDancer(long schoolId, String name, LocalDate birthDate) {
|
||||
DanceSchool school = school(schoolId);
|
||||
Dancer dancer = dancers.add(Dancer.builder().name(name).birthDate(birthDate).build());
|
||||
school.addDancer(dancer);
|
||||
return dancer;
|
||||
}
|
||||
|
||||
public synchronized Dancer updateDancer(long schoolId, long dancerId, String name, LocalDate birthDate) {
|
||||
Dancer dancer = dancer(schoolId, dancerId);
|
||||
dancer.setName(name);
|
||||
dancer.setBirthDate(birthDate);
|
||||
return dancer;
|
||||
}
|
||||
|
||||
public synchronized void deleteDancer(long schoolId, long dancerId) {
|
||||
DanceSchool school = school(schoolId);
|
||||
Dancer dancer = dancer(schoolId, dancerId);
|
||||
school.getDanceGroups().forEach(group -> group.removeDancer(dancer));
|
||||
school.removeDancer(dancer);
|
||||
dancers.delete(dancerId);
|
||||
}
|
||||
|
||||
public synchronized List<DanceGroup> groups(long schoolId) {
|
||||
return List.copyOf(school(schoolId).getDanceGroups());
|
||||
}
|
||||
|
||||
public synchronized DanceGroup group(long schoolId, long groupId) {
|
||||
return member(school(schoolId).getDanceGroups(), groupId, "Dance group");
|
||||
}
|
||||
|
||||
public synchronized DanceGroup createGroup(long schoolId, String category, DanceGroupType type,
|
||||
int startAge, int endAge, Set<DanceGroupStyle> styles) {
|
||||
DanceSchool school = school(schoolId);
|
||||
validateAgeRange(startAge, endAge);
|
||||
DanceGroup group = groups.add(DanceGroup.builder().category(category).type(type)
|
||||
.startAge(startAge).endAge(endAge).styles(new LinkedHashSet<>(styles)).build());
|
||||
school.addDanceGroup(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
public synchronized DanceGroup updateGroup(long schoolId, long groupId, String category, DanceGroupType type,
|
||||
int startAge, int endAge, Set<DanceGroupStyle> styles) {
|
||||
DanceGroup group = group(schoolId, groupId);
|
||||
validateAgeRange(startAge, endAge);
|
||||
group.setCategory(category);
|
||||
group.setType(type);
|
||||
group.setStartAge(startAge);
|
||||
group.setEndAge(endAge);
|
||||
group.replaceStyles(styles);
|
||||
return group;
|
||||
}
|
||||
|
||||
public synchronized void deleteGroup(long schoolId, long groupId) {
|
||||
DanceGroup group = group(schoolId, groupId);
|
||||
school(schoolId).removeDanceGroup(group);
|
||||
groups.delete(groupId);
|
||||
}
|
||||
|
||||
public synchronized void addGroupDancer(long schoolId, long groupId, long dancerId) {
|
||||
DanceGroup group = group(schoolId, groupId);
|
||||
group.addDancer(dancer(schoolId, dancerId));
|
||||
}
|
||||
|
||||
public synchronized void removeGroupDancer(long schoolId, long groupId, long dancerId) {
|
||||
DanceGroup group = group(schoolId, groupId);
|
||||
group.removeDancer(dancer(schoolId, dancerId));
|
||||
}
|
||||
|
||||
public synchronized List<Show> shows(long schoolId) {
|
||||
return List.copyOf(school(schoolId).getShows());
|
||||
}
|
||||
|
||||
public synchronized Show show(long schoolId, long showId) {
|
||||
return member(school(schoolId).getShows(), showId, "Show");
|
||||
}
|
||||
|
||||
public synchronized Show createShow(long schoolId, String name, LocalDate date) {
|
||||
DanceSchool school = school(schoolId);
|
||||
List<DayPart> dayParts = Arrays.stream(DayPartType.values())
|
||||
.map(type -> DayPart.builder().type(type).danceGroups(List.of()).build()).toList();
|
||||
Show show = shows.add(Show.builder().name(name).date(date).dayParts(dayParts).build());
|
||||
school.addShow(show);
|
||||
return show;
|
||||
}
|
||||
|
||||
public synchronized Show updateShow(long schoolId, long showId, String name, LocalDate date) {
|
||||
Show show = show(schoolId, showId);
|
||||
show.setName(name);
|
||||
show.setDate(date);
|
||||
return show;
|
||||
}
|
||||
|
||||
public synchronized void deleteShow(long schoolId, long showId) {
|
||||
Show show = show(schoolId, showId);
|
||||
school(schoolId).removeShow(show);
|
||||
shows.delete(showId);
|
||||
}
|
||||
|
||||
private static void validateAgeRange(int startAge, int endAge) {
|
||||
if (startAge > endAge) {
|
||||
throw new ManagementException(INVALID_INPUT, "startAge must not exceed endAge");
|
||||
}
|
||||
}
|
||||
|
||||
private static <T extends AbstractEntity> T member(Collection<T> entities, long id, String type) {
|
||||
return entities.stream().filter(entity -> entity.getId() == id).findFirst()
|
||||
.orElseThrow(() -> new ManagementException(NOT_FOUND, type + " " + id + " not found in this school"));
|
||||
}
|
||||
|
||||
private static <T> T required(T value, String type, long id) {
|
||||
if (value == null) {
|
||||
throw new ManagementException(NOT_FOUND, type + " " + id + " not found");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user