|
| 1 | +package com.baeldung.linkrest.apis; |
| 2 | + |
| 3 | +import javax.ws.rs.DELETE; |
| 4 | +import javax.ws.rs.GET; |
| 5 | +import javax.ws.rs.POST; |
| 6 | +import javax.ws.rs.PUT; |
| 7 | +import javax.ws.rs.Path; |
| 8 | +import javax.ws.rs.PathParam; |
| 9 | +import javax.ws.rs.Produces; |
| 10 | +import javax.ws.rs.core.Configuration; |
| 11 | +import javax.ws.rs.core.Context; |
| 12 | +import javax.ws.rs.core.MediaType; |
| 13 | +import javax.ws.rs.core.UriInfo; |
| 14 | + |
| 15 | +import com.baeldung.cayenne.Department; |
| 16 | +import com.baeldung.cayenne.Employee; |
| 17 | +import com.nhl.link.rest.DataResponse; |
| 18 | +import com.nhl.link.rest.LinkRest; |
| 19 | +import com.nhl.link.rest.SimpleResponse; |
| 20 | + |
| 21 | +@Produces(MediaType.APPLICATION_JSON) |
| 22 | +public class EmployeeSubResource { |
| 23 | + |
| 24 | + private Configuration config; |
| 25 | + |
| 26 | + private int departmentId; |
| 27 | + |
| 28 | + public EmployeeSubResource(int departmentId, Configuration config) { |
| 29 | + this.departmentId = departmentId; |
| 30 | + this.config = config; |
| 31 | + } |
| 32 | + |
| 33 | + public EmployeeSubResource() { |
| 34 | + } |
| 35 | + |
| 36 | + @GET |
| 37 | + public DataResponse<Employee> getAll(@Context UriInfo uriInfo) { |
| 38 | + return LinkRest.select(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).uri(uriInfo).get(); |
| 39 | + } |
| 40 | + |
| 41 | + @GET |
| 42 | + @Path("{id}") |
| 43 | + public DataResponse<Employee> getOne(@PathParam("id") int id, @Context UriInfo uriInfo) { |
| 44 | + return LinkRest.select(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).byId(id).uri(uriInfo).getOne(); |
| 45 | + } |
| 46 | + |
| 47 | + @POST |
| 48 | + public SimpleResponse create(String data) { |
| 49 | + |
| 50 | + return LinkRest.create(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).sync(data); |
| 51 | + } |
| 52 | + |
| 53 | + @PUT |
| 54 | + public SimpleResponse createOrUpdate(String data) { |
| 55 | + return LinkRest.create(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).sync(data); |
| 56 | + } |
| 57 | + |
| 58 | + @DELETE |
| 59 | + @Path("{id}") |
| 60 | + public SimpleResponse delete(@PathParam("id") int id) { |
| 61 | + return LinkRest.delete(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).id(id).delete(); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments