From 8f04ac7ae884e4a195f67d246c209fd914e9e5b0 Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Tue, 25 Jan 2022 19:17:41 +0100 Subject: [PATCH] Added userDAO --- .../server/controller/EventController.java | 4 +- .../vpr/server/controller/UserController.java | 15 +++-- .../implementation/UserDAOImplementation.java | 24 ++++++++ .../vpr/server/dao/interfaces/UserDAO.java | 9 +++ .../main/java/com/vpr/server/data/User.java | 10 +++- .../java/com/vpr/server/json/JSONMapper.java | 58 +++++++++++++------ .../server/repository/EventRepository.java | 3 - 7 files changed, 95 insertions(+), 28 deletions(-) create mode 100644 server/src/main/java/com/vpr/server/dao/implementation/UserDAOImplementation.java create mode 100644 server/src/main/java/com/vpr/server/dao/interfaces/UserDAO.java diff --git a/server/src/main/java/com/vpr/server/controller/EventController.java b/server/src/main/java/com/vpr/server/controller/EventController.java index fbcd2f7..3cc7b29 100644 --- a/server/src/main/java/com/vpr/server/controller/EventController.java +++ b/server/src/main/java/com/vpr/server/controller/EventController.java @@ -15,8 +15,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; -import java.sql.Time; -import java.text.SimpleDateFormat; import java.util.List; @Controller @@ -96,7 +94,7 @@ public class EventController { List eventList = eventDAO.getAllEventsInTimespan(authUser.getId(), startDate, endDate); - return new ResponseEntity<>(JSONMapper.ToJSON(eventList), HttpStatus.OK); + return new ResponseEntity<>(JSONMapper.eventListToJSON(eventList), HttpStatus.OK); } diff --git a/server/src/main/java/com/vpr/server/controller/UserController.java b/server/src/main/java/com/vpr/server/controller/UserController.java index 2698b35..1426412 100644 --- a/server/src/main/java/com/vpr/server/controller/UserController.java +++ b/server/src/main/java/com/vpr/server/controller/UserController.java @@ -1,6 +1,9 @@ package com.vpr.server.controller; +import com.vpr.server.dao.interfaces.UserDAO; +import com.vpr.server.data.Event; import com.vpr.server.data.User; +import com.vpr.server.json.JSONMapper; import com.vpr.server.repository.UserRepository; import com.vpr.server.security.Hasher; import com.vpr.server.security.Token; @@ -9,19 +12,21 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; -import org.springframework.web.server.ResponseStatusException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.util.Arrays; +import java.util.List; @Controller @RequestMapping(path = "/user") public class UserController { @Autowired private UserRepository userRepository; + @Autowired + private UserDAO userDAO; - private AuthController authController; + private final AuthController authController; public UserController() { this.authController = new AuthController(); @@ -146,7 +151,9 @@ public class UserController { @GetMapping(path = "/all") public @ResponseBody - Object[] getAllUsers() { - return userRepository.findAllUsernames(); + ResponseEntity getAllUser() { + List userList = userDAO.getAllUser(); + + return new ResponseEntity<>(JSONMapper.userListToJSON(userList), HttpStatus.OK); } } diff --git a/server/src/main/java/com/vpr/server/dao/implementation/UserDAOImplementation.java b/server/src/main/java/com/vpr/server/dao/implementation/UserDAOImplementation.java new file mode 100644 index 0000000..3c2b857 --- /dev/null +++ b/server/src/main/java/com/vpr/server/dao/implementation/UserDAOImplementation.java @@ -0,0 +1,24 @@ +package com.vpr.server.dao.implementation; + +import com.vpr.server.dao.interfaces.UserDAO; +import com.vpr.server.data.Event; +import com.vpr.server.data.User; +import org.springframework.stereotype.Repository; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.transaction.Transactional; +import java.util.List; + +@Repository +@Transactional +public class UserDAOImplementation implements UserDAO { + + @PersistenceContext + private EntityManager manager; + + @Override + public List getAllUser() { + return manager.createNamedQuery("getAllUser", User.class).getResultList(); + } +} diff --git a/server/src/main/java/com/vpr/server/dao/interfaces/UserDAO.java b/server/src/main/java/com/vpr/server/dao/interfaces/UserDAO.java new file mode 100644 index 0000000..b163e94 --- /dev/null +++ b/server/src/main/java/com/vpr/server/dao/interfaces/UserDAO.java @@ -0,0 +1,9 @@ +package com.vpr.server.dao.interfaces; + +import com.vpr.server.data.User; + +import java.util.List; + +public interface UserDAO { + List getAllUser(); +} diff --git a/server/src/main/java/com/vpr/server/data/User.java b/server/src/main/java/com/vpr/server/data/User.java index ec69209..01e0b84 100644 --- a/server/src/main/java/com/vpr/server/data/User.java +++ b/server/src/main/java/com/vpr/server/data/User.java @@ -4,7 +4,15 @@ import javax.persistence.*; import java.util.List; // @Entity creates a table out of this class with Hibernate -@Entity +@Entity(name = "User") +@Table(name = "user") +@NamedNativeQueries({ + @NamedNativeQuery( + name = "getAllUser", + query = "SELECT * FROM user", + resultClass = User.class + ) +}) public class User { // Generate the primary key @Id diff --git a/server/src/main/java/com/vpr/server/json/JSONMapper.java b/server/src/main/java/com/vpr/server/json/JSONMapper.java index 30ab4cf..1707132 100644 --- a/server/src/main/java/com/vpr/server/json/JSONMapper.java +++ b/server/src/main/java/com/vpr/server/json/JSONMapper.java @@ -1,29 +1,53 @@ package com.vpr.server.json; import com.vpr.server.data.Event; +import com.vpr.server.data.User; import com.vpr.server.data.UserEvent; + import java.sql.Time; import java.util.ArrayList; import java.util.List; public class JSONMapper { - public static List ToJSON(Event event){ + + public static String userToJSON(User user) { + return "{" + + "\"userId\": " + user.getId() + ", " + + "\"forename\": \"" + user.getForename() + "\", " + + "\"name\": \"" + user.getName() + "\", " + + "\"login\": \"" + user.getLogin() + "\"," + + "\"isAdmin\": " + user.isAdmin() + + "}"; + } + + public static String userListToJSON(List userList) { + StringBuilder userListJSON = new StringBuilder(); + for (User user : userList) { + userListJSON.append(", "); + userListJSON.append(userToJSON(user)); + } + userListJSON.delete(0, 2); + + return "[" + userListJSON + "]"; + } + + public static List eventToJSON(Event event) { List eventListJSON = new ArrayList<>(); for (UserEvent userEvent : event.getUserEvent()) { String eventJSON = "{" + - "\"ownerId\": " + userEvent.getUser().getId() + ", " + - "\"ownerName\": \"" + userEvent.getUser().getForename() + " " + userEvent.getUser().getName() + "\", " + - "\"date\": \"" + userEvent.getDate() + "\", " + - "\"id\": " + event.getId() + "," + - "\"name\": \"" + event.getName() + "\"," + - "\"priority\": " + event.getPriority() + "," + - "\"fullDay\": " + event.isFullDay() + "," + - "\"private\": " + event.isPrivate() + "," + - "\"start\": " + ToJSON(event.getStart()) + "," + - "\"end\": " + ToJSON(event.getEnd()) + + "\"ownerId\": " + userEvent.getUser().getId() + ", " + + "\"ownerName\": \"" + userEvent.getUser().getForename() + " " + userEvent.getUser().getName() + "\", " + + "\"date\": \"" + userEvent.getDate() + "\", " + + "\"id\": " + event.getId() + "," + + "\"name\": \"" + event.getName() + "\"," + + "\"priority\": " + event.getPriority() + "," + + "\"fullDay\": " + event.isFullDay() + "," + + "\"private\": " + event.isPrivate() + "," + + "\"start\": " + timeToJSON(event.getStart()) + "," + + "\"end\": " + timeToJSON(event.getEnd()) + "}"; eventListJSON.add(eventJSON); @@ -32,11 +56,11 @@ public class JSONMapper { return eventListJSON; } - public static String ToJSON(List eventList){ + public static String eventListToJSON(List eventList) { StringBuilder eventListJSON = new StringBuilder(); - for(Event event : eventList){ - List eventsJSON = ToJSON(event); - for(String eventJSON : eventsJSON){ + for (Event event : eventList) { + List eventsJSON = eventToJSON(event); + for (String eventJSON : eventsJSON) { eventListJSON.append(", "); eventListJSON.append(eventJSON); } @@ -46,8 +70,8 @@ public class JSONMapper { return "[" + eventListJSON + "]"; } - public static String ToJSON(Time time){ - if(time == null){ + public static String timeToJSON(Time time) { + if (time == null) { return "null"; } diff --git a/server/src/main/java/com/vpr/server/repository/EventRepository.java b/server/src/main/java/com/vpr/server/repository/EventRepository.java index 2b9d51e..e76e91d 100644 --- a/server/src/main/java/com/vpr/server/repository/EventRepository.java +++ b/server/src/main/java/com/vpr/server/repository/EventRepository.java @@ -78,7 +78,4 @@ public interface EventRepository extends CrudRepository { nativeQuery = true ) void deleteById(long id); - - //@Query(nativeQuery = true) - //List findEventsInDateRange(Long userId, String startDate, String endDate); } \ No newline at end of file