Added new native queries

This commit is contained in:
Marc Beyer 2021-11-27 17:27:04 +01:00
parent 2cec62c453
commit 768c57a234
4 changed files with 36 additions and 16 deletions

View File

@ -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;
}

View File

@ -14,7 +14,17 @@ public interface EventRepository extends CrudRepository<Event, Integer> {
"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<Object> 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);
}

View File

@ -47,26 +47,21 @@ public class MainController {
// GET-request at /all-users
// returns JSON-data
@GetMapping(path="/all-users")
public @ResponseBody Iterable<com.vpr.server.User> 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<com.vpr.server.User> 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<com.vpr.server.Event> getAllEvents() {
@GetMapping(path="/all-events-test")
public @ResponseBody Iterable<com.vpr.server.Event> getAllEventsTest() {
return eventRepository.findAll();
}
@GetMapping(path="/test")
public @ResponseBody Iterable<Object> getTest() {
return eventRepository.test();
}
}

View File

@ -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<User, Integer> {
List<User> findByName(String name);
@Query(value = "SELECT u.id, u.name, u.forename " +
"FROM user u",
nativeQuery = true)
Object[] findAllUsernames();
}