Skip to content

Commit

Permalink
Make review creation more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
Fagorym committed May 22, 2024
1 parent 18f7a92 commit 683d492
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.nsu.fit.directors.userservice.dto.request;

import jakarta.annotation.Nonnull;
import jakarta.validation.constraints.NotNull;

public record ReviewCreationDto(
Expand All @@ -12,4 +13,8 @@ public record ReviewCreationDto(
@NotNull(message = "Оценка не может быть не задана.")
Integer score
) {
@Nonnull
public ReviewCreationDto withUsername(String newUsername) {
return new ReviewCreationDto(newUsername, establishmentId, text, score);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.nsu.fit.directors.userservice.exception;

public class EnumNotFoundException extends BaseException {
public <E> EnumNotFoundException(Class<E> enumClass, String name) {
super("Отсутствует константа %s с названием %s".formatted(enumClass.getSimpleName(), name), "NO_CONSTANT");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Service;
import ru.nsu.fit.directors.userservice.dto.request.ReviewCreationDto;
import ru.nsu.fit.directors.userservice.dto.response.ResponseReviewDto;
import ru.nsu.fit.directors.userservice.model.User;
import ru.nsu.fit.directors.userservice.model.UserReview;
import ru.nsu.fit.directors.userservice.service.EstablishmentService;
import ru.nsu.fit.directors.userservice.service.ReviewService;
Expand All @@ -24,8 +25,9 @@ public class ReviewFacadeImpl implements ReviewFacade {

@Override
public void createReview(ReviewCreationDto reviewCreationDto) {
Long externalId = establishmentService.createReview(reviewCreationDto);
reviewService.save(new UserReview().setUser(securityService.getLoggedInUser()).setExternalId(externalId));
User user = securityService.getLoggedInUser();
Long externalId = establishmentService.createReview(reviewCreationDto.withUsername(user.getUsername()));
reviewService.save(new UserReview().setUser(user).setExternalId(externalId));
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import ru.nsu.fit.directors.userservice.dto.response.BaseResponse;
import ru.nsu.fit.directors.userservice.dto.response.EstablishmentListDto;
import ru.nsu.fit.directors.userservice.dto.response.ResponseReviewDto;
import ru.nsu.fit.directors.userservice.enums.EntityType;
import ru.nsu.fit.directors.userservice.exception.EntityNotFoundException;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
Expand Down Expand Up @@ -61,6 +63,8 @@ public List<ResponseReviewDto> getReviewsByExternalIds(List<Long> externalIds) {
@Nonnull
@Override
public Long createReview(ReviewCreationDto reviewCreationDto) {
return establishmentClient.createReview(reviewCreationDto).getBody().getResult();
return Optional.ofNullable(establishmentClient.createReview(reviewCreationDto).getBody())
.map(BaseResponse::getResult)
.orElseThrow(() -> new EntityNotFoundException(EntityType.COMPANY, reviewCreationDto.establishmentId()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.nsu.fit.directors.userservice.utils;

import java.util.Arrays;

import javax.annotation.Nonnull;

import lombok.experimental.UtilityClass;
import ru.nsu.fit.directors.userservice.exception.EnumNotFoundException;

@UtilityClass
public class EnumUtils {
@Nonnull
public <E extends Enum<E>> E findEnum(String name, Class<E> enumClass) {
return Arrays.stream(enumClass.getEnumConstants())
.filter(parseableEnum -> parseableEnum.name().equalsIgnoreCase(name))
.findFirst()
.orElseThrow(() -> new EnumNotFoundException(enumClass, name));
}
}

0 comments on commit 683d492

Please sign in to comment.