From 768c57a234e4485b5156d56717781d4768c2b43f Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Sat, 27 Nov 2021 17:27:04 +0100 Subject: [PATCH] Added new native queries --- .../src/main/java/com/vpr/server/Event.java | 9 ++++++++ .../java/com/vpr/server/EventRepository.java | 14 +++++++++++-- .../java/com/vpr/server/MainController.java | 21 +++++++------------ .../java/com/vpr/server/UserRepository.java | 8 ++++++- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/server/src/main/java/com/vpr/server/Event.java b/server/src/main/java/com/vpr/server/Event.java index 15e7e4f..2ba66d5 100644 --- a/server/src/main/java/com/vpr/server/Event.java +++ b/server/src/main/java/com/vpr/server/Event.java @@ -15,6 +15,7 @@ public class Event { private String name; private Integer priority; private boolean isFullDay; + private boolean isPrivate; private Time start; private Time end; @@ -57,6 +58,14 @@ public class Event { isFullDay = fullDay; } + public boolean isPrivate() { + return isPrivate; + } + + public void setPrivate(boolean aPrivate) { + isPrivate = aPrivate; + } + public Time getStart() { return start; } diff --git a/server/src/main/java/com/vpr/server/EventRepository.java b/server/src/main/java/com/vpr/server/EventRepository.java index 697b224..d3f1c77 100644 --- a/server/src/main/java/com/vpr/server/EventRepository.java +++ b/server/src/main/java/com/vpr/server/EventRepository.java @@ -14,7 +14,17 @@ public interface EventRepository extends CrudRepository { "INNER JOIN user_event ue " + "ON e.id = ue.event_id " + "INNER JOIN user u " + - "ON ue.user_id = u.id", + "ON ue.user_id = u.id " + + "WHERE u.id = ?1 " + + "OR e.is_private = 0", nativeQuery = true) - List test(); + Object[] findAllVisibleByUserId(long id); + + @Query(value = "SELECT * " + + "FROM event e " + + "INNER JOIN user_event ue " + + "ON e.id = ue.event_id " + + "WHERE ue.user_id = ?1", + nativeQuery = true) + Object[] findAllByUserId(long id); } \ No newline at end of file diff --git a/server/src/main/java/com/vpr/server/MainController.java b/server/src/main/java/com/vpr/server/MainController.java index 06f85cc..d7bafc6 100644 --- a/server/src/main/java/com/vpr/server/MainController.java +++ b/server/src/main/java/com/vpr/server/MainController.java @@ -47,26 +47,21 @@ public class MainController { // GET-request at /all-users // returns JSON-data @GetMapping(path="/all-users") - public @ResponseBody Iterable getAllUsers() { - return userRepository.findAll(); + public @ResponseBody Object[] getAllUsers() { + return userRepository.findAllUsernames(); } - // POST-request at /users-by-name + // POST-request at /all-events // returns JSON-data - @PostMapping(path="/users-by-name") - public @ResponseBody Iterable getUsersByName(@RequestParam String name) { - return userRepository.findByName(name); + @PostMapping(path="/all-events") + public @ResponseBody Object[] getAllEvents(@RequestParam long userId) { + return eventRepository.findAllVisibleByUserId(userId); } // GET-request at /all-events // returns JSON-data - @GetMapping(path="/all-events") - public @ResponseBody Iterable getAllEvents() { + @GetMapping(path="/all-events-test") + public @ResponseBody Iterable getAllEventsTest() { return eventRepository.findAll(); } - - @GetMapping(path="/test") - public @ResponseBody Iterable getTest() { - return eventRepository.test(); - } } \ No newline at end of file diff --git a/server/src/main/java/com/vpr/server/UserRepository.java b/server/src/main/java/com/vpr/server/UserRepository.java index 4215f17..010bea3 100644 --- a/server/src/main/java/com/vpr/server/UserRepository.java +++ b/server/src/main/java/com/vpr/server/UserRepository.java @@ -1,5 +1,6 @@ package com.vpr.server; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import java.util.List; @@ -8,5 +9,10 @@ import java.util.List; // CRUD refers Create, Read, Update, Delete public interface UserRepository extends CrudRepository { - List findByName(String name); + + @Query(value = "SELECT u.id, u.name, u.forename " + + "FROM user u", + nativeQuery = true) + Object[] findAllUsernames(); + } \ No newline at end of file