Internalize creation logic of database entities
This commit is contained in:
@@ -25,7 +25,9 @@ public class EventController {
|
||||
try{
|
||||
service.save(dto);
|
||||
} catch (Exception e){
|
||||
return ResponseEntity.internalServerError().build();
|
||||
return ResponseEntity
|
||||
.internalServerError()
|
||||
.build();
|
||||
}
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
|
||||
@@ -24,25 +24,19 @@ public class EventEntity {
|
||||
@OneToMany(targetEntity=ParticipantEntity.class, cascade=CascadeType.ALL, mappedBy="event")
|
||||
public Set<ParticipantEntity> participants;
|
||||
|
||||
public void prepareForSave(){
|
||||
linkDates();
|
||||
linkParticipants();
|
||||
removeDuplicatedDates();
|
||||
}
|
||||
|
||||
private void linkDates(){
|
||||
public void linkDates(){
|
||||
for (EventDateEntity date : this.dates) {
|
||||
date.event = this;
|
||||
}
|
||||
}
|
||||
|
||||
private void linkParticipants(){
|
||||
public void linkParticipants(){
|
||||
for (ParticipantEntity participant : this.participants) {
|
||||
participant.event = this;
|
||||
}
|
||||
}
|
||||
|
||||
private void removeDuplicatedDates(){
|
||||
public void removeDuplicatedDates(){
|
||||
|
||||
for (EventDateEntity ede: dates) {
|
||||
for (ParticipantEntity pe : participants) {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package be.naaturel.letsmeet.dto.database.factories;
|
||||
|
||||
import be.naaturel.letsmeet.dto.database.EventDateEntity;
|
||||
import be.naaturel.letsmeet.dto.database.EventEntity;
|
||||
import be.naaturel.letsmeet.dto.database.ParticipantEntity;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class DatabasePropsFactory {
|
||||
|
||||
public static EventEntity createEvent(String name, Set<ParticipantEntity> participants){
|
||||
|
||||
EventEntity entity = new EventEntity();
|
||||
entity.name = name;
|
||||
entity.participants = participants;
|
||||
entity.dates = new HashSet<>();
|
||||
for (ParticipantEntity pe : entity.participants) {
|
||||
entity.dates.addAll(pe.dates);
|
||||
}
|
||||
entity.linkDates();
|
||||
entity.linkParticipants();
|
||||
entity.removeDuplicatedDates();
|
||||
return entity;
|
||||
}
|
||||
|
||||
public static ParticipantEntity createParticipant(String name, Set<EventDateEntity> dates){
|
||||
ParticipantEntity entity = new ParticipantEntity();
|
||||
entity.name = name;
|
||||
entity.dates = dates;
|
||||
return entity;
|
||||
}
|
||||
|
||||
public static EventDateEntity createDate(long timestamp){
|
||||
EventDateEntity entity = new EventDateEntity();
|
||||
entity.timeStamp = timestamp;
|
||||
return entity;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package be.naaturel.letsmeet.mappers.database;
|
||||
|
||||
import be.naaturel.letsmeet.dto.database.EventDateEntity;
|
||||
import be.naaturel.letsmeet.dto.database.factories.DatabasePropsFactory;
|
||||
import be.naaturel.letsmeet.mappers.Mapper;
|
||||
import be.naaturel.letsmeet.models.EventDate;
|
||||
|
||||
@@ -12,9 +13,7 @@ public class EventDateMapper implements Mapper<EventDate, EventDateEntity> {
|
||||
|
||||
@Override
|
||||
public EventDateEntity toEntity(EventDate d) {
|
||||
EventDateEntity de = new EventDateEntity();
|
||||
de.timeStamp = d.getTimeStamp();
|
||||
return de;
|
||||
return DatabasePropsFactory.createDate(d.getTimeStamp());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,6 +2,7 @@ package be.naaturel.letsmeet.mappers.database;
|
||||
|
||||
import be.naaturel.letsmeet.dto.database.EventEntity;
|
||||
import be.naaturel.letsmeet.dto.database.ParticipantEntity;
|
||||
import be.naaturel.letsmeet.dto.database.factories.DatabasePropsFactory;
|
||||
import be.naaturel.letsmeet.mappers.Mapper;
|
||||
import be.naaturel.letsmeet.models.Event;
|
||||
import be.naaturel.letsmeet.models.Participant;
|
||||
@@ -19,15 +20,7 @@ public class EventMapper implements Mapper<Event, EventEntity> {
|
||||
|
||||
@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;
|
||||
return DatabasePropsFactory.createEvent(event.getName(), participantMapper.toEntities(event.getParticipants(), HashSet::new));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,6 +2,7 @@ package be.naaturel.letsmeet.mappers.database;
|
||||
|
||||
import be.naaturel.letsmeet.dto.database.EventDateEntity;
|
||||
import be.naaturel.letsmeet.dto.database.ParticipantEntity;
|
||||
import be.naaturel.letsmeet.dto.database.factories.DatabasePropsFactory;
|
||||
import be.naaturel.letsmeet.mappers.Mapper;
|
||||
import be.naaturel.letsmeet.models.EventDate;
|
||||
import be.naaturel.letsmeet.models.Participant;
|
||||
@@ -21,10 +22,11 @@ public class ParticipantMapper implements Mapper<Participant, ParticipantEntity>
|
||||
|
||||
@Override
|
||||
public ParticipantEntity toEntity(Participant d) {
|
||||
ParticipantEntity pe = new ParticipantEntity();
|
||||
return DatabasePropsFactory.createParticipant(d.getName(), dateMapper.toEntities(d.getDates(), HashSet::new));
|
||||
/*ParticipantEntity pe = new ParticipantEntity();
|
||||
pe.name = d.getName();
|
||||
pe.dates = dateMapper.toEntities(d.getDates(), HashSet::new);
|
||||
return pe;
|
||||
return pe;*/
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,7 +23,6 @@ public class EventService extends AbstractService<Event, EventEntity, EventDTO>
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user