Creates error dto, global routing handler, all handler and one routng class (empty) and implements user handler

This commit is contained in:
szsa
2022-03-18 22:47:30 +01:00
parent b154fa988c
commit 0aeda261a3
9 changed files with 101 additions and 2 deletions

View File

@@ -42,7 +42,8 @@ public class TimeEntryService {
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()))
TimeEntryUtils.toTimeFrom.apply(timeEntry).isAfter(timeEntryToCheck.timeTo()) ||
TimeEntryUtils.toTimeTo.apply(timeEntry).isBefore(timeEntryToCheck.timeFrom()))
.switchIfEmpty(Mono.error(new TimeEntryServiceException("entry time conflict")));
}
}

View File

@@ -51,6 +51,9 @@ public class UserService {
return userRepository
.findById(userId)
.flatMap(user -> {
// when teamId is not null then list of members in related team will be updated
String teamId = UserUtils.toTeamId.apply(user);
if (teamId != null) {
teamRepository

View File

@@ -1,6 +1,5 @@
package net.szymonsawicki.reactivetimesheetapp.infrastructure.persistence.dao;
import net.szymonsawicki.reactivetimesheetapp.domain.team.Team;
import net.szymonsawicki.reactivetimesheetapp.infrastructure.persistence.entity.TeamEntity;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import reactor.core.publisher.Mono;

View File

@@ -0,0 +1,4 @@
package net.szymonsawicki.reactivetimesheetapp.web;
public class Routing {
}

View File

@@ -0,0 +1,27 @@
package net.szymonsawicki.reactivetimesheetapp.web.config;
import net.szymonsawicki.reactivetimesheetapp.web.error.ErrorDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
public interface GlobalRoutingHandler {
// Helper method for generic request processing and error handling
static <T> Mono<ServerResponse> doRequest(Mono<T> action, HttpStatus httpStatus) {
return action
.flatMap(result -> ServerResponse
.status(httpStatus)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(result))
)
.onErrorResume(error -> ServerResponse
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(new ErrorDto(error.getMessage())))
);
}
}

View File

@@ -0,0 +1,4 @@
package net.szymonsawicki.reactivetimesheetapp.web.error;
public record ErrorDto(String message) {
}

View File

@@ -0,0 +1,11 @@
package net.szymonsawicki.reactivetimesheetapp.web.handler;
import lombok.RequiredArgsConstructor;
import net.szymonsawicki.reactivetimesheetapp.application.service.TeamService;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class TeamHandler {
private final TeamService teamService;
}

View File

@@ -0,0 +1,12 @@
package net.szymonsawicki.reactivetimesheetapp.web.handler;
import lombok.RequiredArgsConstructor;
import net.szymonsawicki.reactivetimesheetapp.application.service.TimeEntryService;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class TimeEntryHandler {
private final TimeEntryService timeEntryService;
}

View File

@@ -0,0 +1,38 @@
package net.szymonsawicki.reactivetimesheetapp.web.handler;
import lombok.RequiredArgsConstructor;
import net.szymonsawicki.reactivetimesheetapp.application.service.UserService;
import net.szymonsawicki.reactivetimesheetapp.domain.user.dto.CreateUserDto;
import net.szymonsawicki.reactivetimesheetapp.web.config.GlobalRoutingHandler;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
@RequiredArgsConstructor
public class UserHandler {
private final UserService userService;
public Mono<ServerResponse> findById(ServerRequest serverRequest) {
var userId = serverRequest.pathVariable("userId");
return GlobalRoutingHandler.doRequest(userService.findById(userId), HttpStatus.OK);
}
public Mono<ServerResponse> findByUsername(ServerRequest serverRequest) {
var username = serverRequest.pathVariable("username");
return GlobalRoutingHandler.doRequest(userService.findById(username), HttpStatus.OK);
}
public Mono<ServerResponse> createUser(ServerRequest serverRequest) {
var createUsertDtoMono = serverRequest.bodyToMono(CreateUserDto.class);
return GlobalRoutingHandler.doRequest(userService.addUser(createUsertDtoMono), HttpStatus.CREATED);
}
public Mono<ServerResponse> deleteUser(ServerRequest serverRequest) {
var userId = serverRequest.pathVariable("userId");
return GlobalRoutingHandler.doRequest(userService.deleteUser(userId), HttpStatus.OK);
}
}