Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jv-rick-and-morty_by-Starikova-hw #145

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

OlgaStarikova
Copy link

No description provided.

@Operation(summary = "Get a list of characters ", description = " Get a list of all characters"
+ " whose name contains the search string. "
+ " Parameters: name = String for searching")
public List<PersonageDto> findByName(@RequestParam(name = "name") String name) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public List<PersonageDto> findByName(@RequestParam(name = "name") String name) {
public List<PersonageDto> findByName(@RequestParam String name) {


@Mapper(config = MapperConfig.class)
public interface PersonageMapper {
@Mapping(source = "id", target = "id")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Mapping(source = "id", target = "id")

try {
HttpResponse<String> response = httpClient
.send(httprequest, HttpResponse.BodyHandlers.ofString());
ObjectMapper objectMapper = new ObjectMapper();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont create Object evry time. Autowire it

Comment on lines 28 to 30
.stream()
.map(personageMapper::toModel)
.toList();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mapstruct supports mapping collections from the box

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not fixed

}
return personageMapper
.toDto(personage.orElseThrow(
() -> new EntityNotFoundException("Not found any random personage")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is only possible to have an Exception when there are no personages, right? So then change the ex message

Comment on lines 37 to 40
while (personage.isEmpty() && personageRepository.getMaxId() != 0) {
Long randomId = random.nextLong(personageRepository.getMaxId() + 1);
personage = personageRepository.findById(randomId);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a bad approach, so will SELECT max(EXTERNAL_ID) FROM Personages ever return 0?
Also it's doesn't look fine to put call to DB in while condition. Can't you just call personageRepository.count()? So you won't need this while block

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put while loop here for case if the table has some deleted records and so the random number can point to not exist id.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. How is it possible to maintain deleted records in the table in your implementation, you haven't introduced soft deletion in this task
  2. You also don't have any deletion logic, so I can't get such an approach.

Comment on lines 34 to 35
@Override
public PersonageDto getRandomPersonage() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@transactional. You make more than one call to DB

Comment on lines 55 to 58
.findAllByNameContainsIgnoreCase(name)
.stream()
.map(personageMapper::toDto)
.toList();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also Mapstruct supports mapping collections from the box.
List toDtolist(List personages)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

Comment on lines 28 to 30
.stream()
.map(personageMapper::toModel)
.toList();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not fixed

Comment on lines 37 to 40
while (personage.isEmpty() && personageRepository.getMaxId() != 0) {
Long randomId = random.nextLong(personageRepository.getMaxId() + 1);
personage = personageRepository.findById(randomId);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. How is it possible to maintain deleted records in the table in your implementation, you haven't introduced soft deletion in this task
  2. You also don't have any deletion logic, so I can't get such an approach.

public PersonageDto getRandomPersonage() {
Optional<Personage> personage = Optional.empty();
if (personageRepository.count() > 0) {
while (personage.isEmpty()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while (personage.isEmpty()) {

@Transactional
public PersonageDto getRandomPersonage() {
Optional<Personage> personage = Optional.empty();
if (personageRepository.count() > 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract personageRepository.count() to a variable, you call .count() twice


@Entity
@Data
@Table(name = "Personages")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Table(name = "Personages")
@Table(name = "personages")


@Repository
public interface PersonageRepository extends JpaRepository<Personage, Long> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

import mate.academy.rickandmorty.dto.external.ResponsePersonageDto;

public interface PersonageApiClient {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@RequiredArgsConstructor
@Service
public class PersonageApiClientImpl implements PersonageApiClient {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

import lombok.Data;

@Data
public class ResponseDto {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class ResponseDto {
public record ResponseDto {


@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseInfoDto {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class ResponseInfoDto {
public record ResponseInfoDto {


@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponsePersonageDto {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class ResponsePersonageDto {
public record ResponsePersonageDto {

import lombok.Data;

@Data
public class PersonageDto {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class PersonageDto {
public record PersonageDto {

ResponseDto.class);
return responseDto;
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add message when you throw the exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants