Conversion from model to DTO

master
Elliott LE GUEHENNEC 2 years ago
parent e2cf8b34d0
commit 1f97a8ceac

@ -1,9 +1,27 @@
package com.flagg10ma.taf.dto;
import com.flagg10ma.taf.model.Event;
import com.flagg10ma.taf.model.Label;
import com.flagg10ma.taf.model.Task;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public record CompleteEventDto(
EventDto event,
Collection<LabelDto> labels
) {
public static CompleteEventDto fromModel(Event event) {
Set<Label> allLabels = new HashSet<>();
for (Task task : event.tasks()) {
allLabels.addAll(task.labels());
}
return new CompleteEventDto(
EventDto.fromModel(event),
allLabels.stream().map(LabelDto::fromModel).collect(Collectors.toList())
);
}
}

@ -2,9 +2,11 @@ package com.flagg10ma.taf.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.flagg10ma.taf.model.Event;
import java.time.LocalDate;
import java.util.Collection;
import java.util.stream.Collectors;
public record EventDto(
@JsonProperty("event_id") String id,
@ -23,4 +25,16 @@ public record EventDto(
@JsonProperty("task_count") int taskCount,
Collection<EventTaskDto> tasks
) {
public static EventDto fromModel(Event event) {
return new EventDto(
event.id(),
event.title(),
event.description(),
event.startTime().toLocalDate(),
event.endTime().toLocalDate(),
event.colorCode(),
event.tasks().size(),
event.tasks().stream().map(EventTaskDto::fromModel).collect(Collectors.toList())
);
}
}

@ -1,9 +1,28 @@
package com.flagg10ma.taf.dto;
import com.flagg10ma.taf.model.Event;
import com.flagg10ma.taf.model.Label;
import com.flagg10ma.taf.model.Task;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public record EventListDto(
Collection<EventDto> events,
Collection<LabelDto> labels
) {
public static EventListDto fromModel(Collection<Event> events) {
Set<Label> allLabels = new HashSet<>();
for (Event event : events)
for (Task task : event.tasks()) {
allLabels.addAll(task.labels());
}
return new EventListDto(
events.stream().map(EventDto::fromModel).collect(Collectors.toList()),
allLabels.stream().map(LabelDto::fromModel).collect(Collectors.toList())
);
}
}

@ -2,9 +2,12 @@ package com.flagg10ma.taf.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.flagg10ma.taf.model.Label;
import com.flagg10ma.taf.model.Task;
import java.time.LocalDate;
import java.util.Collection;
import java.util.stream.Collectors;
public record EventTaskDto(
@JsonProperty("task_id") String id,
@ -20,4 +23,15 @@ public record EventTaskDto(
@JsonProperty("label_ids") Collection<String> labels
) {
public static EventTaskDto fromModel(Task task) {
return new EventTaskDto(
task.id(),
task.title(),
task.description(),
task.delayed(),
task.dateAssigned(),
task.dateCompleted(),
task.labels().stream().map(Label::id).collect(Collectors.toList())
);
}
}

@ -1,6 +1,7 @@
package com.flagg10ma.taf.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.flagg10ma.taf.model.Label;
public record LabelDto(
@JsonProperty String id,
@ -8,4 +9,12 @@ public record LabelDto(
String description,
@JsonProperty("color_code") String color
) {
public static LabelDto fromModel(Label label) {
return new LabelDto(
label.id(),
label.title(),
label.description(),
label.colorCode()
);
}
}

@ -2,6 +2,7 @@ package com.flagg10ma.taf.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.flagg10ma.taf.model.Event;
import java.time.LocalDate;
@ -21,4 +22,15 @@ public record SimplifiedEventDto(
@JsonProperty("color_code") String color,
@JsonProperty("task_count") int taskCount
) {
public static SimplifiedEventDto fromModel(Event event) {
return new SimplifiedEventDto(
event.id(),
event.title(),
event.description(),
event.startTime().toLocalDate(),
event.endTime().toLocalDate(),
event.colorCode(),
event.tasks().size()
);
}
}

@ -2,9 +2,12 @@ package com.flagg10ma.taf.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.flagg10ma.taf.model.Label;
import com.flagg10ma.taf.model.Task;
import java.time.LocalDate;
import java.util.Collection;
import java.util.stream.Collectors;
public record SimplifiedTaskDto(
@JsonProperty("task_id") String id,
@ -21,4 +24,16 @@ public record SimplifiedTaskDto(
@JsonProperty("event_id") String event,
@JsonProperty("label_ids") Collection<String> labels
) {
public static SimplifiedTaskDto fromModel(Task task) {
return new SimplifiedTaskDto(
task.id(),
task.title(),
task.description(),
task.delayed(),
task.dateAssigned(),
task.dateCompleted(),
task.event().id(),
task.labels().stream().map(Label::id).collect(Collectors.toList())
);
}
}

@ -2,9 +2,12 @@ package com.flagg10ma.taf.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.flagg10ma.taf.model.Label;
import com.flagg10ma.taf.model.Task;
import java.time.LocalDate;
import java.util.Collection;
import java.util.stream.Collectors;
public record TaskDto(
@JsonProperty("task_id") String id,
@ -19,6 +22,18 @@ public record TaskDto(
@JsonProperty("date_completed") LocalDate completionDate,
SimplifiedEventDto event,
@JsonProperty("label_ids") Collection<String> labels
@JsonProperty("label_ids") Collection<LabelDto> labels
) {
public static TaskDto fromModel(Task task) {
return new TaskDto(
task.id(),
task.title(),
task.description(),
task.delayed(),
task.dateAssigned(),
task.dateCompleted(),
SimplifiedEventDto.fromModel(task.event()),
task.labels().stream().map(LabelDto::fromModel).collect(Collectors.toList())
);
}
}

@ -1,10 +1,32 @@
package com.flagg10ma.taf.dto;
import com.flagg10ma.taf.model.Event;
import com.flagg10ma.taf.model.Task;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public record TaskListDto(
Collection<SimplifiedTaskDto> tasks,
Collection<SimplifiedEventDto> events,
Collection<LabelDto> labels
) {
public static TaskListDto fromModel(Collection<Task> tasks, Collection<Event> events) {
Collection<SimplifiedTaskDto> taskDtos = tasks.stream()
.map(SimplifiedTaskDto::fromModel)
.collect(Collectors.toList());
Collection<SimplifiedEventDto> eventDtos = events.stream()
.map(SimplifiedEventDto::fromModel)
.collect(Collectors.toList());
Set<LabelDto> labelDtos = new HashSet<>();
for (Task task: tasks) {
labelDtos.addAll(task.labels().stream().map(LabelDto::fromModel).toList());
}
return new TaskListDto(taskDtos, eventDtos, labelDtos);
}
}

@ -4,7 +4,6 @@ import com.fasterxml.jackson.annotation.JsonProperty;
public record UserDto(
@JsonProperty String id,
String login,
String password
String login
){
}

Loading…
Cancel
Save