Skip to content

Commit

Permalink
refactor(server): decouple Dropwizard resources from JAX-RS contract …
Browse files Browse the repository at this point in the history
…interfaces
  • Loading branch information
fushar committed Jun 24, 2023
1 parent 1a87c6e commit 188b453
Show file tree
Hide file tree
Showing 40 changed files with 1,488 additions and 1,287 deletions.
Original file line number Diff line number Diff line change
@@ -1,57 +1,71 @@
package judgels.jerahmeel.archive;

import static javax.ws.rs.core.HttpHeaders.AUTHORIZATION;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static judgels.service.ServiceUtils.checkAllowed;
import static judgels.service.ServiceUtils.checkFound;

import io.dropwizard.hibernate.UnitOfWork;
import java.util.Optional;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import judgels.jerahmeel.api.archive.Archive;
import judgels.jerahmeel.api.archive.ArchiveCreateData;
import judgels.jerahmeel.api.archive.ArchiveService;
import judgels.jerahmeel.api.archive.ArchiveUpdateData;
import judgels.jerahmeel.api.archive.ArchivesResponse;
import judgels.jerahmeel.role.RoleChecker;
import judgels.service.actor.ActorChecker;
import judgels.service.api.actor.AuthHeader;

public class ArchiveResource implements ArchiveService {
private final ActorChecker actorChecker;
private final RoleChecker roleChecker;
private final ArchiveStore archiveStore;

@Inject
public ArchiveResource(
ActorChecker actorChecker,
RoleChecker roleChecker,
ArchiveStore archiveStore) {
this.actorChecker = actorChecker;
this.roleChecker = roleChecker;
this.archiveStore = archiveStore;
}
@Path("/api/v2/archives")
public class ArchiveResource {
@Inject protected ActorChecker actorChecker;
@Inject protected RoleChecker roleChecker;
@Inject protected ArchiveStore archiveStore;

@Inject public ArchiveResource() {}

@Override
@GET
@Produces(APPLICATION_JSON)
@UnitOfWork(readOnly = true)
public ArchivesResponse getArchives(Optional<AuthHeader> authHeader) {
public ArchivesResponse getArchives(@HeaderParam(AUTHORIZATION) Optional<AuthHeader> authHeader) {
actorChecker.check(authHeader);

return new ArchivesResponse.Builder()
.data(archiveStore.getArchives())
.build();
}

@Override
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@UnitOfWork
public Archive createArchive(AuthHeader authHeader, ArchiveCreateData data) {
public Archive createArchive(
@HeaderParam(AUTHORIZATION) AuthHeader authHeader,
ArchiveCreateData data) {

String actorJid = actorChecker.check(authHeader);
checkAllowed(roleChecker.isAdmin(actorJid));

return archiveStore.createArchive(data);
}

@Override
@POST
@Path("/{archiveJid}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@UnitOfWork
public Archive updateArchive(AuthHeader authHeader, String archiveJid, ArchiveUpdateData data) {
public Archive updateArchive(
@HeaderParam(AUTHORIZATION) AuthHeader authHeader,
@PathParam("archiveJid") String archiveJid,
ArchiveUpdateData data) {

String actorJid = actorChecker.check(authHeader);
checkFound(archiveStore.getArchiveByJid(archiveJid));
checkAllowed(roleChecker.isAdmin(actorJid));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
package judgels.jerahmeel.chapter;

import static javax.ws.rs.core.HttpHeaders.AUTHORIZATION;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static judgels.service.ServiceUtils.checkAllowed;
import static judgels.service.ServiceUtils.checkFound;

import io.dropwizard.hibernate.UnitOfWork;
import java.util.List;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import judgels.jerahmeel.api.chapter.Chapter;
import judgels.jerahmeel.api.chapter.ChapterCreateData;
import judgels.jerahmeel.api.chapter.ChapterService;
import judgels.jerahmeel.api.chapter.ChapterUpdateData;
import judgels.jerahmeel.api.chapter.ChaptersResponse;
import judgels.jerahmeel.role.RoleChecker;
import judgels.service.actor.ActorChecker;
import judgels.service.api.actor.AuthHeader;

public class ChapterResource implements ChapterService {
private final ActorChecker actorChecker;
private final RoleChecker roleChecker;
private final ChapterStore chapterStore;
@Path("/api/v2/chapters")
public class ChapterResource {
@Inject protected ActorChecker actorChecker;
@Inject protected RoleChecker roleChecker;
@Inject protected ChapterStore chapterStore;

@Inject
public ChapterResource(ActorChecker actorChecker, RoleChecker roleChecker, ChapterStore chapterStore) {
this.actorChecker = actorChecker;
this.roleChecker = roleChecker;
this.chapterStore = chapterStore;
}
@Inject public ChapterResource() {}

@Override
@GET
@Produces(APPLICATION_JSON)
@UnitOfWork(readOnly = true)
public ChaptersResponse getChapters(AuthHeader authHeader) {
public ChaptersResponse getChapters(@HeaderParam(AUTHORIZATION) AuthHeader authHeader) {
String actorJid = actorChecker.check(authHeader);
checkAllowed(roleChecker.isAdmin(actorJid));

Expand All @@ -39,18 +44,30 @@ public ChaptersResponse getChapters(AuthHeader authHeader) {
.build();
}

@Override
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@UnitOfWork
public Chapter createChapter(AuthHeader authHeader, ChapterCreateData data) {
public Chapter createChapter(
@HeaderParam(AUTHORIZATION) AuthHeader authHeader,
ChapterCreateData data) {

String actorJid = actorChecker.check(authHeader);
checkAllowed(roleChecker.isAdmin(actorJid));

return chapterStore.createChapter(data);
}

@Override
@POST
@Path("/{chapterJid}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@UnitOfWork
public Chapter updateChapter(AuthHeader authHeader, String chapterJid, ChapterUpdateData data) {
public Chapter updateChapter(
@HeaderParam(AUTHORIZATION) AuthHeader authHeader,
@PathParam("chapterJid") String chapterJid,
ChapterUpdateData data) {

String actorJid = actorChecker.check(authHeader);
checkFound(chapterStore.getChapterByJid(chapterJid));
checkAllowed(roleChecker.isAdmin(actorJid));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package judgels.jerahmeel.chapter.lesson;

import static com.google.common.base.Preconditions.checkArgument;
import static javax.ws.rs.core.HttpHeaders.AUTHORIZATION;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static judgels.service.ServiceUtils.checkAllowed;
import static judgels.service.ServiceUtils.checkFound;

Expand All @@ -11,10 +13,18 @@
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import judgels.jerahmeel.api.chapter.lesson.ChapterLesson;
import judgels.jerahmeel.api.chapter.lesson.ChapterLessonData;
import judgels.jerahmeel.api.chapter.lesson.ChapterLessonService;
import judgels.jerahmeel.api.chapter.lesson.ChapterLessonStatement;
import judgels.jerahmeel.api.chapter.lesson.ChapterLessonsResponse;
import judgels.jerahmeel.chapter.ChapterStore;
Expand All @@ -25,31 +35,24 @@
import judgels.service.actor.ActorChecker;
import judgels.service.api.actor.AuthHeader;

public class ChapterLessonResource implements ChapterLessonService {
private final ActorChecker actorChecker;
private final RoleChecker roleChecker;
private final ChapterStore chapterStore;
private final ChapterLessonStore lessonStore;
private final LessonClient lessonClient;

@Inject
public ChapterLessonResource(
ActorChecker actorChecker,
RoleChecker roleChecker,
ChapterStore chapterStore,
ChapterLessonStore lessonStore,
LessonClient lessonClient) {

this.actorChecker = actorChecker;
this.roleChecker = roleChecker;
this.chapterStore = chapterStore;
this.lessonStore = lessonStore;
this.lessonClient = lessonClient;
}
@Path("/api/v2/chapters/{chapterJid}/lessons")
public class ChapterLessonResource {
@Inject protected ActorChecker actorChecker;
@Inject protected RoleChecker roleChecker;
@Inject protected ChapterStore chapterStore;
@Inject protected ChapterLessonStore lessonStore;
@Inject protected LessonClient lessonClient;

@Inject public ChapterLessonResource() {}

@Override
@PUT
@Consumes(APPLICATION_JSON)
@UnitOfWork
public void setLessons(AuthHeader authHeader, String chapterJid, List<ChapterLessonData> data) {
public void setLessons(
@HeaderParam(AUTHORIZATION) AuthHeader authHeader,
@PathParam("chapterJid") String chapterJid,
List<ChapterLessonData> data) {

String actorJid = actorChecker.check(authHeader);
checkFound(chapterStore.getChapterByJid(chapterJid));
checkAllowed(roleChecker.isAdmin(actorJid));
Expand All @@ -73,9 +76,13 @@ public void setLessons(AuthHeader authHeader, String chapterJid, List<ChapterLes
lessonStore.setLessons(chapterJid, setData);
}

@Override
@GET
@Produces(APPLICATION_JSON)
@UnitOfWork(readOnly = true)
public ChapterLessonsResponse getLessons(Optional<AuthHeader> authHeader, String chapterJid) {
public ChapterLessonsResponse getLessons(
@HeaderParam(AUTHORIZATION) Optional<AuthHeader> authHeader,
@PathParam("chapterJid") String chapterJid) {

actorChecker.check(authHeader);
checkFound(chapterStore.getChapterByJid(chapterJid));

Expand All @@ -89,14 +96,16 @@ public ChapterLessonsResponse getLessons(Optional<AuthHeader> authHeader, String
.build();
}

@Override
@GET
@Path("/{lessonAlias}/statement")
@Produces(APPLICATION_JSON)
@UnitOfWork(readOnly = true)
public ChapterLessonStatement getLessonStatement(
UriInfo uriInfo,
Optional<AuthHeader> authHeader,
String chapterJid,
String lessonAlias,
Optional<String> language) {
@Context UriInfo uriInfo,
@HeaderParam(AUTHORIZATION) Optional<AuthHeader> authHeader,
@PathParam("chapterJid") String chapterJid,
@PathParam("lessonAlias") String lessonAlias,
@QueryParam("language") Optional<String> language) {

actorChecker.check(authHeader);
checkFound(chapterStore.getChapterByJid(chapterJid));
Expand Down
Loading

0 comments on commit 188b453

Please sign in to comment.