Implements creating of the new time entry, and makes some small refactorings
This commit is contained in:
@@ -35,14 +35,13 @@ public class TeamService {
|
||||
public Mono<GetTeamDto> addTeam(Mono<CreateTeamDto> createTeamDtoMono) {
|
||||
|
||||
return createTeamDtoMono
|
||||
.flatMap(createTeamDto -> {
|
||||
return teamRepository.findByName(createTeamDto.name())
|
||||
.map(team -> {
|
||||
log.error("Team with name " + createTeamDto.name() + " already exists");
|
||||
return team.toGetTeamDto();
|
||||
})
|
||||
.switchIfEmpty(createTeamWithMembers(createTeamDto));
|
||||
});
|
||||
.flatMap(createTeamDto -> teamRepository.findByName(createTeamDto.name())
|
||||
.map(team -> {
|
||||
log.error("Team with name " + createTeamDto.name() + " already exists");
|
||||
return team.toGetTeamDto();
|
||||
})
|
||||
.switchIfEmpty(createTeamWithMembers(createTeamDto))
|
||||
);
|
||||
}
|
||||
|
||||
private Mono<GetTeamDto> createTeamWithMembers(CreateTeamDto createTeamDto) {
|
||||
|
||||
@@ -2,6 +2,9 @@ package net.szymonsawicki.reactivetimesheetapp.application.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.szymonsawicki.reactivetimesheetapp.application.service.exception.TimeEntryServiceException;
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.time_entry.TimeEntry;
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.time_entry.TimeEntryUtils;
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.time_entry.dto.CreateTimeEntryDto;
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.time_entry.dto.GetTimeEntryDto;
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.time_entry.repository.TimeEntryRepository;
|
||||
@@ -17,10 +20,29 @@ public class TimeEntryService {
|
||||
private final TimeEntryRepository timeEntryRepository;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public Mono<GetTimeEntryDto> createTimeEntry(Mono<CreateTimeEntryDto> createTimeEntryDtoMono) {
|
||||
public Mono<GetTimeEntryDto> addTimeEntry(Mono<CreateTimeEntryDto> createTimeEntryDtoMono) {
|
||||
return createTimeEntryDtoMono
|
||||
.flatMap(createTimeEntryDto -> userRepository
|
||||
.findById(createTimeEntryDto.user()))
|
||||
.flatMap(createTimeEntryDto -> {
|
||||
|
||||
// check if user from time entry exists in db
|
||||
|
||||
userRepository
|
||||
.findById(createTimeEntryDto.user().id())
|
||||
.switchIfEmpty(Mono.error(new TimeEntryServiceException("cannot find user")));
|
||||
|
||||
// call of the private method, which checks in db if there is a conflict with existing entries
|
||||
|
||||
checkTime(createTimeEntryDto);
|
||||
|
||||
return timeEntryRepository.save(createTimeEntryDto.toTimeEntry())
|
||||
.map(TimeEntry::toGetTimeEntryDto);
|
||||
});
|
||||
}
|
||||
|
||||
private void checkTime(CreateTimeEntryDto timeEntryToCheck) {
|
||||
timeEntryRepository.findAllByUserAndDate(timeEntryToCheck.user().toUser(), timeEntryToCheck.date())
|
||||
.filter(timeEntry ->
|
||||
TimeEntryUtils.toTimeFrom.apply(timeEntry).isAfter(timeEntryToCheck.timeTo()) || TimeEntryUtils.toTimeTo.apply(timeEntry).isBefore(timeEntryToCheck.timeFrom()))
|
||||
.switchIfEmpty(Mono.error(new TimeEntryServiceException("entry time conflict")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import net.szymonsawicki.reactivetimesheetapp.domain.time_entry.dto.GetTimeEntry
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.time_entry.type.Category;
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.user.User;
|
||||
import net.szymonsawicki.reactivetimesheetapp.infrastructure.persistence.entity.TimeEntryEntity;
|
||||
import net.szymonsawicki.reactivetimesheetapp.infrastructure.persistence.entity.UserEntity;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
@@ -45,7 +44,7 @@ public class TimeEntry {
|
||||
date,
|
||||
timeFrom,
|
||||
timeTo,
|
||||
user,
|
||||
user.toGetUserDto(),
|
||||
category,
|
||||
description);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package net.szymonsawicki.reactivetimesheetapp.domain.time_entry;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface TimeEntryUtils {
|
||||
|
||||
Function<TimeEntry, LocalTime> toTimeFrom = timeEntry -> timeEntry.timeFrom;
|
||||
Function<TimeEntry, LocalTime> toTimeTo = timeEntry -> timeEntry.timeTo;
|
||||
|
||||
}
|
||||
@@ -2,18 +2,18 @@ package net.szymonsawicki.reactivetimesheetapp.domain.time_entry.dto;
|
||||
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.time_entry.TimeEntry;
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.time_entry.type.Category;
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.user.User;
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.user.dto.GetUserDto;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
|
||||
public record CreateTimeEntryDto(LocalDate date, LocalTime timeFrom, LocalTime timeTo, User user, Category category, String description) {
|
||||
TimeEntry toTimeEntry() {
|
||||
public record CreateTimeEntryDto(LocalDate date, LocalTime timeFrom, LocalTime timeTo, GetUserDto user, Category category, String description) {
|
||||
public TimeEntry toTimeEntry() {
|
||||
return TimeEntry.builder()
|
||||
.date(date)
|
||||
.timeFrom(timeFrom)
|
||||
.timeTo(timeTo)
|
||||
.user(user)
|
||||
.user(user.toUser())
|
||||
.category(category)
|
||||
.description(description)
|
||||
.build();
|
||||
|
||||
@@ -3,8 +3,6 @@ package net.szymonsawicki.reactivetimesheetapp.domain.time_entry.repository;
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.configs.CrudRepository;
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.time_entry.TimeEntry;
|
||||
import net.szymonsawicki.reactivetimesheetapp.domain.user.User;
|
||||
import net.szymonsawicki.reactivetimesheetapp.infrastructure.persistence.entity.TimeEntryEntity;
|
||||
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
Reference in New Issue
Block a user