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

Solution #199

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

Solution #199

wants to merge 12 commits into from

Conversation

ShastkivRuslan
Copy link

Implement REST API for Rick and Morty Characters

  • Added CharacterController with two endpoints:

    • GET /api/characters/random: Retrieves a random character from the database.
    • GET /api/characters/search: Searches for characters by name.
  • Introduced CharacterService for business logic and data management.

  • Integrated CharacterRepository for database access.

  • Implemented data fetching from the external Rick and Morty API during application startup.

  • Configured MySQL as the main database and H2 for testing.

  • Added Swagger documentation for API endpoints.

added @JsonIgnoreProperties annotation to CreateCharacterDto record;
added character service;
added CharacterRepository;
-> refactoring phase
added swagger api;
- Added CharacterController with two endpoints:
  - GET /api/characters/random: Retrieves a random character from the database.
  - GET /api/characters/search: Searches for characters by name.

- Introduced CharacterService for business logic and data management.
- Integrated CharacterRepository for database access.
- Implemented data fetching from the external Rick and Morty API during application startup.
- Configured MySQL as the main database and H2 for testing.
- Added Swagger documentation for API endpoints.
Copy link

@okuzan okuzan left a comment

Choose a reason for hiding this comment

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

Good job, minor comments

throw new HttpRequestException("Failed to send https request to url: " + url, e);
}
}

Copy link

Choose a reason for hiding this comment

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

Suggested change

public List<CharacterDto> searchCharacters(@RequestParam String name) {
return characterService.searchCharactersByName(name);
}

Copy link

Choose a reason for hiding this comment

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

Suggested change


@Configuration
public class ObjectMapperConfig {

Copy link

Choose a reason for hiding this comment

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

Suggested change


@Entity
@Table(name = "characters")
@Data
Copy link

Choose a reason for hiding this comment

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

Generally, avoid using Lombok's Data on JPA entities, it contains problematic EqualsAndHashcode and ToString, use Getter and Setter for now

import org.mapstruct.Mapper;

@Mapper(config = MapperConfig.class)
public interface CharacterMapper {
Copy link

Choose a reason for hiding this comment

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

FYI you can define List to List mapping here too to avoid mapping in stream chain

Comment on lines 23 to 30
@GetMapping
public CharacterDto getRandomCharacter() {
return characterService.getRandomCharacter();
}

@Operation(summary = "Search characters by name",
description = "Returns a list of characters matching the given name.")
@GetMapping("/search")
Copy link

Choose a reason for hiding this comment

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

Suggested change
@GetMapping
public CharacterDto getRandomCharacter() {
return characterService.getRandomCharacter();
}
@Operation(summary = "Search characters by name",
description = "Returns a list of characters matching the given name.")
@GetMapping("/search")
@GetMapping("/random")
public CharacterDto getRandomCharacter() {
return characterService.getRandomCharacter();
}
@Operation(summary = "Search characters by name",
description = "Returns a list of characters matching the given name.")
@GetMapping

it will be more RESTful (we generally avoid verbs in URL paths, HTTP verb should imply action)

…and @Setter in JPA entity, updated CharacterMapper for List mapping, revised exception handling in RickAndMortyClient, and improved RESTful naming conventions for controller endpoints
Copy link

@liliia-ponomarenko liliia-ponomarenko left a comment

Choose a reason for hiding this comment

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

Good job! A few minor comments ;)

Comment on lines +8 to +13
public class ObjectMapperConfig {
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
}

Choose a reason for hiding this comment

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

you use one configuration file ;)

)
public class MapperConfig {
}

Choose a reason for hiding this comment

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

Suggested change


@Operation(summary = "Search characters by name",
description = "Returns a list of characters matching the given name.")
@GetMapping()

Choose a reason for hiding this comment

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

Suggested change
@GetMapping()
@GetMapping

import org.springframework.data.jpa.repository.JpaRepository;

public interface CharacterRepository extends JpaRepository<Character, Long> {
List<Character> findAllByNameIsContainingIgnoreCase(String name);

Choose a reason for hiding this comment

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

if I remember correctly mysql ignores case by default

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