Fixed named query
This commit is contained in:
parent
37d275d537
commit
35e7350962
@ -3,6 +3,8 @@ package com.vpr.server.controller;
|
|||||||
import com.vpr.server.data.Event;
|
import com.vpr.server.data.Event;
|
||||||
import com.vpr.server.data.User;
|
import com.vpr.server.data.User;
|
||||||
import com.vpr.server.data.UserEvent;
|
import com.vpr.server.data.UserEvent;
|
||||||
|
import com.vpr.server.dao.interfaces.EventDAO;
|
||||||
|
import com.vpr.server.json.EventJSONMapper;
|
||||||
import com.vpr.server.repository.EventRepository;
|
import com.vpr.server.repository.EventRepository;
|
||||||
import com.vpr.server.repository.UserEventRepository;
|
import com.vpr.server.repository.UserEventRepository;
|
||||||
import com.vpr.server.repository.UserRepository;
|
import com.vpr.server.repository.UserRepository;
|
||||||
@ -11,12 +13,10 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
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.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(path = "/event")
|
@RequestMapping(path = "/event")
|
||||||
@ -28,6 +28,9 @@ public class EventController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserEventRepository userEventRepository;
|
private UserEventRepository userEventRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EventDAO eventDAO;
|
||||||
|
|
||||||
/******************
|
/******************
|
||||||
* POST-ENDPOINTS *
|
* POST-ENDPOINTS *
|
||||||
******************/
|
******************/
|
||||||
@ -130,6 +133,7 @@ public class EventController {
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
@PostMapping(path = "/all")
|
@PostMapping(path = "/all")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
List<Event> getAllEvents(
|
List<Event> getAllEvents(
|
||||||
@ -139,6 +143,7 @@ public class EventController {
|
|||||||
) {
|
) {
|
||||||
return eventRepository.findEventsInDateRange(userId, startDate, endDate);
|
return eventRepository.findEventsInDateRange(userId, startDate, endDate);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
@PostMapping(path = "/edit")
|
@PostMapping(path = "/edit")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
@ -147,7 +152,11 @@ public class EventController {
|
|||||||
@RequestParam Long userId,
|
@RequestParam Long userId,
|
||||||
@RequestParam String date
|
@RequestParam String date
|
||||||
) {
|
) {
|
||||||
EventRepository.UserEventInterface userEvent = eventRepository.findUserEventByEventIdUserIdAndDate(eventId, userId, date);
|
//EventRepository.UserEventInterface userEvent = eventRepository.findUserEventByEventIdUserIdAndDate(eventId, userId, date);
|
||||||
return "Length: " + userEvent.getDate();
|
//List<Event> userEvent = eventRepository.findByNativeQuery();
|
||||||
|
List<Event> eventList = eventDAO.getAllEvents();
|
||||||
|
|
||||||
|
return EventJSONMapper.ToJSON(eventList);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.vpr.server.dao.implementation;
|
||||||
|
|
||||||
|
import com.vpr.server.dao.interfaces.EventDAO;
|
||||||
|
import com.vpr.server.data.Event;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
@Transactional
|
||||||
|
public class EventDAOImplementation implements EventDAO {
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager manager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Event> getAllEvents() {
|
||||||
|
return manager.createNamedQuery("getAllEvents", Event.class).getResultList();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.vpr.server.dao.interfaces;
|
||||||
|
|
||||||
|
import com.vpr.server.data.Event;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface EventDAO {
|
||||||
|
List<Event> getAllEvents();
|
||||||
|
}
|
@ -1,10 +1,12 @@
|
|||||||
package com.vpr.server.data;
|
package com.vpr.server.data;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
import java.io.Serializable;
|
||||||
import java.sql.Time;
|
import java.sql.Time;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@NamedNativeQuery(name = "Event.findEventsInDateRange",
|
/*
|
||||||
|
@NamedNativeQuery(name = "findEventsInDateRange",
|
||||||
query = "SELECT e.id as id, e.name as name, e.priority as priority, e.is_full_day as isFullDay, " +
|
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 " +
|
"is_private as isPrivate, e.start as start, e.end as end " +
|
||||||
"FROM event e " +
|
"FROM event e " +
|
||||||
@ -28,8 +30,17 @@ import java.util.List;
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
*/
|
||||||
@Entity(name = "Event") // @Entity creates a table out of this class with Hibernate
|
@Entity(name = "Event") // @Entity creates a table out of this class with Hibernate
|
||||||
public class Event {
|
@Table(name = "event")
|
||||||
|
@NamedNativeQueries({
|
||||||
|
@NamedNativeQuery(
|
||||||
|
name = "getAllEvents",
|
||||||
|
query = "SELECT * FROM event",
|
||||||
|
resultClass = Event.class
|
||||||
|
)
|
||||||
|
})
|
||||||
|
public class Event implements Serializable {
|
||||||
// Generate the primary key
|
// Generate the primary key
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
package com.vpr.server.entries;
|
|
||||||
|
|
||||||
import com.vpr.server.data.Event;
|
|
||||||
|
|
||||||
import javax.persistence.*;
|
|
||||||
|
|
||||||
public class EventEntry {
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,58 @@
|
|||||||
|
package com.vpr.server.json;
|
||||||
|
|
||||||
|
import com.vpr.server.data.Event;
|
||||||
|
import com.vpr.server.data.UserEvent;
|
||||||
|
import java.sql.Time;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class EventJSONMapper {
|
||||||
|
public static List<String> ToJSON(Event event){
|
||||||
|
List<String> eventListJSON = new ArrayList<>();
|
||||||
|
|
||||||
|
for (UserEvent userEvent : event.getUserEvent()) {
|
||||||
|
|
||||||
|
String eventJSON = "{" +
|
||||||
|
"\"ownerId\": " +
|
||||||
|
userEvent.getUser().getId() +
|
||||||
|
", \"ownerId\": \"" +
|
||||||
|
userEvent.getUser().getName() +
|
||||||
|
", \"date\": \"" +
|
||||||
|
userEvent.getDate() +
|
||||||
|
"\", " +
|
||||||
|
"\"id\": " + event.getId() + "," +
|
||||||
|
"\"name\": \"" + event.getName() + "\"," +
|
||||||
|
"\"priority\": " + event.getPriority() + "," +
|
||||||
|
"\"start\": " + ToJSON(event.getStart()) + "," +
|
||||||
|
"\"end\": " + ToJSON(event.getEnd()) +
|
||||||
|
"}";
|
||||||
|
|
||||||
|
eventListJSON.add(eventJSON);
|
||||||
|
}
|
||||||
|
|
||||||
|
return eventListJSON;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String ToJSON(List<Event> eventList){
|
||||||
|
StringBuilder eventListJSON = new StringBuilder();
|
||||||
|
for(Event event : eventList){
|
||||||
|
List<String> eventsJSON = ToJSON(event);
|
||||||
|
for(String eventJSON : eventsJSON){
|
||||||
|
eventListJSON.append(", ");
|
||||||
|
eventListJSON.append(eventJSON);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
eventListJSON.delete(0, 2);
|
||||||
|
|
||||||
|
return "[" + eventListJSON + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String ToJSON(Time time){
|
||||||
|
if(time == null){
|
||||||
|
return "null";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "\"" + time + "\"";
|
||||||
|
}
|
||||||
|
}
|
@ -56,7 +56,7 @@ public interface EventRepository extends CrudRepository<Event, Integer> {
|
|||||||
)
|
)
|
||||||
UserEventInterface findUserEventByEventIdUserIdAndDate(long eventId, long userId, String date);
|
UserEventInterface findUserEventByEventIdUserIdAndDate(long eventId, long userId, String date);
|
||||||
|
|
||||||
public interface UserEventInterface{
|
interface UserEventInterface{
|
||||||
long getEventId();
|
long getEventId();
|
||||||
long getUserId();
|
long getUserId();
|
||||||
long getDate();
|
long getDate();
|
||||||
@ -79,9 +79,6 @@ public interface EventRepository extends CrudRepository<Event, Integer> {
|
|||||||
)
|
)
|
||||||
void deleteById(long id);
|
void deleteById(long id);
|
||||||
|
|
||||||
|
//@Query(nativeQuery = true)
|
||||||
@Query(nativeQuery = true)
|
//List<Event> findEventsInDateRange(Long userId, String startDate, String endDate);
|
||||||
List<Event> findEventsInDateRange(Long userId, String startDate, String endDate);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user