-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Runi-VN/issue#16
Issue#16 - [JOKE] REST-endpoints + testing
- Loading branch information
Showing
5 changed files
with
436 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package rest; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonObject; | ||
import utils.EMF_Creator; | ||
import facades.JokeFacade; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
//Todo Remove or change relevant parts before ACTUAL use | ||
@Path("jokes") | ||
public class JokeResource { | ||
|
||
private static final EntityManagerFactory EMF = EMF_Creator.createEntityManagerFactory(EMF_Creator.DbSelector.DEV, EMF_Creator.Strategy.DROP_AND_CREATE); | ||
private static final JokeFacade FACADE = JokeFacade.getFacadeExample(EMF); | ||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); | ||
|
||
@GET | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public String demo() { | ||
return "{\"msg\":\"ACCESS GRANTED\"}"; | ||
} | ||
|
||
@Path("/count") | ||
@GET | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public String getJokeCount() { | ||
try { | ||
JsonObject count = new JsonObject(); | ||
count.addProperty("count", FACADE.getJokeCount()); | ||
return GSON.toJson(count); | ||
} catch (Exception ex) { | ||
return "{\"error\": \"" + ex.getMessage() + "\"}"; | ||
} | ||
} | ||
|
||
@GET | ||
@Path("/all") | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public String getAllJokes() throws Exception { | ||
try { | ||
return GSON.toJson(FACADE.getAllJokes()); | ||
} catch (Exception ex) { | ||
return "{\"error\": \"" + ex.getMessage() + "\"}"; | ||
} | ||
} | ||
|
||
@GET | ||
@Path("/all/dto") | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public String getAllJokesAsDTO() throws Exception { | ||
try { | ||
return GSON.toJson(FACADE.getAllJokesAsDTO()); | ||
} catch (Exception ex) { | ||
return "{\"error\": \"" + ex.getMessage() + "\"}"; | ||
} | ||
} | ||
|
||
|
||
@GET | ||
@Path("/{id}") | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public String getJokeByID(@PathParam("id") Long id) throws Exception { | ||
try { | ||
return GSON.toJson(FACADE.getJokeById(id)); | ||
} catch (Exception ex) { | ||
return "{\"error\": \"" + ex.getMessage() + "\"}"; | ||
} | ||
} | ||
|
||
@GET | ||
@Path("/{id}/dto") | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public String getJokeDTOByID(@PathParam("id") Long id) throws Exception { | ||
try { | ||
return GSON.toJson(FACADE.getJokeByIdAsDTO(id)); | ||
} catch (Exception ex) { | ||
return "{\"error\": \"" + ex.getMessage() + "\"}"; | ||
} | ||
} | ||
|
||
@GET | ||
@Path("/random") | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public String getJokeRandom() throws Exception { | ||
try { | ||
return GSON.toJson(FACADE.getJokeByRandom()); | ||
} catch (Exception ex) { | ||
return "{\"error\": \"" + ex.getMessage() + "\"}"; | ||
} | ||
} | ||
|
||
@GET | ||
@Path("/random/dto") | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public String getJokeRandomAsDTO() throws Exception { | ||
try { | ||
return GSON.toJson(FACADE.getJokeByRandomAsDTO()); | ||
} catch (Exception ex) { | ||
return "[\"error\": \"" + ex.getMessage() + "\"}"; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.