Skip to content

Commit

Permalink
enhancement(app): cleaned up the code of app resource and services
Browse files Browse the repository at this point in the history
  • Loading branch information
zZHorizonZz committed Aug 4, 2023
1 parent ca69d0c commit 6bfba18
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.shiperist.exception;

import lombok.Getter;

@Getter
public class ErrorMessageException extends RuntimeException{

private final ErrorMessage errorMessage;

public ErrorMessageException(ErrorMessage errorMessage) {
this.errorMessage = errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.shiperist.exception;

import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

@Provider
public class ErrorMessageHandler implements ExceptionMapper<ErrorMessageException> {
@Override
public Response toResponse(ErrorMessageException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getErrorMessage()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ public class HibernateExceptionHandler implements ExceptionMapper<ConstraintViol

@Override
public Response toResponse(ConstraintViolationException e) {
if (e.getConstraintName().contains("email")) {
return Response.status(Response.Status.BAD_REQUEST).entity(ErrorMessage.EMAIL_ALREADY_EXISTS).build();
}

if (e.getConstraintName().contains("project_name_key")) {
return Response.status(Response.Status.BAD_REQUEST).entity(ErrorMessage.PROJECT_ALREADY_EXISTS).build();
}

return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return Response.status(Response.Status.BAD_REQUEST).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;

import java.util.List;
import java.util.Objects;

@WithSession
@ApplicationScoped
public class ProjectAppRepository implements PanacheRepositoryBase<ProjectAppEntity, Long> {

public Uni<ProjectAppEntity> findByName(String name) {
return find("name", name).firstResult();
}

public Uni<List<ProjectAppEntity>> findByProject(Long projectId) {
return find("project.id", projectId).list();
}

public Uni<Boolean> hasPermission(Long appId, Long userId) {
return find("id = ?1 and project.projectMembers.user.id = ?2", appId, userId).firstResult().map(Objects::nonNull);
}

public Uni<ProjectAppEntity> getIfMember(Long appId, Long userId) {
return find("select p from ProjectApp p join p.project.projectMembers m where p.id = ?1 and m.user.id = ?2", appId, userId).firstResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;

import java.util.Objects;

@WithSession
@ApplicationScoped
public class ProjectRepository implements PanacheRepositoryBase<ProjectEntity, Long> {

public Uni<ProjectEntity> findByName(String name) {
return find("name", name).firstResult();
}

public Uni<Boolean> hasPermission(Long projectId, Long userId) {
return find("id = ?1 and projectMembers.user.id = ?2", projectId, userId).firstResult().map(Objects::nonNull);
}

public Uni<ProjectEntity> getIfMember(Long projectId, Long userId) {
return find("select p from Project p join p.projectMembers m where p.id = ?1 and m.user.id = ?2", projectId, userId).firstResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,8 @@ public class ProjectAppResource extends BaseProjectResource {
content = @Content(schema = @Schema(implementation = ProjectApp.class))
)
public Uni<Response> createProjectApp(@Parameter(description = "Project ID") @PathParam("projectId") Long projectId, ProjectApp app) {
return ifMember(Long.parseLong(sub), projectId, isMember -> {
if (isMember) {
return projectAppService.doesProjectAppExist(app.getName()).flatMap(exists -> {
if (!exists) {
return projectAppService.createProjectApp(projectId, app.getName(), app.getDisplayName(), app.getDescription(), app.getImage(), app.getOs(), app.getReleaseType())
.onItem().ifNotNull().transform(projectApp -> Response.status(Response.Status.CREATED).entity(projectApp).build());
} else {
return Uni.createFrom().item(Response.status(Response.Status.BAD_REQUEST).entity(ErrorMessage.PROJECT_APP_ALREADY_EXISTS).build());
}
});
} else {
return Uni.createFrom().item(Response.status(Response.Status.FORBIDDEN).entity(ErrorMessage.PROJECT_NOT_MEMBER).build());
}
});
return projectAppService.createProjectApp(projectId, Long.parseLong(sub), app.getName(), app.getDisplayName(), app.getDescription(), app.getImage(), app.getOs(), app.getReleaseType())
.onItem().ifNotNull().transform(projectApp -> Response.status(Response.Status.CREATED).entity(projectApp).build());
}

@PATCH
Expand All @@ -72,20 +60,8 @@ public Uni<Response> createProjectApp(@Parameter(description = "Project ID") @Pa
content = @Content(schema = @Schema(implementation = ProjectApp.class))
)
public Uni<Response> updateProjectApp(@Parameter(description = "Project App ID") @PathParam("id") Long id, ProjectApp app) {
return ifMember(Long.parseLong(sub), app.getProjectId(), isMember -> {
if (isMember) {
return projectAppService.doesProjectAppExist(app.getName()).flatMap(exists -> {
if (!exists) {
return projectAppService.updateProjectApp(id, app.getName(), app.getDisplayName(), app.getDescription(), app.getImage())
.onItem().ifNotNull().transform(projectApp -> Response.status(Response.Status.OK).entity(projectApp).build());
} else {
return Uni.createFrom().item(Response.status(Response.Status.BAD_REQUEST).entity(ErrorMessage.PROJECT_APP_ALREADY_EXISTS).build());
}
});
} else {
return Uni.createFrom().item(Response.status(Response.Status.FORBIDDEN).entity(ErrorMessage.PROJECT_NOT_MEMBER).build());
}
});
return projectAppService.updateProjectApp(id, Long.parseLong(sub), app.getName(), app.getDisplayName(), app.getDescription(), app.getImage())
.onItem().ifNotNull().transform(projectApp -> Response.status(Response.Status.OK).entity(projectApp).build());
}

@GET
Expand All @@ -97,9 +73,8 @@ public Uni<Response> updateProjectApp(@Parameter(description = "Project App ID")
content = @Content(schema = @Schema(implementation = ProjectApp.class))
)
public Uni<Response> getProjectApp(@Parameter(description = "Project App ID") @PathParam("id") Long id) {
return ifMember(Long.parseLong(sub), id, isMember ->
projectAppService.getProjectApp(id)
.onItem().ifNotNull().transform(projectApp -> Response.status(Response.Status.OK).entity(projectApp).build()));
return projectAppService.getProjectApp(id, Long.parseLong(sub))
.onItem().ifNotNull().transform(projectApp -> Response.status(Response.Status.OK).entity(projectApp).build());
}

@GET
Expand All @@ -110,8 +85,7 @@ public Uni<Response> getProjectApp(@Parameter(description = "Project App ID") @P
content = @Content(schema = @Schema(implementation = ProjectApp.class))
)
public Uni<Response> getProjectApps(@Parameter(description = "Project ID") @PathParam("projectId") Long projectId) {
return ifMember(Long.parseLong(sub), projectId, isMember ->
projectAppService.getProjectApps(projectId)
.onItem().ifNotNull().transform(projectApps -> Response.status(Response.Status.OK).entity(projectApps).build()));
return projectAppService.getProjectApps(projectId, Long.parseLong(sub))
.onItem().ifNotNull().transform(projectApps -> Response.status(Response.Status.OK).entity(projectApps).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import dev.shiperist.data.OsType;
import dev.shiperist.data.ReleaseType;
import dev.shiperist.entity.project.ProjectAppEntity;
import dev.shiperist.entity.project.ProjectEntity;
import dev.shiperist.exception.ErrorMessage;
import dev.shiperist.exception.ErrorMessageException;
import dev.shiperist.mapper.project.ProjectAppMapper;
import dev.shiperist.model.project.ProjectApp;
import dev.shiperist.repository.project.ProjectAppRepository;
import dev.shiperist.repository.project.ProjectRepository;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.NotFoundException;

import java.util.List;
import java.util.Objects;

@ApplicationScoped
public class ProjectAppService {
Expand All @@ -27,51 +30,64 @@ public class ProjectAppService {
@Inject
ProjectAppMapper projectAppMapper;

public Uni<ProjectApp> createProjectApp(Long projectId, String name, String displayName, String description, String image, OsType os, ReleaseType releaseType) {
return projectService.findById(projectId)
.map(project -> {
String appName = project.getName() + "/" + name;
ProjectAppEntity projectApp = new ProjectAppEntity();
projectApp.setName(appName);
projectApp.setDisplayName(displayName);
projectApp.setDescription(description);
projectApp.setImage(image);
projectApp.setOs(os);
projectApp.setReleaseType(releaseType);
projectApp.setProject(project);
return projectApp;
})
.onItem().ifNotNull().transformToUni(projectAppRepository::persistAndFlush)
public Uni<ProjectApp> createProjectApp(Long projectId, Long userId, String name, String displayName, String description, String image, OsType os, ReleaseType releaseType) {
return projectService.getIfMember(projectId, userId)
.onItem().ifNull().failWith(new ErrorMessageException(ErrorMessage.PROJECT_NOT_FOUND))
.map(project -> buildProjectAppEntity(project, name, displayName, description, image, os, releaseType))
.onItem().ifNotNull().transformToUni(this::persistIfNotExists)
.onItem().ifNotNull().transform(projectAppMapper::toDomain);
}

public Uni<ProjectApp> updateProjectApp(Long id, String name, String displayName, String description, String image) {
return projectAppRepository.findById(id)
.map(projectApp -> {
String appName = projectApp.getProject().getName() + "/" + name;
projectApp.setName(appName);
projectApp.setDisplayName(displayName);
projectApp.setDescription(description);
projectApp.setImage(image);
return projectApp;
})
.onItem().ifNotNull().transformToUni(projectAppRepository::persistAndFlush)
public Uni<ProjectApp> updateProjectApp(Long id, Long userId, String name, String displayName, String description, String image) {
return projectAppRepository.getIfMember(id, userId)
.onItem().ifNull().failWith(new ErrorMessageException(ErrorMessage.PROJECT_APP_NOT_FOUND))
.map(projectApp -> updateProjectAppEntity(projectApp, name, displayName, description, image))
.onItem().ifNotNull().transformToUni(this::persistIfNotExists)
.onItem().ifNotNull().transform(projectAppMapper::toDomain);
}

public Uni<ProjectApp> getProjectApp(Long id) {
return projectAppRepository.findById(id).map(projectAppMapper::toDomain);
public Uni<ProjectApp> getProjectApp(Long id, Long userId) {
return projectAppRepository.getIfMember(id, userId)
.onItem().ifNull().failWith(new ErrorMessageException(ErrorMessage.PROJECT_APP_NOT_FOUND))
.map(projectAppMapper::toDomain);
}

public Uni<List<ProjectApp>> getProjectApps(Long projectId) {
return projectAppRepository.list("projectId", projectId).map(projectAppMapper::toDomainList);
public Uni<List<ProjectApp>> getProjectApps(Long projectId, Long userId) {
return projectService.getIfMember(projectId, userId)
.onItem().ifNull().failWith(new ErrorMessageException(ErrorMessage.PROJECT_NOT_FOUND))
.onItem().ifNotNull().transformToUni(project -> projectAppRepository.findByProject(projectId))
.onItem().ifNotNull().transform(projectAppMapper::toDomainList);
}

public Uni<Boolean> doesProjectAppExist(String name) {
return projectAppRepository.findByName(name).map(Objects::nonNull);
private Uni<ProjectAppEntity> persistIfNotExists(ProjectAppEntity projectApp) {
return projectAppRepository.findByName(projectApp.getName())
.onItem().ifNotNull().failWith(new ErrorMessageException(ErrorMessage.PROJECT_APP_ALREADY_EXISTS))
.onItem().ifNull().switchTo(() -> projectAppRepository.persistAndFlush(projectApp));
}

public Uni<Boolean> deleteProjectApp(Long id) {
return projectAppRepository.deleteById(id);
private ProjectAppEntity buildProjectAppEntity(ProjectEntity project, String name, String displayName, String description, String image, OsType os, ReleaseType releaseType) {
String appName = constructAppName(project.getName(), name);
ProjectAppEntity projectApp = new ProjectAppEntity();
projectApp.setName(appName);
projectApp.setDisplayName(displayName);
projectApp.setDescription(description);
projectApp.setImage(image);
projectApp.setOs(os);
projectApp.setReleaseType(releaseType);
projectApp.setProject(project);
return projectApp;
}

private ProjectAppEntity updateProjectAppEntity(ProjectAppEntity projectApp, String name, String displayName, String description, String image) {
String appName = constructAppName(projectApp.getProject().getName(), name);
projectApp.setName(appName);
projectApp.setDisplayName(displayName);
projectApp.setDescription(description);
projectApp.setImage(image);
return projectApp;
}

private String constructAppName(String projectName, String appName) {
return projectName + "/" + appName;
}
}

0 comments on commit 6bfba18

Please sign in to comment.