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>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>2.0.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
@@ -79,16 +84,6 @@
|
||||
<artifactId>reflections</artifactId>
|
||||
<version>0.10.2</version>
|
||||
</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) -->
|
||||
<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;
|
||||
|
||||
import com.humanbooster.config.HibernateConfig;
|
||||
import com.humanbooster.dao.GenericDao;
|
||||
import com.humanbooster.dao.UserDao;
|
||||
import com.humanbooster.model.User;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Path("/users")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class UserController {
|
||||
public class UserController extends GenericControllerImpl<User, Long> {
|
||||
|
||||
SessionFactory sessionFactory = HibernateConfig.getSessionFactory();
|
||||
|
||||
private final UserDao dao = new UserDao(sessionFactory);
|
||||
|
||||
@GET
|
||||
public List<User> getAll() {
|
||||
return dao.findAll();
|
||||
public UserController() {
|
||||
this(HibernateConfig.getSessionFactory(), new UserDao(HibernateConfig.getSessionFactory()));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{id}")
|
||||
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);
|
||||
public UserController(SessionFactory sessionFactory, UserDao userDao) {
|
||||
super(sessionFactory, userDao);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.List;
|
||||
public interface GenericDao<T, ID> {
|
||||
void create(T entity);
|
||||
T read(ID id);
|
||||
void update(T entity);
|
||||
void update(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
|
||||
public void update(T entity) {
|
||||
public void update(ID id) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
T entity = session.get(entityClass, id);
|
||||
session.merge(entity);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
@@ -54,7 +55,7 @@ public abstract class GenericDaoImpl<T, ID> implements GenericDao<T, ID> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll() {
|
||||
public List<T> getAll() {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
List<T> entities = session.createQuery("from " + entityClass.getName(), entityClass).list();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.humanbooster.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
@@ -9,6 +10,7 @@ public class Article extends Publication {
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name="author_id", nullable = false)
|
||||
@JsonBackReference
|
||||
private User author;
|
||||
|
||||
private int views = 0;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.humanbooster.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
@@ -19,6 +20,7 @@ public class User {
|
||||
private String email;
|
||||
|
||||
@OneToMany(mappedBy="author", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
|
||||
@JsonManagedReference
|
||||
private List<Article> articles;
|
||||
|
||||
public User() {}
|
||||
|
||||
@@ -15,8 +15,8 @@ public record AdService (AdDao adDao) {
|
||||
return adDao.read(id);
|
||||
}
|
||||
|
||||
public void updateAd(Ad ad) {
|
||||
adDao.update(ad);
|
||||
public void updateAd(Long id) {
|
||||
adDao.update(id);
|
||||
}
|
||||
|
||||
public void deleteAd(Long id) {
|
||||
@@ -24,6 +24,6 @@ public record AdService (AdDao adDao) {
|
||||
}
|
||||
|
||||
public List<Ad> getAllAds() {
|
||||
return adDao.findAll();
|
||||
return adDao.getAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ public record ArticleService(ArticleDao articleDao) {
|
||||
articleDao.read(id);
|
||||
}
|
||||
|
||||
public void updateArticle(Article article) {
|
||||
articleDao.update(article);
|
||||
public void updateArticle(Long id) {
|
||||
articleDao.update(id);
|
||||
}
|
||||
|
||||
public void deleteArticle(Long id) {
|
||||
@@ -24,7 +24,7 @@ public record ArticleService(ArticleDao articleDao) {
|
||||
}
|
||||
|
||||
public List<Article> getAllArticles() {
|
||||
return articleDao.findAll();
|
||||
return articleDao.getAll();
|
||||
}
|
||||
|
||||
public Article findArticleByAuthor(String author) {
|
||||
|
||||
@@ -15,8 +15,8 @@ public record UserService (UserDao userDao) {
|
||||
userDao.read(id);
|
||||
}
|
||||
|
||||
public void updateUser(User user) {
|
||||
userDao.update(user);
|
||||
public void updateUser(Long id) {
|
||||
userDao.update(id);
|
||||
}
|
||||
|
||||
public void deleteUser(Long id) {
|
||||
@@ -24,7 +24,7 @@ public record UserService (UserDao userDao) {
|
||||
}
|
||||
|
||||
public List<User> getAllUsers() {
|
||||
return userDao.findAll();
|
||||
return userDao.getAll();
|
||||
}
|
||||
|
||||
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