Add data model
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package be.naaturel.letsmeet.controllers;
|
package be.naaturel.letsmeet.controllers;
|
||||||
|
|
||||||
import be.naaturel.letsmeet.models.Event;
|
import be.naaturel.letsmeet.dto.httpRequest.EventDTO;
|
||||||
|
import be.naaturel.letsmeet.services.EventService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
@@ -11,12 +12,21 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
@RestController
|
@RestController
|
||||||
public class EventController {
|
public class EventController {
|
||||||
|
|
||||||
|
private final EventService service;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public EventController(){
|
public EventController(EventService service){
|
||||||
|
this.service = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping({"/create", "/create/"})
|
@PostMapping({"/create", "/create/"})
|
||||||
public ResponseEntity<?> submit(@RequestBody Event e){
|
public ResponseEntity<?> submit(@RequestBody EventDTO dto){
|
||||||
|
|
||||||
|
try{
|
||||||
|
service.save(dto);
|
||||||
|
} catch (Exception e){
|
||||||
|
return ResponseEntity.internalServerError().build();
|
||||||
|
}
|
||||||
|
|
||||||
return ResponseEntity.ok().build();
|
return ResponseEntity.ok().build();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package be.naaturel.letsmeet.dto.database;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.models.EventDate;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Entity(name = "Dates")
|
||||||
|
@Table(uniqueConstraints = {
|
||||||
|
@UniqueConstraint(columnNames = {"event_id", "timestamp"})
|
||||||
|
})
|
||||||
|
public class EventDateEntity {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.UUID)
|
||||||
|
public String id;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
public long timeStamp;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(nullable=true)
|
||||||
|
public EventEntity event;
|
||||||
|
|
||||||
|
@ManyToMany(mappedBy = "dates")
|
||||||
|
public Set<ParticipantEntity> participants;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj) return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass()) return false;
|
||||||
|
EventDateEntity date = (EventDateEntity) obj;
|
||||||
|
return Objects.equals(timeStamp, date.timeStamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(timeStamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package be.naaturel.letsmeet.dto.database;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.models.EventDate;
|
||||||
|
import be.naaturel.letsmeet.models.Participant;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Entity(name = "Events")
|
||||||
|
public class EventEntity {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.UUID)
|
||||||
|
public String id;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
@OneToMany(targetEntity=EventDateEntity.class, cascade=CascadeType.ALL, mappedBy="event")
|
||||||
|
public Set<EventDateEntity> dates;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
@OneToMany(targetEntity=ParticipantEntity.class, cascade=CascadeType.ALL, mappedBy="event")
|
||||||
|
public Set<ParticipantEntity> participants;
|
||||||
|
|
||||||
|
public void prepareForSave(){
|
||||||
|
linkDates();
|
||||||
|
linkParticipants();
|
||||||
|
removeDuplicatedDates();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void linkDates(){
|
||||||
|
for (EventDateEntity date : this.dates) {
|
||||||
|
date.event = this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void linkParticipants(){
|
||||||
|
for (ParticipantEntity participant : this.participants) {
|
||||||
|
participant.event = this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeDuplicatedDates(){
|
||||||
|
|
||||||
|
for (EventDateEntity ede: dates) {
|
||||||
|
for (ParticipantEntity pe : participants) {
|
||||||
|
if(pe.dates.contains(ede)){
|
||||||
|
pe.dates.remove(ede);
|
||||||
|
pe.dates.add(ede);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package be.naaturel.letsmeet.dto.database;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class ParticipantEntity {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.UUID)
|
||||||
|
public String id;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
@ManyToMany(cascade = {CascadeType.ALL})
|
||||||
|
@JoinTable
|
||||||
|
public Set<EventDateEntity> dates;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(nullable=true)
|
||||||
|
public EventEntity event;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package be.naaturel.letsmeet.dto.httpRequest;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class EventDTO {
|
||||||
|
|
||||||
|
public String name;
|
||||||
|
public Set<ParticipantDTO> participants;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package be.naaturel.letsmeet.dto.httpRequest;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.models.EventDate;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class EventDateDTO {
|
||||||
|
|
||||||
|
public long timestamp;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj) return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass()) return false;
|
||||||
|
EventDateDTO date = (EventDateDTO) obj;
|
||||||
|
return Objects.equals(timestamp, date.timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package be.naaturel.letsmeet.dto.httpRequest;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class ParticipantDTO {
|
||||||
|
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
public Set<EventDateDTO> dates;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
package be.naaturel.letsmeet.entities;
|
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static jakarta.persistence.CascadeType.ALL;
|
|
||||||
|
|
||||||
@Entity(name = "Dates")
|
|
||||||
public class DateEntity {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.UUID)
|
|
||||||
public String id;
|
|
||||||
|
|
||||||
@Column
|
|
||||||
public long timeStamp;
|
|
||||||
|
|
||||||
@ManyToOne
|
|
||||||
@JoinColumn(name="eventId", nullable=false)
|
|
||||||
public EventEntity event;
|
|
||||||
|
|
||||||
@OneToMany(targetEntity= ParticipantEntity.class, cascade=ALL, mappedBy="id")
|
|
||||||
public Set<ParticipantEntity> participants;
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package be.naaturel.letsmeet.entities;
|
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static jakarta.persistence.CascadeType.ALL;
|
|
||||||
|
|
||||||
@Entity(name = "Events")
|
|
||||||
public class EventEntity {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.UUID)
|
|
||||||
public String id;
|
|
||||||
|
|
||||||
@Column
|
|
||||||
public String name;
|
|
||||||
|
|
||||||
@Column
|
|
||||||
@OneToMany(targetEntity= DateEntity.class, cascade=ALL, mappedBy="event")
|
|
||||||
public Set<DateEntity> dates;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package be.naaturel.letsmeet.entities;
|
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
|
|
||||||
@Entity(name = "Participants")
|
|
||||||
public class ParticipantEntity {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.UUID)
|
|
||||||
public String id;
|
|
||||||
|
|
||||||
@Column
|
|
||||||
public String name;
|
|
||||||
|
|
||||||
@ManyToOne
|
|
||||||
@JoinColumn(name="dateId", nullable=false)
|
|
||||||
public DateEntity date;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
package be.naaturel.letsmeet.mappers;
|
|
||||||
|
|
||||||
import be.naaturel.letsmeet.entities.DateEntity;
|
|
||||||
import be.naaturel.letsmeet.models.Date;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
|
|
||||||
public class DateMapper implements Mapper<Date, DateEntity> {
|
|
||||||
@Override
|
|
||||||
public DateEntity toEntity(Date d) {
|
|
||||||
DateEntity de = new DateEntity();
|
|
||||||
de.timeStamp = d.timeStamp;
|
|
||||||
return de;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Date toModel(DateEntity d) {
|
|
||||||
return new Date(d.timeStamp);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <C extends Collection<DateEntity>> C toEntities(Collection<Date> e, Supplier<C> collectionSupplier) {
|
|
||||||
C result = collectionSupplier.get();
|
|
||||||
e.forEach(r -> result.add(toEntity(r)));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <C extends Collection<Date>> C toModels(Collection<DateEntity> e, Supplier<C> collectionSupplier) {
|
|
||||||
C result = collectionSupplier.get();
|
|
||||||
e.forEach(r -> result.add(toModel(r)));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
package be.naaturel.letsmeet.mappers;
|
|
||||||
|
|
||||||
import be.naaturel.letsmeet.entities.DateEntity;
|
|
||||||
import be.naaturel.letsmeet.entities.EventEntity;
|
|
||||||
import be.naaturel.letsmeet.models.Date;
|
|
||||||
import be.naaturel.letsmeet.models.Event;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
public class EventMapper implements Mapper<Event, EventEntity> {
|
|
||||||
|
|
||||||
private final Mapper<Date, DateEntity> dateMapper;
|
|
||||||
|
|
||||||
public EventMapper(){
|
|
||||||
this.dateMapper = new DateMapper();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public EventEntity toEntity(Event event) {
|
|
||||||
EventEntity ee = new EventEntity();
|
|
||||||
ee.name = event.getName();
|
|
||||||
ee.dates = dateMapper.toEntities(event.getDates(), HashSet::new);
|
|
||||||
return ee;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Event toModel(EventEntity eventEntity) {
|
|
||||||
return new Event(eventEntity.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <C extends Collection<EventEntity>> C toEntities(Collection<Event> events, Supplier<C> collectionSupplier) {
|
|
||||||
C result = collectionSupplier.get();
|
|
||||||
events.forEach(e -> result.add(toEntity(e)));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <C extends Collection<Event>> C toModels(Collection<EventEntity> eventEntities, Supplier<C> collectionSupplier) {
|
|
||||||
C result = collectionSupplier.get();
|
|
||||||
eventEntities.forEach(e -> result.add(toModel(e)));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package be.naaturel.letsmeet.mappers.database;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.dto.database.EventDateEntity;
|
||||||
|
import be.naaturel.letsmeet.mappers.Mapper;
|
||||||
|
import be.naaturel.letsmeet.models.EventDate;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
|
||||||
|
public class EventDateMapper implements Mapper<EventDate, EventDateEntity> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EventDateEntity toEntity(EventDate d) {
|
||||||
|
EventDateEntity de = new EventDateEntity();
|
||||||
|
de.timeStamp = d.getTimeStamp();
|
||||||
|
return de;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EventDate toModel(EventDateEntity d) {
|
||||||
|
return new EventDate(d.timeStamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends Collection<EventDateEntity>> C toEntities(Collection<EventDate> dates, Supplier<C> collectionSupplier) {
|
||||||
|
C result = collectionSupplier.get();
|
||||||
|
dates.forEach(d -> result.add(toEntity(d)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends Collection<EventDate>> C toModels(Collection<EventDateEntity> e, Supplier<C> collectionSupplier) {
|
||||||
|
C result = collectionSupplier.get();
|
||||||
|
e.forEach(r -> result.add(toModel(r)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package be.naaturel.letsmeet.mappers.database;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.dto.database.EventEntity;
|
||||||
|
import be.naaturel.letsmeet.dto.database.ParticipantEntity;
|
||||||
|
import be.naaturel.letsmeet.mappers.Mapper;
|
||||||
|
import be.naaturel.letsmeet.models.Event;
|
||||||
|
import be.naaturel.letsmeet.models.Participant;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class EventMapper implements Mapper<Event, EventEntity> {
|
||||||
|
|
||||||
|
private final Mapper<Participant, ParticipantEntity> participantMapper;
|
||||||
|
|
||||||
|
public EventMapper(){
|
||||||
|
this.participantMapper = new ParticipantMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EventEntity toEntity(Event event) {
|
||||||
|
EventEntity eventEntity = new EventEntity();
|
||||||
|
eventEntity.name = event.getName();
|
||||||
|
eventEntity.participants = participantMapper.toEntities(event.getParticipants(), HashSet::new);
|
||||||
|
|
||||||
|
eventEntity.dates = new HashSet<>();
|
||||||
|
for (ParticipantEntity pe : eventEntity.participants) {
|
||||||
|
eventEntity.dates.addAll(pe.dates);
|
||||||
|
}
|
||||||
|
return eventEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Event toModel(EventEntity eventEntity) {
|
||||||
|
return new Event(eventEntity.name, participantMapper.toModels(eventEntity.participants, HashSet::new));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends Collection<EventEntity>> C toEntities(Collection<Event> events, Supplier<C> collectionSupplier) {
|
||||||
|
C result = collectionSupplier.get();
|
||||||
|
events.forEach(e -> result.add(toEntity(e)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends Collection<Event>> C toModels(Collection<EventEntity> eventEntities, Supplier<C> collectionSupplier) {
|
||||||
|
C result = collectionSupplier.get();
|
||||||
|
eventEntities.forEach(e -> result.add(toModel(e)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package be.naaturel.letsmeet.mappers.database;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.dto.database.EventDateEntity;
|
||||||
|
import be.naaturel.letsmeet.dto.database.ParticipantEntity;
|
||||||
|
import be.naaturel.letsmeet.mappers.Mapper;
|
||||||
|
import be.naaturel.letsmeet.models.EventDate;
|
||||||
|
import be.naaturel.letsmeet.models.Participant;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class ParticipantMapper implements Mapper<Participant, ParticipantEntity> {
|
||||||
|
|
||||||
|
|
||||||
|
private final Mapper<EventDate, EventDateEntity> dateMapper;
|
||||||
|
|
||||||
|
public ParticipantMapper(){
|
||||||
|
dateMapper = new EventDateMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ParticipantEntity toEntity(Participant d) {
|
||||||
|
ParticipantEntity pe = new ParticipantEntity();
|
||||||
|
pe.name = d.getName();
|
||||||
|
pe.dates = dateMapper.toEntities(d.getDates(), HashSet::new);
|
||||||
|
return pe;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Participant toModel(ParticipantEntity d) {
|
||||||
|
return new Participant(d.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends Collection<ParticipantEntity>> C toEntities(Collection<Participant> participants, Supplier<C> collectionSupplier) {
|
||||||
|
C result = collectionSupplier.get();
|
||||||
|
participants.forEach(p -> result.add(toEntity(p)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends Collection<Participant>> C toModels(Collection<ParticipantEntity> participants, Supplier<C> collectionSupplier) {
|
||||||
|
C result = collectionSupplier.get();
|
||||||
|
participants.forEach(p -> result.add(toModel(p)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package be.naaturel.letsmeet.mappers.requests;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.dto.httpRequest.EventDTO;
|
||||||
|
import be.naaturel.letsmeet.dto.httpRequest.ParticipantDTO;
|
||||||
|
import be.naaturel.letsmeet.mappers.Mapper;
|
||||||
|
import be.naaturel.letsmeet.models.Event;
|
||||||
|
import be.naaturel.letsmeet.models.Participant;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class EventDTOMapper implements Mapper<Event, EventDTO> {
|
||||||
|
|
||||||
|
private final Mapper<Participant, ParticipantDTO> participantMapper = new ParticipantDTOMapper();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EventDTO toEntity(Event event) {
|
||||||
|
EventDTO eventDTO = new EventDTO();
|
||||||
|
eventDTO.name = event.getName();
|
||||||
|
eventDTO.participants = participantMapper.toEntities(event.getParticipants(), HashSet::new);
|
||||||
|
return new EventDTO();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Event toModel(EventDTO dto) {
|
||||||
|
return new Event(dto.name, participantMapper.toModels(dto.participants, HashSet::new));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends Collection<EventDTO>> C toEntities(Collection<Event> events, Supplier<C> collectionSupplier) {
|
||||||
|
C result = collectionSupplier.get();
|
||||||
|
events.forEach(e -> result.add(toEntity(e)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends Collection<Event>> C toModels(Collection<EventDTO> eventDTOS, Supplier<C> collectionSupplier) {
|
||||||
|
C result = collectionSupplier.get();
|
||||||
|
eventDTOS.forEach(e -> result.add(toModel(e)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package be.naaturel.letsmeet.mappers.requests;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.dto.httpRequest.EventDateDTO;
|
||||||
|
import be.naaturel.letsmeet.dto.httpRequest.ParticipantDTO;
|
||||||
|
import be.naaturel.letsmeet.mappers.Mapper;
|
||||||
|
import be.naaturel.letsmeet.models.EventDate;
|
||||||
|
import be.naaturel.letsmeet.models.Participant;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class EventDateDTOMapper implements Mapper<EventDate, EventDateDTO> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EventDateDTO toEntity(EventDate d) {
|
||||||
|
EventDateDTO dto = new EventDateDTO();
|
||||||
|
dto.timestamp = d.getTimeStamp();
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EventDate toModel(EventDateDTO d) {
|
||||||
|
return new EventDate(d.timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends Collection<EventDateDTO>> C toEntities(Collection<EventDate> dates, Supplier<C> collectionSupplier) {
|
||||||
|
C result = collectionSupplier.get();
|
||||||
|
dates.forEach(d -> result.add(toEntity(d)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends Collection<EventDate>> C toModels(Collection<EventDateDTO> dates, Supplier<C> collectionSupplier) {
|
||||||
|
C result = collectionSupplier.get();
|
||||||
|
dates.forEach(d -> result.add(toModel(d)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package be.naaturel.letsmeet.mappers.requests;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.dto.httpRequest.EventDateDTO;
|
||||||
|
import be.naaturel.letsmeet.dto.httpRequest.ParticipantDTO;
|
||||||
|
import be.naaturel.letsmeet.mappers.Mapper;
|
||||||
|
import be.naaturel.letsmeet.models.EventDate;
|
||||||
|
import be.naaturel.letsmeet.models.Participant;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class ParticipantDTOMapper implements Mapper<Participant, ParticipantDTO> {
|
||||||
|
|
||||||
|
private final Mapper<EventDate, EventDateDTO> dateMapper;
|
||||||
|
|
||||||
|
public ParticipantDTOMapper(){
|
||||||
|
this.dateMapper = new EventDateDTOMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ParticipantDTO toEntity(Participant d) {
|
||||||
|
ParticipantDTO dto = new ParticipantDTO();
|
||||||
|
dto.name = d.getName();
|
||||||
|
dto.dates = dateMapper.toEntities(d.getDates(), HashSet::new);
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Participant toModel(ParticipantDTO d) {
|
||||||
|
return new Participant(d.name, dateMapper.toModels(d.dates, HashSet::new));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends Collection<ParticipantDTO>> C toEntities(Collection<Participant> participants, Supplier<C> collectionSupplier) {
|
||||||
|
C result = collectionSupplier.get();
|
||||||
|
participants.forEach(p -> result.add(toEntity(p)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends Collection<Participant>> C toModels(Collection<ParticipantDTO> participantDTOS, Supplier<C> collectionSupplier) {
|
||||||
|
C result = collectionSupplier.get();
|
||||||
|
participantDTOS.forEach(p -> result.add(toModel(p)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package be.naaturel.letsmeet.models;
|
|
||||||
|
|
||||||
|
|
||||||
public class Date {
|
|
||||||
|
|
||||||
public long timeStamp;
|
|
||||||
|
|
||||||
public Date(long timeStamp){
|
|
||||||
this.timeStamp = timeStamp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,18 +5,9 @@ import java.util.*;
|
|||||||
public class Event {
|
public class Event {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private Map<Date, Set<Participant>> participants;
|
private Set<Participant> participants;
|
||||||
|
|
||||||
public Event(String name){
|
public Event(String name, Set<Participant> participants){
|
||||||
this(name, new HashMap<>());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Event(String name, Set<Date> dates, Set<Participant> participants){
|
|
||||||
this.name = name;
|
|
||||||
this.participants = new HashMap<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Event(String name, Map<Date, Set<Participant>> participants){
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.participants = participants;
|
this.participants = participants;
|
||||||
}
|
}
|
||||||
@@ -25,7 +16,15 @@ public class Event {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Date> getDates() {
|
public Set<Participant> getParticipants(){
|
||||||
return this.participants.keySet();
|
return new HashSet<>(this.participants);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<EventDate> getDates() {
|
||||||
|
Set<EventDate> dates = new HashSet<>();
|
||||||
|
for (Participant p : this.participants) {
|
||||||
|
dates.addAll(p.getDates());
|
||||||
|
}
|
||||||
|
return dates;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package be.naaturel.letsmeet.models;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class EventDate {
|
||||||
|
|
||||||
|
private long timeStamp;
|
||||||
|
|
||||||
|
public EventDate(long timeStamp){
|
||||||
|
this.timeStamp = timeStamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getTimeStamp() {
|
||||||
|
return timeStamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,14 +1,48 @@
|
|||||||
package be.naaturel.letsmeet.models;
|
package be.naaturel.letsmeet.models;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class Participant {
|
public class Participant {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
private Set<EventDate> dates;
|
||||||
|
|
||||||
public Participant(String name){
|
public Participant(String name){
|
||||||
this.name = name;
|
this(name, new HashSet<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Participant(String name, Set<EventDate> dates){
|
||||||
|
this.name = name;
|
||||||
|
this.dates = dates;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<EventDate> getDates() {
|
||||||
|
return new HashSet<>(this.dates);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void replaceDate(EventDate oldDate, EventDate newDate){
|
||||||
|
dates.remove(oldDate);
|
||||||
|
dates.add(newDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj) return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass()) return false;
|
||||||
|
Participant date = (Participant) obj;
|
||||||
|
return Objects.equals(name, date.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
package be.naaturel.letsmeet.repositories;
|
package be.naaturel.letsmeet.repositories;
|
||||||
|
|
||||||
import be.naaturel.letsmeet.entities.EventEntity;
|
import be.naaturel.letsmeet.dto.database.EventEntity;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public interface EventRepo
|
public interface EventRepo
|
||||||
extends JpaRepository<EventEntity, String> {
|
extends JpaRepository<EventEntity, String> {
|
||||||
|
|
||||||
@Query(value = "SELECT * FROM events WHERE ;", nativeQuery = true)
|
@Query(value = "SELECT * FROM events;", nativeQuery = true)
|
||||||
List<EventEntity> findEventEntityById(String id);
|
List<EventEntity> findEventEntityById(String id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package be.naaturel.letsmeet.services;
|
||||||
|
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.mappers.Mapper;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public abstract class AbstractService<T, T_ENTITY, T_DTO> {
|
||||||
|
|
||||||
|
protected JpaRepository<T_ENTITY, String> repo;
|
||||||
|
protected Mapper<T, T_ENTITY> dataBaseMapper;
|
||||||
|
protected Mapper<T, T_DTO> requestMapper;
|
||||||
|
|
||||||
|
AbstractService(JpaRepository<T_ENTITY, String> repo, Mapper<T, T_ENTITY> dataBaseMapper, Mapper<T, T_DTO> requestMapper){
|
||||||
|
this.repo = repo;
|
||||||
|
this.dataBaseMapper = dataBaseMapper;
|
||||||
|
this.requestMapper = requestMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract boolean save(T_DTO prop);
|
||||||
|
public abstract boolean delete(T_DTO prop);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package be.naaturel.letsmeet.services;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.dto.database.EventDateEntity;
|
||||||
|
import be.naaturel.letsmeet.dto.database.EventEntity;
|
||||||
|
import be.naaturel.letsmeet.dto.httpRequest.EventDTO;
|
||||||
|
import be.naaturel.letsmeet.mappers.database.EventMapper;
|
||||||
|
import be.naaturel.letsmeet.mappers.requests.EventDTOMapper;
|
||||||
|
import be.naaturel.letsmeet.models.Event;
|
||||||
|
import be.naaturel.letsmeet.repositories.EventRepo;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.dao.OptimisticLockingFailureException;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class EventService extends AbstractService<Event, EventEntity, EventDTO> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public EventService(EventRepo eventRepo){
|
||||||
|
super(eventRepo, new EventMapper(), new EventDTOMapper());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean save(EventDTO dto) {
|
||||||
|
Event event = this.requestMapper.toModel(dto);
|
||||||
|
EventEntity entity = this.dataBaseMapper.toEntity(event);
|
||||||
|
entity.prepareForSave();
|
||||||
|
try{
|
||||||
|
this.repo.save(entity);
|
||||||
|
return true;
|
||||||
|
} catch (IllegalArgumentException iae){
|
||||||
|
return false;
|
||||||
|
} catch (OptimisticLockingFailureException olfe){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean delete(EventDTO dto) {
|
||||||
|
Event event = this.requestMapper.toModel(dto);
|
||||||
|
EventEntity entity = this.dataBaseMapper.toEntity(event);
|
||||||
|
try{
|
||||||
|
this.repo.delete(entity);
|
||||||
|
return true;
|
||||||
|
} catch (IllegalArgumentException iae){
|
||||||
|
return false;
|
||||||
|
} catch (OptimisticLockingFailureException olfe){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
spring.application.name=letsmeet
|
spring.application.name=letsmeet
|
||||||
|
|
||||||
#=============SERVER=============
|
#=============SERVER=============
|
||||||
server.port=5000
|
server.port=5001
|
||||||
|
|
||||||
#=============SECURITY=============
|
#=============SECURITY=============
|
||||||
sec.cors.authorizedHots=*
|
sec.cors.authorizedHots=*
|
||||||
@@ -16,6 +16,8 @@ spring.datasource.password=${DB_PASSWORD}
|
|||||||
|
|
||||||
spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect
|
spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect
|
||||||
spring.jpa.show-sql=true
|
spring.jpa.show-sql=true
|
||||||
spring.jpa.hibernate.ddl-auto=create-drop
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
spring.jpa.defer-datasource-initialization=true
|
spring.jpa.defer-datasource-initialization=true
|
||||||
spring.user.datasource.driver-class-name=com.mysql.jdbc.Driver
|
spring.user.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||||
|
|
||||||
|
logging.level.org.springframework.web=DEBUG
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package be.naaturel.letsmeet.mappers;
|
package be.naaturel.letsmeet.mappers;
|
||||||
|
|
||||||
import be.naaturel.letsmeet.entities.DateEntity;
|
import be.naaturel.letsmeet.dto.database.EventDateEntity;
|
||||||
import be.naaturel.letsmeet.models.Date;
|
import be.naaturel.letsmeet.mappers.database.EventDateMapper;
|
||||||
|
import be.naaturel.letsmeet.models.EventDate;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@@ -12,43 +13,42 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
|
|
||||||
public class DateMapperTest {
|
public class DateMapperTest {
|
||||||
|
|
||||||
private static final Mapper<Date, DateEntity> mapper = new DateMapper();
|
private static final Mapper<EventDate, EventDateEntity> mapper = new EventDateMapper();
|
||||||
private static final Date model = new Date(0);
|
private static final EventDate model = new EventDate(0);
|
||||||
private static final Set<Date> models = Set.of(new Date(0), new Date(1), new Date(2));
|
private static final Set<EventDate> models = Set.of(new EventDate(0), new EventDate(1), new EventDate(2));
|
||||||
private static final DateEntity entity = new DateEntity();
|
private static final EventDateEntity entity = new EventDateEntity();
|
||||||
private static final Set<DateEntity> entities = new HashSet<>();
|
private static final Set<EventDateEntity> entities = new HashSet<>();
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
static void setup(){
|
static void setup(){
|
||||||
entity.id = "BLA BLA BLA FAKE ID";
|
|
||||||
entity.timeStamp = 0;
|
entity.timeStamp = 0;
|
||||||
|
|
||||||
entities.add(new DateEntity());
|
entities.add(new EventDateEntity());
|
||||||
entities.add(new DateEntity());
|
entities.add(new EventDateEntity());
|
||||||
entities.add(new DateEntity());
|
entities.add(new EventDateEntity());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void single_model_to_entity() {
|
void single_model_to_entity() {
|
||||||
DateEntity converted = mapper.toEntity(model);
|
EventDateEntity converted = mapper.toEntity(model);
|
||||||
assertEquals(converted.timeStamp, model.timeStamp);
|
assertEquals(converted.timeStamp, model.getTimeStamp());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void single_entity_to_model() {
|
void single_entity_to_model() {
|
||||||
Date converted = mapper.toModel(entity);
|
EventDate converted = mapper.toModel(entity);
|
||||||
assertEquals(converted.timeStamp, model.timeStamp);
|
assertEquals(converted.getTimeStamp(), entity.timeStamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void multiple_entities_to_models() {
|
void multiple_entities_to_models() {
|
||||||
Set<Date> dates = mapper.toModels(entities, HashSet::new);
|
Set<EventDate> dates = mapper.toModels(entities, HashSet::new);
|
||||||
assertEquals(dates.size(), entities.size());
|
assertEquals(dates.size(), entities.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void multiple_models_to_entities() {
|
void multiple_models_to_entities() {
|
||||||
Set<DateEntity> dates = mapper.toEntities(models, HashSet::new);
|
Set<EventDateEntity> dates = mapper.toEntities(models, HashSet::new);
|
||||||
assertEquals(dates.size(), models.size());
|
assertEquals(dates.size(), models.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package be.naaturel.letsmeet.mappers;
|
package be.naaturel.letsmeet.mappers;
|
||||||
|
|
||||||
import be.naaturel.letsmeet.entities.DateEntity;
|
import be.naaturel.letsmeet.dto.database.EventDateEntity;
|
||||||
import be.naaturel.letsmeet.entities.EventEntity;
|
import be.naaturel.letsmeet.dto.database.EventEntity;
|
||||||
import be.naaturel.letsmeet.models.Date;
|
import be.naaturel.letsmeet.mappers.database.EventMapper;
|
||||||
|
import be.naaturel.letsmeet.models.EventDate;
|
||||||
import be.naaturel.letsmeet.models.Event;
|
import be.naaturel.letsmeet.models.Event;
|
||||||
import be.naaturel.letsmeet.models.Participant;
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@@ -13,13 +13,11 @@ import java.util.*;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public class EventMapperTest {
|
public class EventMapperTest {
|
||||||
private static final Mapper<Event, EventEntity> mapper = new EventMapper();
|
/*private static final Mapper<Event, EventEntity> mapper = new EventMapper();
|
||||||
|
|
||||||
private static final Map<Date, Set<Participant>> participants = Map.of(new Date(0), Set.of(new Participant("Participant 1"), new Participant("Participant 2")),
|
private static final Set<EventDate> dates = Set.of(new EventDate(0, new HashSet<>()));
|
||||||
new Date(1), Set.of(new Participant("Participant 3"), new Participant("Participant 4")),
|
|
||||||
new Date(2), Set.of(new Participant("Participant 5"), new Participant("Participant 6")));
|
|
||||||
|
|
||||||
private static final Event model = new Event("Event for tests", participants);
|
private static final Event model = new Event("Event for tests", dates);
|
||||||
|
|
||||||
private static final List<Event> models = List.of(model, model, model);
|
private static final List<Event> models = List.of(model, model, model);
|
||||||
|
|
||||||
@@ -30,7 +28,7 @@ public class EventMapperTest {
|
|||||||
static void setup(){
|
static void setup(){
|
||||||
entity.id = "BLA BLA BLA FAKE ID";
|
entity.id = "BLA BLA BLA FAKE ID";
|
||||||
entity.name = "Event entity for tests";
|
entity.name = "Event entity for tests";
|
||||||
entity.dates = Set.of(new DateEntity(), new DateEntity(), new DateEntity());
|
entity.dates = Set.of(new EventDateEntity(), new EventDateEntity(), new EventDateEntity());
|
||||||
|
|
||||||
entities.add(entity);
|
entities.add(entity);
|
||||||
entities.add(entity);
|
entities.add(entity);
|
||||||
@@ -62,6 +60,6 @@ public class EventMapperTest {
|
|||||||
void multiple_models_to_entities() {
|
void multiple_models_to_entities() {
|
||||||
List<EventEntity> eventEntities = mapper.toEntities(models, ArrayList::new);
|
List<EventEntity> eventEntities = mapper.toEntities(models, ArrayList::new);
|
||||||
assertEquals(eventEntities.size(), models.size());
|
assertEquals(eventEntities.size(), models.size());
|
||||||
}
|
}*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package be.naaturel.letsmeet.repositories;
|
package be.naaturel.letsmeet.repositories;
|
||||||
|
|
||||||
import be.naaturel.letsmeet.entities.EventEntity;
|
import be.naaturel.letsmeet.dto.database.EventEntity;
|
||||||
import be.naaturel.letsmeet.models.Event;
|
|
||||||
import org.springframework.boot.test.context.TestConfiguration;
|
import org.springframework.boot.test.context.TestConfiguration;
|
||||||
import org.springframework.boot.CommandLineRunner;
|
import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|||||||
Reference in New Issue
Block a user