-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Rick and Morty API with random character wiki generation…
… and character search. Utilized MySQL for the main database and H2 for testing. Ensured identical configuration in both main and test properties files. Integrated Swagger for documentation
- Loading branch information
1 parent
c3bbe60
commit d157828
Showing
21 changed files
with
404 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,12 +1,26 @@ | ||
package mate.academy.rickandmorty; | ||
|
||
import mate.academy.rickandmorty.service.CharacterClient; | ||
import mate.academy.rickandmorty.service.CharacterService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
public class Application implements CommandLineRunner { | ||
@Autowired | ||
private CharacterService characterService; | ||
@Autowired | ||
private CharacterClient characterClient; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
|
||
} | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
characterService.saveAllCharacter(characterClient.getAllCharacters()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/mate/academy/rickandmorty/config/CharacterMapper.java
This file contains 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,17 @@ | ||
package mate.academy.rickandmorty.config; | ||
|
||
import mate.academy.rickandmorty.dto.external.CharacterExternalDto; | ||
import mate.academy.rickandmorty.model.Character; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CharacterMapper { | ||
public Character mapToCharacter(CharacterExternalDto dto) { | ||
Character character = new Character(); | ||
character.setExternalId(dto.getId()); | ||
character.setName(dto.getName()); | ||
character.setStatus(dto.getStatus()); | ||
character.setGender(dto.getGender()); | ||
return character; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/rickandmorty/config/RandomConfig.java
This file contains 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.config; | ||
|
||
import java.util.Random; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class RandomConfig { | ||
@Bean | ||
public Random random() { | ||
return new Random(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/mate/academy/rickandmorty/controller/CharacterController.java
This file contains 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,34 @@ | ||
package mate.academy.rickandmorty.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.CharacterSearchParam; | ||
import mate.academy.rickandmorty.model.Character; | ||
import mate.academy.rickandmorty.service.CharacterService; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/character/api") | ||
@Tag(name = "Character API", description = "API endpoints for managing characters.") | ||
public class CharacterController { | ||
private final CharacterService characterService; | ||
|
||
@GetMapping("/random") | ||
@Operation(summary = "Get a Random Character", | ||
description = "Retrieve a random character from the database.") | ||
public Character getRandomCharacter() { | ||
return characterService.getRandomCharacter(); | ||
} | ||
|
||
@GetMapping("/search") | ||
@Operation(summary = "Search Characters", description = "Search for characters by name.") | ||
public List<Character> searchCharacter(CharacterSearchParam name) { | ||
return characterService.search(name); | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/mate/academy/rickandmorty/dto/CharacterSearchParam.java
This file contains 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,4 @@ | ||
package mate.academy.rickandmorty.dto; | ||
|
||
public record CharacterSearchParam(String name) { | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/rickandmorty/dto/external/CharacterExternalDto.java
This file contains 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.external; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.Data; | ||
|
||
@Data | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class CharacterExternalDto { | ||
private String id; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/mate/academy/rickandmorty/dto/external/CharacterResponseDto.java
This file contains 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,24 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class CharacterResponseDto { | ||
private InfoDto info; | ||
private List<CharacterExternalDto> results; | ||
|
||
public CharacterResponseDto() { | ||
} | ||
|
||
public CharacterResponseDto(String json) throws IOException, JsonProcessingException { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
CharacterResponseDto characterResponseDto = | ||
objectMapper.readValue(json, CharacterResponseDto.class); | ||
this.info = characterResponseDto.getInfo(); | ||
this.results = characterResponseDto.getResults(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/rickandmorty/dto/external/InfoDto.java
This file contains 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,10 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.Data; | ||
|
||
@Data | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class InfoDto { | ||
private String next; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/mate/academy/rickandmorty/model/Character.java
This file contains 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,24 @@ | ||
package mate.academy.rickandmorty.model; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.Data; | ||
|
||
@Entity | ||
@Data | ||
@Table(name = "characters") | ||
public class Character { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@Column(name = "externalId") | ||
private String externalId; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/mate/academy/rickandmorty/repository/CharacterRepository.java
This file contains 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,9 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import mate.academy.rickandmorty.model.Character; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||
|
||
public interface CharacterRepository extends JpaRepository<Character, Long>, | ||
JpaSpecificationExecutor<Character> { | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/mate/academy/rickandmorty/repository/CharacterSpecificationBuilder.java
This file contains 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,23 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.CharacterSearchParam; | ||
import mate.academy.rickandmorty.model.Character; | ||
import mate.academy.rickandmorty.repository.user.NameSpecification; | ||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CharacterSpecificationBuilder implements SpecificationBuilder<Character> { | ||
private final NameSpecification nameSpecification; | ||
|
||
@Override | ||
public Specification<Character> build(CharacterSearchParam param) { | ||
Specification<Character> spec = Specification.where(null); | ||
if (param.name() != null && !param.name().isEmpty()) { | ||
spec = spec.and(nameSpecification.getSpecification(param.name())); | ||
} | ||
return spec; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/mate/academy/rickandmorty/repository/SpecificationBuilder.java
This file contains 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,8 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import mate.academy.rickandmorty.dto.CharacterSearchParam; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
public interface SpecificationBuilder<T> { | ||
Specification<T> build(CharacterSearchParam param); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/rickandmorty/repository/user/NameSpecification.java
This file contains 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.repository.user; | ||
|
||
import mate.academy.rickandmorty.model.Character; | ||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class NameSpecification { | ||
public Specification<Character> getSpecification(String param) { | ||
return (root, query, criteriaBuilder) | ||
-> criteriaBuilder.like(root.get("name"), "%" + param + "%"); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/mate/academy/rickandmorty/service/CharacterClient.java
This file contains 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,45 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.external.CharacterExternalDto; | ||
import mate.academy.rickandmorty.dto.external.CharacterResponseDto; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CharacterClient { | ||
private static final String BASE_URL = "https://rickandmortyapi.com/api/character"; | ||
private final ObjectMapper objectMapper; | ||
|
||
public List<CharacterExternalDto> getAllCharacters() { | ||
HttpClient httpClient = HttpClient.newHttpClient(); | ||
String url = BASE_URL; | ||
List<CharacterExternalDto> listDto = new ArrayList<>(); | ||
while (url != null) { | ||
HttpRequest httpRequest = HttpRequest.newBuilder() | ||
.GET() | ||
.uri(URI.create(url)) | ||
.build(); | ||
try { | ||
HttpResponse<String> httpResponse = httpClient.send(httpRequest, | ||
HttpResponse.BodyHandlers.ofString()); | ||
CharacterResponseDto responseDto = objectMapper.convertValue(httpResponse.body(), | ||
CharacterResponseDto.class); | ||
listDto.addAll(responseDto.getResults()); | ||
url = responseDto.getInfo().getNext(); | ||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
return listDto; | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/rickandmorty/service/CharacterService.java
This file contains 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.service; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.dto.CharacterSearchParam; | ||
import mate.academy.rickandmorty.dto.external.CharacterExternalDto; | ||
import mate.academy.rickandmorty.model.Character; | ||
|
||
public interface CharacterService { | ||
void saveAllCharacter(List<CharacterExternalDto> listDto); | ||
|
||
Character getRandomCharacter(); | ||
|
||
List<Character> search(CharacterSearchParam param); | ||
} |
Oops, something went wrong.