An API call to Spring is returning 0 for the courseid
field of an Event
entitiy, all other fields return the correct value.
in EventController
@GetMapping("/events")
Page<EventVM> getEvents(Pageable page) {
return eventService.getEvents(page).map(EventVM::new);
}
EventService
public Page<Event> getEvents(Pageable pageable ) {
return eventRepository.findAll(pageable);
}
then if I try this request in postman I get
{
"id": 453,
"eventname": "This is new",
"courseid": 0,
"date": "2021-12-12",
"maxentrants": 12,
"cost": 20.00,
"eventtype": "4BBB",
"qualifier": false,
"info": "This is a test from react",
"winner": null
}
so courseid
is showing at 0 when in fact it is 241
To ensure I can get this value I made a simple getEvents in EventController,
@GetMapping("management/events")
List<Event> getEvents() {
return eventRepository.findAll();
}
This returns all the values inc courseid
correctly.
courseid
in the Event
table is a foreign key to id
in the Course
table. I thought this may have been the issue but it worked fine when I made the simple getEvents call.
Edited
com.hoaxify.hoaxify.event.vm;
import com.hoaxify.hoaxify.event.Event;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
@Data
@NoArgsConstructor
public class EventVM {
private Long id;
private String eventname;
private long courseid;
private String date;
private int maxentrants;
private BigDecimal cost;
private String eventtype;
private Boolean qualifier;
private String info;
private String winner;
public EventVM(Event event) {
this.setId(event.getEventid());
this.setEventname(event.getEventname());
this.setEventtype(event.getEventtype());
this.setDate(event.getDate());
this.setCost(event.getCost());
this.setInfo(event.getInfo());
this.setQualifier(event.getQualifier());
this.setMaxentrants(event.getMaxentrants());
this.setWinner(event.getWinner());
}
}
CodePudding user response:
Assuming Event
has a courseid
attribute then you need to fix EventVM
constructor as follows:
public EventVM(Event event) {
this.setId(event.getEventid());
this.setEventname(event.getEventname());
this.setEventtype(event.getEventtype());
this.setCourseid(event.getCourseid());
this.setDate(event.getDate());
this.setCost(event.getCost());
this.setInfo(event.getInfo());
this.setQualifier(event.getQualifier());
this.setMaxentrants(event.getMaxentrants());
this.setWinner(event.getWinner());
}
Mind the added line this.setCourseid(event.getCourseid());
.