Added named native query

This commit is contained in:
Marc Beyer 2022-01-18 08:18:58 +01:00
parent 3796afb712
commit 37d275d537
4 changed files with 94 additions and 29 deletions

View File

@ -15,6 +15,7 @@ import org.springframework.web.server.ResponseStatusException;
import java.sql.Time; import java.sql.Time;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@Controller @Controller
@ -103,43 +104,50 @@ public class EventController {
public @ResponseBody public @ResponseBody
ResponseEntity<String> delEvent( ResponseEntity<String> delEvent(
@RequestHeader("Authorization") String authorizationHeader, @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]); 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); return new ResponseEntity<>( "Du hast keine Rechte um den Termin zu löschen", HttpStatus.UNAUTHORIZED);
} }
Optional<Event> event = eventRepository.findById(eventId); EventRepository.UserEventInterface userEvent = eventRepository.findUserEventByEventIdUserIdAndDate(eventId, authUser.getId(), date);
if (event.isEmpty()){ //Optional<Event> event = eventRepository.findById(eventId);
if (userEvent == null){
return new ResponseEntity<>( "Der Termin exestiert nicht", HttpStatus.BAD_REQUEST); return new ResponseEntity<>( "Der Termin exestiert nicht", HttpStatus.BAD_REQUEST);
} }
return new ResponseEntity<>( "Der Termin exestiert", HttpStatus.OK);
/*
eventRepository.deleteUserEventsById(Long.valueOf(eventId)); eventRepository.deleteUserEventsById(eventId);
eventRepository.deleteById(Long.valueOf(eventId)); eventRepository.deleteById(eventId);
return new ResponseEntity<>("", HttpStatus.OK); return new ResponseEntity<>("", HttpStatus.OK);
*/
} }
@PostMapping(path = "/all") @PostMapping(path = "/all")
public @ResponseBody public @ResponseBody
Object[] getAllEvents(@RequestParam long userId) { List<Event> getAllEvents(
return eventRepository.findAllVisibleByUserId(userId); @RequestParam long userId,
@RequestParam String startDate,
@RequestParam String endDate
) {
return eventRepository.findEventsInDateRange(userId, startDate, endDate);
} }
@PostMapping(path = "/edit") @PostMapping(path = "/edit")
public @ResponseBody public @ResponseBody
String editEvent( String editEvent(
@RequestParam Integer userId, @RequestParam Long eventId,
@RequestParam String date, @RequestParam Long userId,
@RequestParam String name, @RequestParam String date
@RequestParam String start,
@RequestParam String end,
@RequestParam Integer prority,
@RequestParam Boolean isFullDay,
@RequestParam Boolean isPrivate
) { ) {
return ""; EventRepository.UserEventInterface userEvent = eventRepository.findUserEventByEventIdUserIdAndDate(eventId, userId, date);
return "Length: " + userEvent.getDate();
} }
} }

View File

@ -4,30 +4,53 @@ import javax.persistence.*;
import java.sql.Time; import java.sql.Time;
import java.util.List; import java.util.List;
// @Entity creates a table out of this class with Hibernate @NamedNativeQuery(name = "Event.findEventsInDateRange",
@Entity(name = "Event") 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 { public class Event {
// Generate the primary key // Generate the primary key
@Id @Id
@GeneratedValue(strategy=GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private long id; private long id;
@Column(name="name", nullable=false) @Column(name = "name", nullable = false)
private String name; private String name;
@Column(name="priority", nullable=false) @Column(name = "priority", nullable = false)
private Integer priority; private Integer priority;
@Column(name="is_full_day", nullable=false) @Column(name = "is_full_day", nullable = false)
private boolean isFullDay; private boolean isFullDay;
@Column(name="is_private", nullable=false) @Column(name = "is_private", nullable = false)
private boolean isPrivate; private boolean isPrivate;
@Column(name="start") @Column(name = "start")
private Time start; private Time start;
@Column(name="end") @Column(name = "end")
private Time end; private Time end;
@OneToMany(mappedBy = "event") @OneToMany(mappedBy = "event")

View File

@ -0,0 +1,9 @@
package com.vpr.server.entries;
import com.vpr.server.data.Event;
import javax.persistence.*;
public class EventEntry {
}

View File

@ -1,11 +1,18 @@
package com.vpr.server.repository; package com.vpr.server.repository;
import com.vpr.server.data.Event; 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.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; 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 javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;
// This will be AUTO IMPLEMENTED by Spring into a Bean called eventRepository // This will be AUTO IMPLEMENTED by Spring into a Bean called eventRepository
// CRUD refers Create, Read, Update, Delete // CRUD refers Create, Read, Update, Delete
@ -38,10 +45,22 @@ public interface EventRepository extends CrudRepository<Event, Integer> {
@Query( @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 nativeQuery = true
) )
Object[] findUserIdByEventId(long id); UserEventInterface findUserEventByEventIdUserIdAndDate(long eventId, long userId, String date);
public interface UserEventInterface{
long getEventId();
long getUserId();
long getDate();
}
@Modifying @Modifying
@Transactional @Transactional
@ -59,4 +78,10 @@ public interface EventRepository extends CrudRepository<Event, Integer> {
nativeQuery = true nativeQuery = true
) )
void deleteById(long id); void deleteById(long id);
@Query(nativeQuery = true)
List<Event> findEventsInDateRange(Long userId, String startDate, String endDate);
} }