From 4cd8bf49ab237707e5e14f9a628040caea5ff78b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Wed, 10 Jan 2018 14:23:01 +0100 Subject: [PATCH 1/2] noissue adress rest --- src/main/java/library/App.java | 12 ++--- .../repositories/impl/DatabaseCatalog.java | 1 - .../examples/AddressRepositoryExample.java | 6 +-- .../library/web/rest/AdressesResources.java | 48 ++++++++++++++++++- 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/main/java/library/App.java b/src/main/java/library/App.java index cb3f214..ec5405f 100644 --- a/src/main/java/library/App.java +++ b/src/main/java/library/App.java @@ -18,12 +18,12 @@ public static void main( String[] args ) throws SQLException = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/workdb"); IUnitOfWork uow = new UnitOfWork(connection); IDatabaseCatalog catalog = new DatabaseCatalog(connection, uow); - AuthorRepositoryExample.execute(connection, catalog); - //AddressRepositoryExample.execute(connection, catalog); - UserRepositoryExample.execute(connection, catalog); - BookInformationRepositoryExample.execute(connection, catalog); - NotificationRepositoryExample.execute(connection, catalog); - PublisherRepositoryExample.execute(connection, catalog); + //AuthorRepositoryExample.execute(connection, catalog); + AddressRepositoryExample.execute(connection, catalog); + //UserRepositoryExample.execute(connection, catalog); + //BookInformationRepositoryExample.execute(connection, catalog); + //NotificationRepositoryExample.execute(connection, catalog); + //PublisherRepositoryExample.execute(connection, catalog); uow.saveChanges(); connection.close(); diff --git a/src/main/java/library/dao/repositories/impl/DatabaseCatalog.java b/src/main/java/library/dao/repositories/impl/DatabaseCatalog.java index 52831ba..3306edc 100644 --- a/src/main/java/library/dao/repositories/impl/DatabaseCatalog.java +++ b/src/main/java/library/dao/repositories/impl/DatabaseCatalog.java @@ -83,7 +83,6 @@ public IAdressRepository addresses() { try { return new AddressRepository(connection, new AdressMapper(), uow); } catch (SQLException e) { - // TODO Auto-generated catch block e.printStackTrace(); return null; } diff --git a/src/main/java/library/examples/AddressRepositoryExample.java b/src/main/java/library/examples/AddressRepositoryExample.java index a78c8b4..77511f8 100644 --- a/src/main/java/library/examples/AddressRepositoryExample.java +++ b/src/main/java/library/examples/AddressRepositoryExample.java @@ -21,12 +21,12 @@ public static void execute(Connection connection, IDatabaseCatalog catalog){ catalog.addresses().add(adress); catalog.addresses().add(adress); catalog.addresses().add(adress); - + System.out.println("Count: "+ catalog.addresses().count()); System.out.println("last id: "+ catalog.addresses().lastId()); - List
addressWithCity = catalog.addresses().withCity("Gdansk"); - List
addressWithPostal = catalog.addresses().withPostal("80-041"); + //List
addressWithCity = catalog.addresses().withCity("Gdansk"); + //List
addressWithPostal = catalog.addresses().withPostal("80-041"); List
adresses = catalog.addresses().getPage(1, 2); diff --git a/src/main/java/library/web/rest/AdressesResources.java b/src/main/java/library/web/rest/AdressesResources.java index 7f24358..870fab9 100644 --- a/src/main/java/library/web/rest/AdressesResources.java +++ b/src/main/java/library/web/rest/AdressesResources.java @@ -2,7 +2,11 @@ import java.util.List; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; @@ -13,6 +17,7 @@ import javax.ejb.Stateless; import library.dao.repositories.IAdressRepository; +import library.dao.repositories.IDatabaseCatalog; import library.dao.repositories.impl.HsqlCatalogFactory; import library.domain.Address; @@ -20,9 +25,10 @@ @Stateless public class AdressesResources { IAdressRepository _addresses; - + IDatabaseCatalog _library; public AdressesResources() { _addresses = new HsqlCatalogFactory().library().addresses(); + _library = new HsqlCatalogFactory().library(); } @@ -44,4 +50,44 @@ public Response get(@PathParam("id") int id){ return Response.status(404).build(); return Response.ok(address).build(); } + + @POST + @Consumes(MediaType.APPLICATION_JSON) + public Response addAddress(Address address){ + _addresses.add(address); + _library.saveChanges(); + return Response.ok().build(); + } + + @PUT + @Consumes(MediaType.APPLICATION_JSON) + @Path("/{id}") + public Response updateAddress(@PathParam("id") int id ,Address address){ + + Address a = _addresses.get(id); + if(a ==null) + return Response.status(404).build(); + address.setId(id); + _addresses.update(address); + _library.saveChanges(); + return Response.ok().build(); + } + + @DELETE + @Path("/{id}") + public Response deleteAddress(@PathParam("id") int id){ + Address a = _addresses.get(id); + if(a==null) + return Response.status(404).build(); + _addresses.delete(a); + _library.saveChanges(); + return Response.noContent().build(); + } + + @GET + @Path("/status") + @Produces(MediaType.TEXT_HTML) + public String test(){ + return "OK"; + } } From c6e4b9f620f2a5866b5a5c5a6ae48c833c9d836a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= <32327142+s15667@users.noreply.github.com> Date: Wed, 17 Jan 2018 11:40:42 +0100 Subject: [PATCH 2/2] Update App.java --- src/main/java/library/App.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/library/App.java b/src/main/java/library/App.java index ec5405f..cb3f214 100644 --- a/src/main/java/library/App.java +++ b/src/main/java/library/App.java @@ -18,12 +18,12 @@ public static void main( String[] args ) throws SQLException = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/workdb"); IUnitOfWork uow = new UnitOfWork(connection); IDatabaseCatalog catalog = new DatabaseCatalog(connection, uow); - //AuthorRepositoryExample.execute(connection, catalog); - AddressRepositoryExample.execute(connection, catalog); - //UserRepositoryExample.execute(connection, catalog); - //BookInformationRepositoryExample.execute(connection, catalog); - //NotificationRepositoryExample.execute(connection, catalog); - //PublisherRepositoryExample.execute(connection, catalog); + AuthorRepositoryExample.execute(connection, catalog); + //AddressRepositoryExample.execute(connection, catalog); + UserRepositoryExample.execute(connection, catalog); + BookInformationRepositoryExample.execute(connection, catalog); + NotificationRepositoryExample.execute(connection, catalog); + PublisherRepositoryExample.execute(connection, catalog); uow.saveChanges(); connection.close();