From bfbcd5c44b3247146ae7541b7048d5ec059f5b50 Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Sun, 28 Nov 2021 18:20:17 +0100 Subject: [PATCH] Added del-event endpoint --- .../main/java/com/vpr/server/EventRepository.java | 15 +++++++++++++++ .../main/java/com/vpr/server/MainController.java | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/server/src/main/java/com/vpr/server/EventRepository.java b/server/src/main/java/com/vpr/server/EventRepository.java index 7f9b86e..9ba909e 100644 --- a/server/src/main/java/com/vpr/server/EventRepository.java +++ b/server/src/main/java/com/vpr/server/EventRepository.java @@ -1,8 +1,10 @@ package com.vpr.server; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; +import javax.transaction.Transactional; import java.util.List; // This will be AUTO IMPLEMENTED by Spring into a Bean called eventRepository @@ -29,4 +31,17 @@ public interface EventRepository extends CrudRepository { "WHERE ue.user_id = ?1", nativeQuery = true) Object[] findAllByUserId(long id); + + @Modifying + @Transactional + @Query(value = "DELETE ue FROM user_event ue WHERE ue.event_id = ?1", + nativeQuery = true) + void deleteUserEventsById(long id); + + + @Modifying + @Transactional + @Query(value = "DELETE e FROM event e WHERE e.id = ?1", + nativeQuery = true) + void deleteById(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 d27751b..2f137b9 100644 --- a/server/src/main/java/com/vpr/server/MainController.java +++ b/server/src/main/java/com/vpr/server/MainController.java @@ -110,6 +110,13 @@ public class MainController { return "Saved"; } + @PostMapping(path="/del-event") + public @ResponseBody String addEvent ( @RequestParam Integer eventId ) { + eventRepository.deleteUserEventsById(Long.valueOf(eventId)); + eventRepository.deleteById(Long.valueOf(eventId)); + return "Deleted"; + } + // GET-request at /all-users // returns JSON-data @GetMapping(path="/all-users")