Fixed named query

This commit is contained in:
Marc Beyer 2022-01-18 15:04:13 +01:00
parent 37d275d537
commit 35e7350962
7 changed files with 119 additions and 21 deletions

View File

@ -3,6 +3,8 @@ package com.vpr.server.controller;
import com.vpr.server.data.Event;
import com.vpr.server.data.User;
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.UserEventRepository;
import com.vpr.server.repository.UserRepository;
@ -11,12 +13,10 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;
@Controller
@RequestMapping(path = "/event")
@ -28,6 +28,9 @@ public class EventController {
@Autowired
private UserEventRepository userEventRepository;
@Autowired
private EventDAO eventDAO;
/******************
* POST-ENDPOINTS *
******************/
@ -130,6 +133,7 @@ public class EventController {
*/
}
/*
@PostMapping(path = "/all")
public @ResponseBody
List<Event> getAllEvents(
@ -139,6 +143,7 @@ public class EventController {
) {
return eventRepository.findEventsInDateRange(userId, startDate, endDate);
}
*/
@PostMapping(path = "/edit")
public @ResponseBody
@ -147,7 +152,11 @@ public class EventController {
@RequestParam Long userId,
@RequestParam String date
) {
EventRepository.UserEventInterface userEvent = eventRepository.findUserEventByEventIdUserIdAndDate(eventId, userId, date);
return "Length: " + userEvent.getDate();
//EventRepository.UserEventInterface userEvent = eventRepository.findUserEventByEventIdUserIdAndDate(eventId, userId, date);
//List<Event> userEvent = eventRepository.findByNativeQuery();
List<Event> eventList = eventDAO.getAllEvents();
return EventJSONMapper.ToJSON(eventList);
}
}

View File

@ -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();
}
}

View File

@ -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();
}

View File

@ -1,10 +1,12 @@
package com.vpr.server.data;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Time;
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, " +
"is_private as isPrivate, e.start as start, e.end as end " +
"FROM event e " +
@ -28,8 +30,17 @@ import java.util.List;
}
)
)
*/
@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
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

View File

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

View File

@ -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 + "\"";
}
}

View File

@ -56,7 +56,7 @@ public interface EventRepository extends CrudRepository<Event, Integer> {
)
UserEventInterface findUserEventByEventIdUserIdAndDate(long eventId, long userId, String date);
public interface UserEventInterface{
interface UserEventInterface{
long getEventId();
long getUserId();
long getDate();
@ -79,9 +79,6 @@ public interface EventRepository extends CrudRepository<Event, Integer> {
)
void deleteById(long id);
@Query(nativeQuery = true)
List<Event> findEventsInDateRange(Long userId, String startDate, String endDate);
//@Query(nativeQuery = true)
//List<Event> findEventsInDateRange(Long userId, String startDate, String endDate);
}