-
Notifications
You must be signed in to change notification settings - Fork 240
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
base: main
Are you sure you want to change the base?
Solution #199
Conversation
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.
There was a problem hiding this 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); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public List<CharacterDto> searchCharacters(@RequestParam String name) { | ||
return characterService.searchCharactersByName(name); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
@Configuration | ||
public class ObjectMapperConfig { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
@Entity | ||
@Table(name = "characters") | ||
@Data |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
@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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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)
There was a problem hiding this 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 ;)
public class ObjectMapperConfig { | ||
@Bean | ||
public ObjectMapper objectMapper() { | ||
return new ObjectMapper(); | ||
} | ||
} |
There was a problem hiding this comment.
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 { | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
@Operation(summary = "Search characters by name", | ||
description = "Returns a list of characters matching the given name.") | ||
@GetMapping() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GetMapping() | |
@GetMapping |
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CharacterRepository extends JpaRepository<Character, Long> { | ||
List<Character> findAllByNameIsContainingIgnoreCase(String name); |
There was a problem hiding this comment.
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
Implement REST API for Rick and Morty Characters
Added CharacterController with two endpoints:
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.