Skip to content
Snippets Groups Projects
Commit 60e889f3 authored by Sven Kästle's avatar Sven Kästle
Browse files

refactor: No need for gson and refactored the annotation REST paths

parent c2645d9c
No related branches found
No related tags found
No related merge requests found
...@@ -147,14 +147,7 @@ ...@@ -147,14 +147,7 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- gson - converts Java Objects into their JSON representation --> <!-- dropwizard - Need this for '@PATCH' annotation -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!-- dropwizard - -->
<dependency> <dependency>
<groupId>io.dropwizard</groupId> <groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId> <artifactId>dropwizard-core</artifactId>
......
package unipotsdam.gf.modules.annotation.model; package unipotsdam.gf.modules.annotation.model;
/**
* @author Sven Kästle
* skaestle@uni-potsdam.de
*/
public class AnnotationPatchRequest { public class AnnotationPatchRequest {
// variables // variables
......
package unipotsdam.gf.modules.annotation.model;
/**
* @author Sven Kästle
* skaestle@uni-potsdam.de
*/
public class AnnotationResponse {
// variables
String message;
// constructors
public AnnotationResponse(String message) {
this.message = message;
}
public AnnotationResponse() {
}
// methods
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "AnnotationResponse{" +
"message='" + message + '\'' +
'}';
}
}
package unipotsdam.gf.modules.annotation.view; package unipotsdam.gf.modules.annotation.view;
import com.google.gson.Gson;
import io.dropwizard.jersey.PATCH; import io.dropwizard.jersey.PATCH;
import unipotsdam.gf.modules.annotation.controller.AnnotationController; import unipotsdam.gf.modules.annotation.controller.AnnotationController;
import unipotsdam.gf.modules.annotation.model.Annotation; import unipotsdam.gf.modules.annotation.model.Annotation;
import unipotsdam.gf.modules.annotation.model.AnnotationPatchRequest; import unipotsdam.gf.modules.annotation.model.AnnotationPatchRequest;
import unipotsdam.gf.modules.annotation.model.AnnotationPostRequest; import unipotsdam.gf.modules.annotation.model.AnnotationPostRequest;
import unipotsdam.gf.modules.annotation.model.AnnotationResponse;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
...@@ -28,11 +28,7 @@ public class AnnotationService { ...@@ -28,11 +28,7 @@ public class AnnotationService {
AnnotationController controller = new AnnotationController(); AnnotationController controller = new AnnotationController();
Annotation annotation = controller.addAnnotation(request); Annotation annotation = controller.addAnnotation(request);
// build response return Response.ok(annotation).build();
Gson gson = new Gson();
String response = gson.toJson(annotation);
return Response.status(201).entity(response).build();
} }
...@@ -40,9 +36,8 @@ public class AnnotationService { ...@@ -40,9 +36,8 @@ public class AnnotationService {
@Path("{id}") @Path("{id}")
public Response alterAnnotation(@PathParam("id") String annotationId, AnnotationPatchRequest request) { public Response alterAnnotation(@PathParam("id") String annotationId, AnnotationPatchRequest request) {
// declare response and initialize gson // declare response
String response; AnnotationResponse response = new AnnotationResponse();
Gson gson = new Gson();
// check if annotation exists // check if annotation exists
AnnotationController controller = new AnnotationController(); AnnotationController controller = new AnnotationController();
...@@ -51,15 +46,15 @@ public class AnnotationService { ...@@ -51,15 +46,15 @@ public class AnnotationService {
if (exists) { if (exists) {
// alter annotation and response 200 // alter annotation and response 200
controller.alterAnnotation(annotationId, request); controller.alterAnnotation(annotationId, request);
response = gson.toJson("Altered the annotation with the id " + annotationId); response.setMessage("Altered the annotation with the id " + annotationId);
return Response.status(200).entity(response).build(); return Response.ok(response).build();
} }
else { else {
// no annotation with the given id, response 404 // no annotation with the given id, response 404
response = gson.toJson("Annotation with the id '" + annotationId + "' can't be found"); response.setMessage("Annotation with the id '" + annotationId + "' can't be found");
return Response.status(404).entity(response).build(); return Response.status(Response.Status.NOT_FOUND).entity(response).build();
} }
} }
...@@ -68,9 +63,8 @@ public class AnnotationService { ...@@ -68,9 +63,8 @@ public class AnnotationService {
@Path("{id}") @Path("{id}")
public Response deleteAnnotation(@PathParam("id") String annotationId) { public Response deleteAnnotation(@PathParam("id") String annotationId) {
// declare response and initialize gson // declare response
String response; AnnotationResponse response = new AnnotationResponse();
Gson gson = new Gson();
// check if annotation exists // check if annotation exists
AnnotationController controller = new AnnotationController(); AnnotationController controller = new AnnotationController();
...@@ -79,16 +73,15 @@ public class AnnotationService { ...@@ -79,16 +73,15 @@ public class AnnotationService {
if (exists) { if (exists) {
// delete annotation and response 200 // delete annotation and response 200
controller.deleteAnnotation(annotationId); controller.deleteAnnotation(annotationId);
response.setMessage("Deleted the annotation with the id " + annotationId);
response = gson.toJson("Deleted the annotation with the id " + annotationId); return Response.ok(response).build();
return Response.status(204).entity(response).build();
} }
else { else {
// no annotation with the given id, response 404 // no annotation with the given id, response 404
response = gson.toJson("Annotation with the id '" + annotationId + "' can't be found"); response.setMessage("Annotation with the id '" + annotationId + "' can't be found");
return Response.status(404).entity(response).build();
return Response.status(Response.Status.NOT_FOUND).entity(response).build();
} }
} }
...@@ -97,21 +90,19 @@ public class AnnotationService { ...@@ -97,21 +90,19 @@ public class AnnotationService {
@Path("{id}") @Path("{id}")
public Response getAnnotation(@PathParam("id") String annotationId) { public Response getAnnotation(@PathParam("id") String annotationId) {
// declare response and initialize gson
String response;
Gson gson = new Gson();
// receive the annotation // receive the annotation
AnnotationController controller = new AnnotationController(); AnnotationController controller = new AnnotationController();
Annotation annotation = controller.getAnnotation(annotationId); Annotation annotation = controller.getAnnotation(annotationId);
if (annotation != null) { if (annotation != null) {
response = gson.toJson(annotation); return Response.ok(annotation).build();
return Response.status(200).entity(response).build();
} }
else { else {
response = gson.toJson("Annotation with the id '" + annotationId + "' can't be found"); // declare response
return Response.status(404).entity(response).build(); AnnotationResponse response = new AnnotationResponse();
response.setMessage("Annotation with the id '" + annotationId + "' can't be found");
return Response.status(Response.Status.NOT_FOUND).entity(response).build();
} }
} }
...@@ -120,21 +111,19 @@ public class AnnotationService { ...@@ -120,21 +111,19 @@ public class AnnotationService {
@Path("/target/{id}") @Path("/target/{id}")
public Response getAnnotations(@PathParam("id") int targetId) { public Response getAnnotations(@PathParam("id") int targetId) {
// declare response and initialize gson
String response;
Gson gson = new Gson();
// receive the annotation // receive the annotation
AnnotationController controller = new AnnotationController(); AnnotationController controller = new AnnotationController();
ArrayList<Annotation> annotations = controller.getAnnotations(targetId); ArrayList<Annotation> annotations = controller.getAnnotations(targetId);
if (!annotations.isEmpty()) { if (!annotations.isEmpty()) {
response = gson.toJson(annotations); return Response.ok(annotations).build();
return Response.status(200).entity(response).build();
} }
else { else {
response = gson.toJson("Found no annotations for the target id '" + targetId + "'"); // declare response
return Response.status(404).entity(response).build(); AnnotationResponse response = new AnnotationResponse();
response.setMessage("Found no annotations for the target id '" + targetId + "'");
return Response.status(Response.Status.NOT_FOUND).entity(response).build();
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment