Added named native query
This commit is contained in:
parent
3796afb712
commit
37d275d537
@ -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<String> 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> 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", 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<Event> 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();
|
||||
}
|
||||
}
|
||||
|
@ -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")
|
||||
|
@ -0,0 +1,9 @@
|
||||
package com.vpr.server.entries;
|
||||
|
||||
import com.vpr.server.data.Event;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
public class EventEntry {
|
||||
|
||||
}
|
@ -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<Event, Integer> {
|
||||
|
||||
|
||||
@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<Event, Integer> {
|
||||
nativeQuery = true
|
||||
)
|
||||
void deleteById(long id);
|
||||
|
||||
|
||||
@Query(nativeQuery = true)
|
||||
List<Event> findEventsInDateRange(Long userId, String startDate, String endDate);
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user