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