-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #9 - Add REST API to create a forum
- Loading branch information
Showing
17 changed files
with
585 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/api/target/ |
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,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project-shared-configuration> | ||
<!-- | ||
This file contains additional configuration written by modules in the NetBeans IDE. | ||
The configuration is intended to be shared among all the users of project and | ||
therefore it is assumed to be part of version control checkout. | ||
Without this configuration present, some functionality in the IDE may be limited or fail altogether. | ||
--> | ||
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1"> | ||
<!-- | ||
Properties that influence various parts of the IDE, especially code formatting and the like. | ||
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up. | ||
That way multiple projects can share the same settings (useful for formatting rules for example). | ||
Any value defined here will override the pom.xml file value but is only applicable to the current project. | ||
--> | ||
<org-netbeans-modules-maven-jaxws.rest_2e_config_2e_type>ide</org-netbeans-modules-maven-jaxws.rest_2e_config_2e_type> | ||
</properties> | ||
</project-shared-configuration> |
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,79 @@ | ||
/* | ||
* Copyright (c) 2002-2024, Manorrock.com. All Rights Reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package api; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.EntityManagerFactory; | ||
import jakarta.persistence.Persistence; | ||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* The global application bean. | ||
* | ||
* @author Manfred Riem ([email protected]) | ||
*/ | ||
@ApplicationScoped | ||
public class ApplicationBean { | ||
|
||
/** | ||
* Stores the entity manager. | ||
*/ | ||
private EntityManager em; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public ApplicationBean() { | ||
|
||
File databaseDirectory = new File(System.getProperty("user.home"), ".manorrock/phoenix"); | ||
if (!databaseDirectory.exists()) { | ||
databaseDirectory.mkdirs(); | ||
} | ||
|
||
Map<String, String> properties = new HashMap<>(); | ||
properties.put("jakarta.persistence.jdbc.url", "jdbc:sqlite:" + | ||
new File(System.getProperty("user.home"), ".manorrock/phoenix/sqlite.db").getAbsolutePath()); | ||
|
||
EntityManagerFactory emf = Persistence.createEntityManagerFactory("phoenix", properties); | ||
em = emf.createEntityManager(); | ||
} | ||
|
||
/** | ||
* Get the entity manager. | ||
* | ||
* @return the entity manager. | ||
*/ | ||
public EntityManager getEntityManager() { | ||
return em; | ||
} | ||
} |
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,98 @@ | ||
/* | ||
* Copyright (c) 2002-2024, Manorrock.com. All Rights Reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package api; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import static jakarta.persistence.GenerationType.AUTO; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import java.math.BigInteger; | ||
|
||
/** | ||
* A forum. | ||
* | ||
* @author Manfred Riem ([email protected]) | ||
*/ | ||
@Entity | ||
@Table(name = "forum") | ||
public class Forum { | ||
|
||
/** | ||
* Stores the id. | ||
*/ | ||
@Column(name = "id") | ||
@GeneratedValue(strategy = AUTO) | ||
@Id | ||
private BigInteger id; | ||
|
||
/** | ||
* Stores the name. | ||
*/ | ||
@Column(name = "name", nullable = true) | ||
private String name; | ||
|
||
/** | ||
* Get the id. | ||
* | ||
* @return the id. | ||
*/ | ||
public BigInteger getId() { | ||
return id; | ||
} | ||
|
||
/** | ||
* Get the name. | ||
* | ||
* @return the name. | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Set the id. | ||
* | ||
* @param id the id. | ||
*/ | ||
public void setId(BigInteger id) { | ||
this.id = id; | ||
} | ||
|
||
/** | ||
* Set the name. | ||
* | ||
* @param name the name. | ||
*/ | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
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,73 @@ | ||
/* | ||
* Copyright (c) 2002-2024, Manorrock.com. All Rights Reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package api; | ||
|
||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.EntityTransaction; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
/** | ||
* The Forum resource. | ||
* | ||
* @author Manfred Riem ([email protected]) | ||
*/ | ||
@Path("forum") | ||
@RequestScoped | ||
public class ForumResource { | ||
|
||
/** | ||
* Stores the application. | ||
*/ | ||
@Inject | ||
private ApplicationBean application; | ||
|
||
/** | ||
* Create forum. | ||
* | ||
* @return the forum. | ||
*/ | ||
@Path("") | ||
@GET | ||
public Forum create() { | ||
EntityManager em = application.getEntityManager(); | ||
EntityTransaction tx = em.getTransaction(); | ||
tx.begin(); | ||
Forum forum = new Forum(); | ||
forum.setName("test" + System.currentTimeMillis()); | ||
forum = em.merge(forum); | ||
em.persist(forum); | ||
em.flush(); | ||
tx.commit(); | ||
return forum; | ||
} | ||
} |
Oops, something went wrong.