-
Notifications
You must be signed in to change notification settings - Fork 298
Completed Rick and Morty task #208
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
Open
AntonZhdanov
wants to merge
1
commit into
mate-academy:main
Choose a base branch
from
AntonZhdanov:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/rickandmorty/config/HttpConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mate.academy.rickandmorty.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class HttpConfig { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/mate/academy/rickandmorty/controller/CharacterController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package mate.academy.rickandmorty.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.CharacterResponseDto; | ||
import mate.academy.rickandmorty.service.InternalCharacterService; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/characters") | ||
public class CharacterController { | ||
private final InternalCharacterService internalCharacterService; | ||
|
||
@Operation(summary = "Get a random character", | ||
description = "Returns a randomly selected character from the database, " | ||
+ "including all available details such as: id, name, gender, and status") | ||
@GetMapping("/random") | ||
public CharacterResponseDto getRandomCharacter() { | ||
return internalCharacterService.getRandomCharacter(); | ||
} | ||
|
||
@Operation(summary = "Search for characters by name", | ||
description = "Retrieves a list of characters whose names contain " | ||
+ "the provided search string.") | ||
@GetMapping("/search") | ||
public List<CharacterResponseDto> searchCharacterByName(@RequestParam String name) { | ||
if (!name.matches("[a-zA-Z]+")) { | ||
throw new IllegalArgumentException("Invalid name format"); | ||
} | ||
|
||
return internalCharacterService.searchCharacterByName(name); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/mate/academy/rickandmorty/dto/CharacterResponseDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package mate.academy.rickandmorty.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CharacterResponseDto { | ||
private Long id; | ||
private Long externalId; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/rickandmorty/dto/ExternalApiResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mate.academy.rickandmorty.dto; | ||
|
||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ExternalApiResponse { | ||
private List<ExternalCharacterDto> results; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/mate/academy/rickandmorty/dto/ExternalCharacterDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mate.academy.rickandmorty.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ExternalCharacterDto { | ||
private Long id; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/mate/academy/rickandmorty/entity/Character.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package mate.academy.rickandmorty.entity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Table(name = "characters") | ||
public class Character { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private Long externalId; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/rickandmorty/entity/Gender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mate.academy.rickandmorty.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum Gender { | ||
MALE("Male"), | ||
FEMALE("Female"), | ||
UNKNOWN("Unknown"); | ||
|
||
private final String gender; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/rickandmorty/entity/Status.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mate.academy.rickandmorty.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum Status { | ||
ALIVE("Alive"), | ||
DEAD("Dead"), | ||
UNKNOWN("Unknown"); | ||
|
||
private final String status; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/rickandmorty/exception/CharacterNotFoundException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.rickandmorty.exception; | ||
|
||
public class CharacterNotFoundException extends RuntimeException { | ||
public CharacterNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/rickandmorty/exception/ExternalApiException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.rickandmorty.exception; | ||
|
||
public class ExternalApiException extends RuntimeException { | ||
public ExternalApiException(String message) { | ||
super(message); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/mate/academy/rickandmorty/exception/GlobalExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package mate.academy.rickandmorty.exception; | ||
|
||
import java.util.Map; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.MissingServletRequestParameterException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
@ExceptionHandler(CharacterNotFoundException.class) | ||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
public Map<String, String> handleCharacterNotFound(CharacterNotFoundException e) { | ||
return Map.of("error", e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(ExternalApiException.class) | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
public Map<String, String> handleExternalApiException(ExternalApiException e) { | ||
return Map.of("error", e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
public Map<String, String> handleGenericException(Exception e) { | ||
return Map.of("error", "Unexpected error occurred", "details", e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(MissingServletRequestParameterException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public Map<String, String> handleMissingServletRequestParameter( | ||
MissingServletRequestParameterException e) { | ||
return Map.of("error", "Missing required parameter: " + e.getParameterName()); | ||
} | ||
|
||
@ExceptionHandler(IllegalArgumentException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public Map<String, String> handleIllegalArgumentException(IllegalArgumentException e) { | ||
return Map.of("error", e.getMessage()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/mate/academy/rickandmorty/initdata/DataInitializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package mate.academy.rickandmorty.initdata; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.entity.Character; | ||
import mate.academy.rickandmorty.service.ExternalCharacterService; | ||
import mate.academy.rickandmorty.service.InternalCharacterService; | ||
import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class DataInitializer { | ||
private final ExternalCharacterService externalCharacterService; | ||
private final InternalCharacterService internalCharacterService; | ||
|
||
@EventListener(ApplicationReadyEvent.class) | ||
public void initData() { | ||
if (internalCharacterService.getAllCharacters().isEmpty()) { | ||
List<Character> characters = externalCharacterService.fetchAllCharactersFromApi(); | ||
internalCharacterService.saveAllCharacters(characters); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package mate.academy.rickandmorty.mapper; | ||
|
||
import java.util.Arrays; | ||
import mate.academy.rickandmorty.dto.CharacterResponseDto; | ||
import mate.academy.rickandmorty.dto.ExternalCharacterDto; | ||
import mate.academy.rickandmorty.entity.Character; | ||
import mate.academy.rickandmorty.entity.Gender; | ||
import mate.academy.rickandmorty.entity.Status; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.Named; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface CharacterMapper { | ||
@Mapping(source = "externalId", target = "externalId") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The mapping |
||
@Mapping(source = "status", target = "status", qualifiedByName = "stringToStatus") | ||
@Mapping(source = "gender", target = "gender", qualifiedByName = "stringToGender") | ||
CharacterResponseDto toDto(Character character); | ||
|
||
@Mapping(source = "id", target = "externalId") | ||
@Mapping(source = "status", target = "status", qualifiedByName = "stringToStatus") | ||
@Mapping(source = "gender", target = "gender", qualifiedByName = "stringToGender") | ||
Character toEntity(ExternalCharacterDto dto); | ||
|
||
@Named("stringToStatus") | ||
default Status stringToStatus(String status) { | ||
return Arrays.stream(Status.values()) | ||
.filter(s -> s.getStatus().equalsIgnoreCase(status)) | ||
.findFirst() | ||
.orElse(Status.UNKNOWN); | ||
} | ||
|
||
@Named("stringToGender") | ||
default Gender stringToGender(String gender) { | ||
return Arrays.stream(Gender.values()) | ||
.filter(g -> g.getGender().equalsIgnoreCase(gender)) | ||
.findFirst() | ||
.orElse(Gender.UNKNOWN); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/mate/academy/rickandmorty/repository/CharacterRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import mate.academy.rickandmorty.entity.Character; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface CharacterRepository extends JpaRepository<Character, Long> { | ||
Optional<Character> findByExternalId(Long externalId); | ||
|
||
@Query("SELECT c FROM Character c WHERE LOWER(c.name) LIKE LOWER(CONCAT('%', :name, '%'))") | ||
List<Character> findByNameContainingIgnoreCase(String name); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The regex
[a-zA-Z]+
will only match names consisting entirely of alphabetic characters without spaces or special characters. If the intention is to allow names with spaces or other characters, consider updating the regex accordingly.