Add Entities, Models and Mappers
This commit is contained in:
@@ -9,5 +9,4 @@ public class LetsmeetApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LetsmeetApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ public class EventController {
|
||||
|
||||
@PostMapping({"/create", "/create/"})
|
||||
public ResponseEntity<?> submit(@RequestBody Event e){
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,16 +6,13 @@ import java.util.Set;
|
||||
|
||||
import static jakarta.persistence.CascadeType.ALL;
|
||||
|
||||
@Entity(name = "Date")
|
||||
@Entity(name = "Dates")
|
||||
public class DateEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.UUID)
|
||||
public String id;
|
||||
|
||||
@Column
|
||||
public String name;
|
||||
|
||||
@Column
|
||||
public long timeStamp;
|
||||
|
||||
|
||||
@@ -6,13 +6,16 @@ import java.util.Set;
|
||||
|
||||
import static jakarta.persistence.CascadeType.ALL;
|
||||
|
||||
@Entity(name = "Event")
|
||||
@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> timeStamps;
|
||||
|
||||
@@ -2,7 +2,7 @@ package be.naaturel.letsmeet.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity(name = "Participant")
|
||||
@Entity(name = "Participants")
|
||||
public class ParticipantEntity {
|
||||
|
||||
@Id
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
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.timeStamps = 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) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <C extends Collection<Event>> C toModels(Collection<EventEntity> eventEntities, Supplier<C> collectionSupplier) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
16
back/src/main/java/be/naaturel/letsmeet/mappers/Mapper.java
Normal file
16
back/src/main/java/be/naaturel/letsmeet/mappers/Mapper.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package be.naaturel.letsmeet.mappers;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public interface Mapper<T, T_ENTITY> {
|
||||
|
||||
T_ENTITY toEntity(T d);
|
||||
|
||||
T toModel(T_ENTITY d);
|
||||
|
||||
<C extends Collection<T_ENTITY>> C toEntities(Collection<T> e, Supplier<C> collectionSupplier);
|
||||
|
||||
<C extends Collection<T>> C toModels(Collection<T_ENTITY> e, Supplier<C> collectionSupplier);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package be.naaturel.letsmeet.models;
|
||||
|
||||
|
||||
public class Date {
|
||||
|
||||
private long timestamp;
|
||||
public long timeStamp;
|
||||
|
||||
public Date(long timeStamp){
|
||||
this.timeStamp = timeStamp;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,31 @@
|
||||
package be.naaturel.letsmeet.models;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Event {
|
||||
|
||||
private String name;
|
||||
private long[] timestamp;
|
||||
private Map<Date, Set<Participant>> participants;
|
||||
|
||||
public Event(String name, long[] timestamp){
|
||||
public Event(String name){
|
||||
this(name, new HashMap<>());
|
||||
}
|
||||
|
||||
public Event(String name, Set<Date> dates, Set<Participant> participants){
|
||||
this.name = name;
|
||||
this.timestamp = timestamp;
|
||||
this.participants = new HashMap<>();
|
||||
}
|
||||
|
||||
public Event(String name, Map<Date, Set<Participant>> participants){
|
||||
this.name = name;
|
||||
this.participants = participants;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Set<Date> getDates() {
|
||||
return this.participants.keySet();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package be.naaturel.letsmeet.models;
|
||||
|
||||
public class Participant {
|
||||
|
||||
private String name;
|
||||
|
||||
public Participant(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,15 @@
|
||||
package be.naaturel.letsmeet.repositories;
|
||||
|
||||
public class EventRepo {
|
||||
import be.naaturel.letsmeet.entities.EventEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EventRepo
|
||||
extends JpaRepository<EventEntity, String> {
|
||||
|
||||
@Query(value = "SELECT * FROM events WHERE ;", nativeQuery = true)
|
||||
List<EventEntity> findEventEntityById(String id);
|
||||
}
|
||||
|
||||
@@ -17,4 +17,5 @@ spring.datasource.password=${DB_PASSWORD}
|
||||
spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.user.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
spring.jpa.defer-datasource-initialization=true
|
||||
spring.user.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package be.naaturel.letsmeet.mappers;
|
||||
|
||||
import be.naaturel.letsmeet.entities.DateEntity;
|
||||
import be.naaturel.letsmeet.models.Date;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class DateMapperTest {
|
||||
|
||||
private static final Mapper<Date, DateEntity> mapper = new DateMapper();
|
||||
private static final Date model = new Date(0);
|
||||
private static final Set<Date> models = Set.of(new Date(0), new Date(1), new Date(2));
|
||||
private static final DateEntity entity = new DateEntity();
|
||||
private static final Set<DateEntity> entities = new HashSet<>();
|
||||
|
||||
@BeforeAll
|
||||
static void setup(){
|
||||
entity.id = "BLA BLA BLA FAKE ID";
|
||||
entity.timeStamp = 0;
|
||||
|
||||
entities.add(new DateEntity());
|
||||
entities.add(new DateEntity());
|
||||
entities.add(new DateEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void single_model_to_entity() {
|
||||
DateEntity converted = mapper.toEntity(model);
|
||||
assertEquals(converted.timeStamp, model.timeStamp);
|
||||
}
|
||||
|
||||
@Test
|
||||
void single_entity_to_model() {
|
||||
Date converted = mapper.toModel(entity);
|
||||
assertEquals(converted.timeStamp, model.timeStamp);
|
||||
}
|
||||
|
||||
@Test
|
||||
void multiple_entities_to_models() {
|
||||
Set<Date> dates = mapper.toModels(entities, HashSet::new);
|
||||
assertEquals(dates.size(), entities.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void multiple_models_to_entities() {
|
||||
Set<DateEntity> dates = mapper.toEntities(models, HashSet::new);
|
||||
assertEquals(dates.size(), models.size());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package be.naaturel.letsmeet.mappers;
|
||||
|
||||
import be.naaturel.letsmeet.entities.DateEntity;
|
||||
import be.naaturel.letsmeet.models.Date;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class EventMapperTest {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package be.naaturel.letsmeet.repositories;
|
||||
|
||||
public class EventRepoTest {
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package be.naaturel.letsmeet.repositories;
|
||||
|
||||
import be.naaturel.letsmeet.entities.EventEntity;
|
||||
import be.naaturel.letsmeet.models.Event;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@TestConfiguration
|
||||
public class Seeder {
|
||||
|
||||
@Bean
|
||||
public CommandLineRunner seedDatabase(EventRepo eventRepo) {
|
||||
return args -> {
|
||||
if (eventRepo.count() == 0) {
|
||||
eventRepo.save(new EventEntity());
|
||||
eventRepo.save(new EventEntity());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,8 @@ services:
|
||||
environment:
|
||||
- MARIADB_ROOT_PASSWORD=root
|
||||
- DB_NAME=letsmeet_db
|
||||
- DB_USER=letsmeet_user
|
||||
- DB_PASSWORD=vklbzvkzbkeibv
|
||||
- DB_USER=
|
||||
- DB_PASSWORD=
|
||||
volumes:
|
||||
- letsmeet_database:/var/lib/mysql
|
||||
networks:
|
||||
@@ -35,8 +35,8 @@ services:
|
||||
dockerfile: Dockerfile
|
||||
environment:
|
||||
DB_URL: mysql://database:3306/letsmeet_db
|
||||
DB_USER: letsmeet_user
|
||||
DB_PASSWORD: vklbzvkzbkeibv
|
||||
DB_USER:
|
||||
DB_PASSWORD:
|
||||
ports:
|
||||
- "5001:5001"
|
||||
networks:
|
||||
|
||||
Reference in New Issue
Block a user