Further API implmentation.
This commit is contained in:
12
src/main/groovy/com/jdbernard/nlsongs/rest/PingResource.java
Normal file
12
src/main/groovy/com/jdbernard/nlsongs/rest/PingResource.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.jdbernard.nlsongs.rest;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
|
||||
@Path("v1/ping") @AllowCors
|
||||
public class PingResource {
|
||||
|
||||
@GET
|
||||
@Produces("text/plain")
|
||||
public String ping() { return "pong"; } }
|
@ -2,6 +2,7 @@ package com.jdbernard.nlsongs.rest;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
@ -38,6 +39,14 @@ public class ServicesResource {
|
||||
NLSongsContext.songsDB.update(service);
|
||||
return service; }
|
||||
|
||||
@DELETE @Path("/{serviceId}")
|
||||
public Service deleteService(@PathParam("serviceId") int serviceId) {
|
||||
Service service = NLSongsContext.songsDB.findService(serviceId);
|
||||
|
||||
if (service != null) { NLSongsContext.songsDB.delete(service); }
|
||||
|
||||
return service; }
|
||||
|
||||
@GET @Path("/withSong/{songId}")
|
||||
public List<Service> getServicesForSong(@PathParam("songId") int songId) {
|
||||
return NLSongsContext.songsDB.findServicesForSongId(songId); }
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.jdbernard.nlsongs.rest;
|
||||
|
||||
import java.util.List;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
@ -36,6 +37,14 @@ public class SongsResource {
|
||||
NLSongsContext.songsDB.update(song);
|
||||
return song; }
|
||||
|
||||
@DELETE @Path("/{songId}")
|
||||
public Song deleteSong(@PathParam("songId") int songId) {
|
||||
Song song = NLSongsContext.songsDB.findSong(songId);
|
||||
|
||||
if (song != null) { NLSongsContext.songsDB.delete(song); }
|
||||
|
||||
return song; }
|
||||
|
||||
@GET @Path("/forService/{serviceId}")
|
||||
public List<Song> getSongsForService(@PathParam("serviceId") int serviceId) {
|
||||
return NLSongsContext.songsDB.findSongsForServiceId(serviceId); }
|
||||
@ -43,4 +52,5 @@ public class SongsResource {
|
||||
@GET @Path("/byArtist/{artist}")
|
||||
public List<Song> getSongsForArtist(@PathParam("artist") String artist) {
|
||||
return NLSongsContext.songsDB.findSongsByArtist(artist); }
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,83 @@
|
||||
package com.jdbernard.nlsongs.rest;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
import javax.annotation.security.PermitAll;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.SecurityContext;
|
||||
|
||||
import com.jdbernard.nlsongs.servlet.NLSongsContext;
|
||||
import com.jdbernard.nlsongs.model.User;
|
||||
import com.jdbernard.nlsongs.model.Token;
|
||||
|
||||
@Path("v1/users") @AllowCors @PermitAll
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
@Consumes({MediaType.APPLICATION_JSON})
|
||||
public class UsersResource {
|
||||
|
||||
@Context SecurityContext secCtx;
|
||||
|
||||
@GET @RolesAllowed("admin")
|
||||
public List<User> getUsers() {
|
||||
return NLSongsContext.songsDB.findAllUsers(); }
|
||||
|
||||
@POST @RolesAllowed("admin")
|
||||
public User postUser(User user) {
|
||||
return NLSongsContext.songsDB.create(user); }
|
||||
|
||||
@GET @Path("/{username}")
|
||||
public Response getUser(@PathParam("username") String username) {
|
||||
|
||||
// If they are looking up their own information, OK.
|
||||
if (username == secCtx.getUserPrincipal().getName() ||
|
||||
// Or if they are an admin, OK.
|
||||
secCtx.isUserInRole("admin")) {
|
||||
|
||||
return Response.ok(
|
||||
NLSongsContext.songsDB.findUser(username)).build(); }
|
||||
|
||||
else return Response.status(Response.Status.FORBIDDEN).build(); }
|
||||
|
||||
|
||||
@PUT @Path("/{username}")
|
||||
public Response putUser(@PathParam("username") String username, User user) {
|
||||
|
||||
// If they are looking up their own information, OK.
|
||||
if (username == secCtx.getUserPrincipal().getName() ||
|
||||
// Or if they are an admin, OK.
|
||||
secCtx.isUserInRole("admin")) {
|
||||
|
||||
NLSongsContext.songsDB.update(user);
|
||||
|
||||
return Response.ok(user).build(); }
|
||||
|
||||
else return Response.status(Response.Status.FORBIDDEN).build(); }
|
||||
|
||||
@DELETE @Path("/{username}")
|
||||
public Response deleteUser(@PathParam("username") String username) {
|
||||
|
||||
// If they are looking up their own information, OK.
|
||||
if (username == secCtx.getUserPrincipal().getName() ||
|
||||
// Or if they are an admin, OK.
|
||||
secCtx.isUserInRole("admin")) {
|
||||
|
||||
User user = NLSongsContext.songsDB.findUser(username);
|
||||
|
||||
if (user != null) NLSongsContext.songsDB.delete(user);
|
||||
|
||||
return Response.ok(user).build(); }
|
||||
|
||||
else return Response.status(Response.Status.FORBIDDEN).build(); }
|
||||
|
||||
}
|
Reference in New Issue
Block a user