From 37d275d537c430be832ceb397cc59bd68e693422 Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Tue, 18 Jan 2022 08:18:58 +0100 Subject: [PATCH] Added named native query --- .../server/controller/EventController.java | 44 +++++++++++-------- .../main/java/com/vpr/server/data/Event.java | 41 +++++++++++++---- .../com/vpr/server/entries/EventEntry.java | 9 ++++ .../server/repository/EventRepository.java | 29 +++++++++++- 4 files changed, 94 insertions(+), 29 deletions(-) create mode 100644 server/src/main/java/com/vpr/server/entries/EventEntry.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 18ba039..7c98bee 100644 --- a/server/src/main/java/com/vpr/server/controller/EventController.java +++ b/server/src/main/java/com/vpr/server/controller/EventController.java @@ -15,6 +15,7 @@ import org.springframework.web.server.ResponseStatusException; import java.sql.Time; import java.text.SimpleDateFormat; +import java.util.List; import java.util.Optional; @Controller @@ -103,43 +104,50 @@ public class EventController { public @ResponseBody ResponseEntity delEvent( @RequestHeader("Authorization") String authorizationHeader, - @RequestParam Integer eventId + @RequestParam long eventId, + @RequestParam long userId, + @RequestParam String date ) { + System.out.println("authorizationHeader " + authorizationHeader); User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]); - if(authUser == null || authUser.isAdmin()){ + if(authUser == null || (!authUser.isAdmin() && authUser.getId() != userId)){ return new ResponseEntity<>( "Du hast keine Rechte um den Termin zu löschen", HttpStatus.UNAUTHORIZED); } - Optional event = eventRepository.findById(eventId); + EventRepository.UserEventInterface userEvent = eventRepository.findUserEventByEventIdUserIdAndDate(eventId, authUser.getId(), date); - if (event.isEmpty()){ + //Optional event = eventRepository.findById(eventId); + + if (userEvent == null){ return new ResponseEntity<>( "Der Termin exestiert nicht", HttpStatus.BAD_REQUEST); } + return new ResponseEntity<>( "Der Termin exestiert", HttpStatus.OK); - - eventRepository.deleteUserEventsById(Long.valueOf(eventId)); - eventRepository.deleteById(Long.valueOf(eventId)); +/* + eventRepository.deleteUserEventsById(eventId); + eventRepository.deleteById(eventId); return new ResponseEntity<>("", HttpStatus.OK); + */ } @PostMapping(path = "/all") public @ResponseBody - Object[] getAllEvents(@RequestParam long userId) { - return eventRepository.findAllVisibleByUserId(userId); + List getAllEvents( + @RequestParam long userId, + @RequestParam String startDate, + @RequestParam String endDate + ) { + return eventRepository.findEventsInDateRange(userId, startDate, endDate); } @PostMapping(path = "/edit") public @ResponseBody String editEvent( - @RequestParam Integer userId, - @RequestParam String date, - @RequestParam String name, - @RequestParam String start, - @RequestParam String end, - @RequestParam Integer prority, - @RequestParam Boolean isFullDay, - @RequestParam Boolean isPrivate + @RequestParam Long eventId, + @RequestParam Long userId, + @RequestParam String date ) { - return ""; + EventRepository.UserEventInterface userEvent = eventRepository.findUserEventByEventIdUserIdAndDate(eventId, userId, date); + return "Length: " + userEvent.getDate(); } } 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 773dbfb..3fe17ad 100644 --- a/server/src/main/java/com/vpr/server/data/Event.java +++ b/server/src/main/java/com/vpr/server/data/Event.java @@ -4,30 +4,53 @@ import javax.persistence.*; import java.sql.Time; import java.util.List; -// @Entity creates a table out of this class with Hibernate -@Entity(name = "Event") +@NamedNativeQuery(name = "Event.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 " + + "INNER JOIN user_event ue " + + "ON e.id = ue.event_id " + + "WHERE (ue.user_id = :userId OR e.is_private = 0) " + + "AND ue.date > :startDate " + + "AND ue.date < :endDate", + resultSetMapping = "Mapping.Event" +) +@SqlResultSetMapping(name = "Mapping.Event", + classes = @ConstructorResult(targetClass = Event.class, + columns = { + @ColumnResult(name = "id"), + @ColumnResult(name = "name"), + @ColumnResult(name = "priority"), + @ColumnResult(name = "isFullDay"), + @ColumnResult(name = "isPrivate"), + @ColumnResult(name = "start"), + @ColumnResult(name = "end") + } + ) +) +@Entity(name = "Event") // @Entity creates a table out of this class with Hibernate public class Event { // Generate the primary key @Id - @GeneratedValue(strategy=GenerationType.IDENTITY) + @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; - @Column(name="name", nullable=false) + @Column(name = "name", nullable = false) private String name; - @Column(name="priority", nullable=false) + @Column(name = "priority", nullable = false) private Integer priority; - @Column(name="is_full_day", nullable=false) + @Column(name = "is_full_day", nullable = false) private boolean isFullDay; - @Column(name="is_private", nullable=false) + @Column(name = "is_private", nullable = false) private boolean isPrivate; - @Column(name="start") + @Column(name = "start") private Time start; - @Column(name="end") + @Column(name = "end") private Time end; @OneToMany(mappedBy = "event") diff --git a/server/src/main/java/com/vpr/server/entries/EventEntry.java b/server/src/main/java/com/vpr/server/entries/EventEntry.java new file mode 100644 index 0000000..2baf4e0 --- /dev/null +++ b/server/src/main/java/com/vpr/server/entries/EventEntry.java @@ -0,0 +1,9 @@ +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/repository/EventRepository.java b/server/src/main/java/com/vpr/server/repository/EventRepository.java index a5c17d6..d3b2285 100644 --- a/server/src/main/java/com/vpr/server/repository/EventRepository.java +++ b/server/src/main/java/com/vpr/server/repository/EventRepository.java @@ -1,11 +1,18 @@ package com.vpr.server.repository; import com.vpr.server.data.Event; +import com.vpr.server.data.UserEvent; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; +import javax.persistence.ColumnResult; +import javax.persistence.ConstructorResult; +import javax.persistence.NamedNativeQuery; +import javax.persistence.SqlResultSetMapping; import javax.transaction.Transactional; +import java.util.List; +import java.util.Optional; // This will be AUTO IMPLEMENTED by Spring into a Bean called eventRepository // CRUD refers Create, Read, Update, Delete @@ -38,10 +45,22 @@ public interface EventRepository extends CrudRepository { @Query( - value = "DELETE ue FROM user_event ue WHERE ue.event_id = ?1", + value = "SELECT ue.user_id as userId, ue.event_id as eventId, ue.date as date " + + "FROM event e " + + "INNER JOIN user_event ue " + + "ON e.id = ue.event_id " + + "WHERE ue.event_id = ?1 " + + "AND ue.user_id = ?2 " + + "AND ue.date = ?3", nativeQuery = true ) - Object[] findUserIdByEventId(long id); + UserEventInterface findUserEventByEventIdUserIdAndDate(long eventId, long userId, String date); + + public interface UserEventInterface{ + long getEventId(); + long getUserId(); + long getDate(); + } @Modifying @Transactional @@ -59,4 +78,10 @@ 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