From 35e73509626960f5b123a3d152ab59364e861f06 Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Tue, 18 Jan 2022 15:04:13 +0100 Subject: [PATCH] Fixed named query --- .../server/controller/EventController.java | 17 ++++-- .../EventDAOImplementation.java | 23 ++++++++ .../vpr/server/dao/interfaces/EventDAO.java | 9 +++ .../main/java/com/vpr/server/data/Event.java | 15 ++++- .../com/vpr/server/entries/EventEntry.java | 9 --- .../com/vpr/server/json/EventJSONMapper.java | 58 +++++++++++++++++++ .../server/repository/EventRepository.java | 9 +-- 7 files changed, 119 insertions(+), 21 deletions(-) create mode 100644 server/src/main/java/com/vpr/server/dao/implementation/EventDAOImplementation.java create mode 100644 server/src/main/java/com/vpr/server/dao/interfaces/EventDAO.java delete mode 100644 server/src/main/java/com/vpr/server/entries/EventEntry.java create mode 100644 server/src/main/java/com/vpr/server/json/EventJSONMapper.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 7c98bee..b168eff 100644 --- a/server/src/main/java/com/vpr/server/controller/EventController.java +++ b/server/src/main/java/com/vpr/server/controller/EventController.java @@ -3,6 +3,8 @@ package com.vpr.server.controller; import com.vpr.server.data.Event; import com.vpr.server.data.User; import com.vpr.server.data.UserEvent; +import com.vpr.server.dao.interfaces.EventDAO; +import com.vpr.server.json.EventJSONMapper; import com.vpr.server.repository.EventRepository; import com.vpr.server.repository.UserEventRepository; import com.vpr.server.repository.UserRepository; @@ -11,12 +13,10 @@ 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.sql.Time; import java.text.SimpleDateFormat; import java.util.List; -import java.util.Optional; @Controller @RequestMapping(path = "/event") @@ -28,6 +28,9 @@ public class EventController { @Autowired private UserEventRepository userEventRepository; + @Autowired + private EventDAO eventDAO; + /****************** * POST-ENDPOINTS * ******************/ @@ -130,6 +133,7 @@ public class EventController { */ } + /* @PostMapping(path = "/all") public @ResponseBody List getAllEvents( @@ -139,6 +143,7 @@ public class EventController { ) { return eventRepository.findEventsInDateRange(userId, startDate, endDate); } + */ @PostMapping(path = "/edit") public @ResponseBody @@ -147,7 +152,11 @@ public class EventController { @RequestParam Long userId, @RequestParam String date ) { - EventRepository.UserEventInterface userEvent = eventRepository.findUserEventByEventIdUserIdAndDate(eventId, userId, date); - return "Length: " + userEvent.getDate(); + //EventRepository.UserEventInterface userEvent = eventRepository.findUserEventByEventIdUserIdAndDate(eventId, userId, date); + //List userEvent = eventRepository.findByNativeQuery(); + List eventList = eventDAO.getAllEvents(); + + return EventJSONMapper.ToJSON(eventList); } + } diff --git a/server/src/main/java/com/vpr/server/dao/implementation/EventDAOImplementation.java b/server/src/main/java/com/vpr/server/dao/implementation/EventDAOImplementation.java new file mode 100644 index 0000000..68d028a --- /dev/null +++ b/server/src/main/java/com/vpr/server/dao/implementation/EventDAOImplementation.java @@ -0,0 +1,23 @@ +package com.vpr.server.dao.implementation; + +import com.vpr.server.dao.interfaces.EventDAO; +import com.vpr.server.data.Event; +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 EventDAOImplementation implements EventDAO { + + @PersistenceContext + private EntityManager manager; + + @Override + public List getAllEvents() { + return manager.createNamedQuery("getAllEvents", Event.class).getResultList(); + } +} diff --git a/server/src/main/java/com/vpr/server/dao/interfaces/EventDAO.java b/server/src/main/java/com/vpr/server/dao/interfaces/EventDAO.java new file mode 100644 index 0000000..7f51211 --- /dev/null +++ b/server/src/main/java/com/vpr/server/dao/interfaces/EventDAO.java @@ -0,0 +1,9 @@ +package com.vpr.server.dao.interfaces; + +import com.vpr.server.data.Event; + +import java.util.List; + +public interface EventDAO { + List getAllEvents(); +} diff --git a/server/src/main/java/com/vpr/server/data/Event.java b/server/src/main/java/com/vpr/server/data/Event.java index 3fe17ad..744a6ca 100644 --- a/server/src/main/java/com/vpr/server/data/Event.java +++ b/server/src/main/java/com/vpr/server/data/Event.java @@ -1,10 +1,12 @@ package com.vpr.server.data; import javax.persistence.*; +import java.io.Serializable; import java.sql.Time; import java.util.List; -@NamedNativeQuery(name = "Event.findEventsInDateRange", +/* +@NamedNativeQuery(name = "findEventsInDateRange", query = "SELECT e.id as id, e.name as name, e.priority as priority, e.is_full_day as isFullDay, " + "is_private as isPrivate, e.start as start, e.end as end " + "FROM event e " + @@ -28,8 +30,17 @@ import java.util.List; } ) ) +*/ @Entity(name = "Event") // @Entity creates a table out of this class with Hibernate -public class Event { +@Table(name = "event") +@NamedNativeQueries({ + @NamedNativeQuery( + name = "getAllEvents", + query = "SELECT * FROM event", + resultClass = Event.class + ) +}) +public class Event implements Serializable { // Generate the primary key @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/server/src/main/java/com/vpr/server/entries/EventEntry.java b/server/src/main/java/com/vpr/server/entries/EventEntry.java deleted file mode 100644 index 2baf4e0..0000000 --- a/server/src/main/java/com/vpr/server/entries/EventEntry.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.vpr.server.entries; - -import com.vpr.server.data.Event; - -import javax.persistence.*; - -public class EventEntry { - -} diff --git a/server/src/main/java/com/vpr/server/json/EventJSONMapper.java b/server/src/main/java/com/vpr/server/json/EventJSONMapper.java new file mode 100644 index 0000000..a56cb19 --- /dev/null +++ b/server/src/main/java/com/vpr/server/json/EventJSONMapper.java @@ -0,0 +1,58 @@ +package com.vpr.server.json; + +import com.vpr.server.data.Event; +import com.vpr.server.data.UserEvent; +import java.sql.Time; + +import java.util.ArrayList; +import java.util.List; + +public class EventJSONMapper { + public static List ToJSON(Event event){ + List eventListJSON = new ArrayList<>(); + + for (UserEvent userEvent : event.getUserEvent()) { + + String eventJSON = "{" + + "\"ownerId\": " + + userEvent.getUser().getId() + + ", \"ownerId\": \"" + + userEvent.getUser().getName() + + ", \"date\": \"" + + userEvent.getDate() + + "\", " + + "\"id\": " + event.getId() + "," + + "\"name\": \"" + event.getName() + "\"," + + "\"priority\": " + event.getPriority() + "," + + "\"start\": " + ToJSON(event.getStart()) + "," + + "\"end\": " + ToJSON(event.getEnd()) + + "}"; + + eventListJSON.add(eventJSON); + } + + return eventListJSON; + } + + public static String ToJSON(List eventList){ + StringBuilder eventListJSON = new StringBuilder(); + for(Event event : eventList){ + List eventsJSON = ToJSON(event); + for(String eventJSON : eventsJSON){ + eventListJSON.append(", "); + eventListJSON.append(eventJSON); + } + } + eventListJSON.delete(0, 2); + + return "[" + eventListJSON + "]"; + } + + public static String ToJSON(Time time){ + if(time == null){ + return "null"; + } + + return "\"" + time + "\""; + } +} 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 d3b2285..0754a44 100644 --- a/server/src/main/java/com/vpr/server/repository/EventRepository.java +++ b/server/src/main/java/com/vpr/server/repository/EventRepository.java @@ -56,7 +56,7 @@ public interface EventRepository extends CrudRepository { ) UserEventInterface findUserEventByEventIdUserIdAndDate(long eventId, long userId, String date); - public interface UserEventInterface{ + interface UserEventInterface{ long getEventId(); long getUserId(); long getDate(); @@ -79,9 +79,6 @@ public interface EventRepository extends CrudRepository { ) void deleteById(long id); - - @Query(nativeQuery = true) - List findEventsInDateRange(Long userId, String startDate, String endDate); - - + //@Query(nativeQuery = true) + //List findEventsInDateRange(Long userId, String startDate, String endDate); } \ No newline at end of file