Refactor services and controllers to use ID for updates and standardize DAO method names
This commit is contained in:
@@ -69,6 +69,11 @@
|
|||||||
<artifactId>jakarta.validation-api</artifactId>
|
<artifactId>jakarta.validation-api</artifactId>
|
||||||
<version>3.1.0</version>
|
<version>3.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-simple</artifactId>
|
||||||
|
<version>2.0.17</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.mysql</groupId>
|
<groupId>com.mysql</groupId>
|
||||||
<artifactId>mysql-connector-j</artifactId>
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
@@ -79,16 +84,6 @@
|
|||||||
<artifactId>reflections</artifactId>
|
<artifactId>reflections</artifactId>
|
||||||
<version>0.10.2</version>
|
<version>0.10.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.slf4j</groupId>
|
|
||||||
<artifactId>slf4j-simple</artifactId>
|
|
||||||
<version>2.0.17</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mockito</groupId>
|
|
||||||
<artifactId>mockito-core</artifactId>
|
|
||||||
<version>5.17.0</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- JAX-RS (Jersey) -->
|
<!-- JAX-RS (Jersey) -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.humanbooster.controller;
|
||||||
|
|
||||||
|
import com.humanbooster.dao.GenericDao;
|
||||||
|
import com.humanbooster.model.Article;
|
||||||
|
import jakarta.ws.rs.Path;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
|
||||||
|
@Path("/articles")
|
||||||
|
public class ArticleController extends GenericControllerImpl<Article, Long> {
|
||||||
|
|
||||||
|
public ArticleController(SessionFactory sessionFactory, GenericDao<Article, Long> dao) {
|
||||||
|
super(sessionFactory,dao);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.humanbooster.controller;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface GenericController<T, ID> {
|
||||||
|
void create(T entity);
|
||||||
|
T read(ID id);
|
||||||
|
void update(ID id);
|
||||||
|
void delete(ID id);
|
||||||
|
List<T> getAll();
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package com.humanbooster.controller;
|
||||||
|
|
||||||
|
import com.humanbooster.dao.GenericDao;
|
||||||
|
import jakarta.ws.rs.*;
|
||||||
|
import jakarta.ws.rs.core.MediaType;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
public abstract class GenericControllerImpl<T, ID> implements GenericController<T, ID>, GenericDao<T, ID> {
|
||||||
|
|
||||||
|
protected final SessionFactory sessionFactory;
|
||||||
|
private final GenericDao<T, ID> dao;
|
||||||
|
|
||||||
|
public GenericControllerImpl(SessionFactory sessionFactory, GenericDao<T, ID> dao) {
|
||||||
|
this.sessionFactory = sessionFactory;
|
||||||
|
this.dao = dao;
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Override
|
||||||
|
public void create(T entity) {
|
||||||
|
dao.create(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("/{id}")
|
||||||
|
@Override
|
||||||
|
public T read(@PathParam("id") ID id) {
|
||||||
|
return dao.read(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PUT
|
||||||
|
@Path("/{id}")
|
||||||
|
@Override
|
||||||
|
public void update(@PathParam("id") ID id) {
|
||||||
|
dao.update(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DELETE
|
||||||
|
@Path("/{id}")
|
||||||
|
@Override
|
||||||
|
public void delete(@PathParam("id") ID id) {
|
||||||
|
dao.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Override
|
||||||
|
public List<T> getAll() {
|
||||||
|
return dao.getAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,50 +1,25 @@
|
|||||||
package com.humanbooster.controller;
|
package com.humanbooster.controller;
|
||||||
|
|
||||||
import com.humanbooster.config.HibernateConfig;
|
import com.humanbooster.config.HibernateConfig;
|
||||||
|
import com.humanbooster.dao.GenericDao;
|
||||||
import com.humanbooster.dao.UserDao;
|
import com.humanbooster.dao.UserDao;
|
||||||
import com.humanbooster.model.User;
|
import com.humanbooster.model.User;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
import jakarta.ws.rs.*;
|
import jakarta.ws.rs.*;
|
||||||
import jakarta.ws.rs.core.MediaType;
|
import jakarta.ws.rs.core.MediaType;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Path("/users")
|
@Path("/users")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
public class UserController {
|
public class UserController extends GenericControllerImpl<User, Long> {
|
||||||
|
|
||||||
SessionFactory sessionFactory = HibernateConfig.getSessionFactory();
|
public UserController() {
|
||||||
|
this(HibernateConfig.getSessionFactory(), new UserDao(HibernateConfig.getSessionFactory()));
|
||||||
private final UserDao dao = new UserDao(sessionFactory);
|
|
||||||
|
|
||||||
@GET
|
|
||||||
public List<User> getAll() {
|
|
||||||
return dao.findAll();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
public UserController(SessionFactory sessionFactory, UserDao userDao) {
|
||||||
@Path("/{id}")
|
super(sessionFactory, userDao);
|
||||||
public User getById(@PathParam("id") Long id) {
|
|
||||||
return dao.read(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@POST
|
|
||||||
public void create(User person) {
|
|
||||||
dao.create(person);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PUT
|
|
||||||
@Path("/{id}")
|
|
||||||
public void update(@PathParam("id") Long id, User user) {
|
|
||||||
user.setId(id);
|
|
||||||
dao.update(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DELETE
|
|
||||||
@Path("/{id}")
|
|
||||||
public void delete(@PathParam("id") Long id) {
|
|
||||||
System.out.println();
|
|
||||||
dao.delete(id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import java.util.List;
|
|||||||
public interface GenericDao<T, ID> {
|
public interface GenericDao<T, ID> {
|
||||||
void create(T entity);
|
void create(T entity);
|
||||||
T read(ID id);
|
T read(ID id);
|
||||||
void update(T entity);
|
void update(ID id);
|
||||||
void delete(ID id);
|
void delete(ID id);
|
||||||
List<T> findAll();
|
List<T> getAll();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,9 +35,10 @@ public abstract class GenericDaoImpl<T, ID> implements GenericDao<T, ID> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(T entity) {
|
public void update(ID id) {
|
||||||
try (Session session = sessionFactory.openSession()) {
|
try (Session session = sessionFactory.openSession()) {
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
|
T entity = session.get(entityClass, id);
|
||||||
session.merge(entity);
|
session.merge(entity);
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
}
|
}
|
||||||
@@ -54,7 +55,7 @@ public abstract class GenericDaoImpl<T, ID> implements GenericDao<T, ID> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<T> findAll() {
|
public List<T> getAll() {
|
||||||
try (Session session = sessionFactory.openSession()) {
|
try (Session session = sessionFactory.openSession()) {
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
List<T> entities = session.createQuery("from " + entityClass.getName(), entityClass).list();
|
List<T> entities = session.createQuery("from " + entityClass.getName(), entityClass).list();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.humanbooster.model;
|
package com.humanbooster.model;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -9,6 +10,7 @@ public class Article extends Publication {
|
|||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name="author_id", nullable = false)
|
@JoinColumn(name="author_id", nullable = false)
|
||||||
|
@JsonBackReference
|
||||||
private User author;
|
private User author;
|
||||||
|
|
||||||
private int views = 0;
|
private int views = 0;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.humanbooster.model;
|
package com.humanbooster.model;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
@@ -19,6 +20,7 @@ public class User {
|
|||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
@OneToMany(mappedBy="author", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
|
@OneToMany(mappedBy="author", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
|
||||||
|
@JsonManagedReference
|
||||||
private List<Article> articles;
|
private List<Article> articles;
|
||||||
|
|
||||||
public User() {}
|
public User() {}
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ public record AdService (AdDao adDao) {
|
|||||||
return adDao.read(id);
|
return adDao.read(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateAd(Ad ad) {
|
public void updateAd(Long id) {
|
||||||
adDao.update(ad);
|
adDao.update(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteAd(Long id) {
|
public void deleteAd(Long id) {
|
||||||
@@ -24,6 +24,6 @@ public record AdService (AdDao adDao) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<Ad> getAllAds() {
|
public List<Ad> getAllAds() {
|
||||||
return adDao.findAll();
|
return adDao.getAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ public record ArticleService(ArticleDao articleDao) {
|
|||||||
articleDao.read(id);
|
articleDao.read(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateArticle(Article article) {
|
public void updateArticle(Long id) {
|
||||||
articleDao.update(article);
|
articleDao.update(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteArticle(Long id) {
|
public void deleteArticle(Long id) {
|
||||||
@@ -24,7 +24,7 @@ public record ArticleService(ArticleDao articleDao) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<Article> getAllArticles() {
|
public List<Article> getAllArticles() {
|
||||||
return articleDao.findAll();
|
return articleDao.getAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Article findArticleByAuthor(String author) {
|
public Article findArticleByAuthor(String author) {
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ public record UserService (UserDao userDao) {
|
|||||||
userDao.read(id);
|
userDao.read(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateUser(User user) {
|
public void updateUser(Long id) {
|
||||||
userDao.update(user);
|
userDao.update(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteUser(Long id) {
|
public void deleteUser(Long id) {
|
||||||
@@ -24,7 +24,7 @@ public record UserService (UserDao userDao) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<User> getAllUsers() {
|
public List<User> getAllUsers() {
|
||||||
return userDao.findAll();
|
return userDao.getAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findUserByEmail(String email) {
|
public User findUserByEmail(String email) {
|
||||||
|
|||||||
14
hibernate-project/test.http
Normal file
14
hibernate-project/test.http
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
### GET request to example server
|
||||||
|
GET http://localhost/users
|
||||||
|
|
||||||
|
###
|
||||||
|
POST http://localhost/users/
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"name": "John Doe",
|
||||||
|
"email": "john.doe@example.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
###
|
||||||
|
DELETE http://localhost/articles/13
|
||||||
Reference in New Issue
Block a user