forked from mate-academy/jv-rick-and-morty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added solution to rick and morty API mate-academy#1
- Loading branch information
1 parent
c3bbe60
commit b1d7826
Showing
15 changed files
with
312 additions
and
3 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
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/rickandmorty/config/MapStructConfig.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 org.mapstruct.InjectionStrategy; | ||
import org.mapstruct.NullValueCheckStrategy; | ||
|
||
@org.mapstruct.MapperConfig( | ||
componentModel = "spring", | ||
injectionStrategy = InjectionStrategy.CONSTRUCTOR, | ||
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, | ||
implementationPackage = "<PACKAGE_NAME>.impl" | ||
) | ||
public class MapStructConfig { | ||
} |
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
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.internal.CharacterDto; | ||
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.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "Rick and Morty characters", | ||
description = "Endpoints for searching characters from Rick and Morty") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/characters") | ||
public class CharacterController { | ||
private final CharacterService characterService; | ||
|
||
@Operation(summary = "Get character", description = "Get random character") | ||
@GetMapping("/random") | ||
public CharacterDto getRandomCharacter() { | ||
return characterService.getRandomCharacter(); | ||
} | ||
|
||
@Operation(summary = "Get characters by name", | ||
description = "Get characters by name contains") | ||
@GetMapping | ||
public List<CharacterDto> findAllByNameContains(@RequestParam String name) { | ||
return characterService.searchCharacters(name); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/mate/academy/rickandmorty/dto/external/CharacterMetadataDto.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.dto.external; | ||
|
||
public record CharacterMetadataDto( | ||
Integer count, | ||
Integer pages, | ||
String next, | ||
String prev | ||
) { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/rickandmorty/dto/external/CharacterResponseDataDto.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,11 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.dto.internal.CreateCharacterRequestDto; | ||
|
||
public record CharacterResponseDataDto( | ||
CharacterMetadataDto info, | ||
List<CreateCharacterRequestDto> results | ||
) { | ||
} | ||
|
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/rickandmorty/dto/internal/CharacterDto.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.internal; | ||
|
||
public record CharacterDto( | ||
Long id, | ||
String externalId, | ||
String name, | ||
String status, | ||
String gender | ||
) { | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/mate/academy/rickandmorty/dto/internal/CreateCharacterRequestDto.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,12 @@ | ||
package mate.academy.rickandmorty.dto.internal; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record CreateCharacterRequestDto( | ||
@JsonProperty("id") | ||
String externalId, | ||
String name, | ||
String status, | ||
String gender | ||
) { | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/rickandmorty/mapper/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,14 @@ | ||
package mate.academy.rickandmorty.mapper; | ||
|
||
import mate.academy.rickandmorty.config.MapStructConfig; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
import mate.academy.rickandmorty.dto.internal.CreateCharacterRequestDto; | ||
import mate.academy.rickandmorty.model.CartoonCharacter; | ||
import org.mapstruct.Mapper; | ||
|
||
@Mapper(config = MapStructConfig.class) | ||
public interface CharacterMapper { | ||
CharacterDto toDto(CartoonCharacter character); | ||
|
||
CartoonCharacter toModel(CreateCharacterRequestDto requestDto); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/mate/academy/rickandmorty/model/CartoonCharacter.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.model; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
public class CartoonCharacter { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String externalId; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.model.CartoonCharacter; | ||
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<CartoonCharacter, Long> { | ||
List<CartoonCharacter> findAllByNameContainingIgnoreCase(String name); | ||
|
||
@Query(value = "FROM CartoonCharacter ORDER BY RAND() LIMIT 1") | ||
CartoonCharacter getRandom(); | ||
} |
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
import mate.academy.rickandmorty.dto.internal.CreateCharacterRequestDto; | ||
|
||
public interface CharacterService { | ||
|
||
List<CharacterDto> searchCharacters(String name); | ||
|
||
CharacterDto getRandomCharacter(); | ||
|
||
CharacterDto save(CreateCharacterRequestDto requestDto); | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/mate/academy/rickandmorty/service/CharactersClient.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,51 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.annotation.PostConstruct; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.external.CharacterResponseDataDto; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CharactersClient { | ||
private static final String BASE_URL = "https://rickandmortyapi.com/api/character"; | ||
private final ObjectMapper mapper; | ||
private final CharacterService characterService; | ||
|
||
@PostConstruct | ||
public void dbInit() { | ||
getCharactersInfo(); | ||
} | ||
|
||
public void getCharactersInfo() { | ||
HttpClient httpClient = HttpClient.newHttpClient(); | ||
String url = BASE_URL; | ||
while (url != null) { | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.GET() | ||
.uri(URI.create(url)) | ||
.build(); | ||
try { | ||
HttpResponse<String> response = httpClient.send( | ||
request, | ||
HttpResponse.BodyHandlers.ofString()); | ||
CharacterResponseDataDto characterResponseDataDto = mapper.readValue( | ||
response.body(), | ||
CharacterResponseDataDto.class); | ||
characterResponseDataDto.results() | ||
.stream().map(createCharacterRequestDto -> | ||
characterService.save(createCharacterRequestDto)) | ||
.toList(); | ||
url = characterResponseDataDto.info().next(); | ||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException("Can't get data from API", e); | ||
} | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.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,37 @@ | ||
package mate.academy.rickandmorty.service.impl; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
import mate.academy.rickandmorty.dto.internal.CreateCharacterRequestDto; | ||
import mate.academy.rickandmorty.mapper.CharacterMapper; | ||
import mate.academy.rickandmorty.model.CartoonCharacter; | ||
import mate.academy.rickandmorty.repository.CharacterRepository; | ||
import mate.academy.rickandmorty.service.CharacterService; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CharacterServiceImpl implements CharacterService { | ||
private final CharacterMapper mapper; | ||
private final CharacterRepository repository; | ||
|
||
@Override | ||
public List<CharacterDto> searchCharacters(String name) { | ||
return repository.findAllByNameContainingIgnoreCase(name) | ||
.stream() | ||
.map(mapper::toDto) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public CharacterDto getRandomCharacter() { | ||
return mapper.toDto(repository.getRandom()); | ||
} | ||
|
||
@Override | ||
public CharacterDto save(CreateCharacterRequestDto requestDto) { | ||
CartoonCharacter character = mapper.toModel(requestDto); | ||
return mapper.toDto(repository.save(character)); | ||
} | ||
} |
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 +1,7 @@ | ||
spring.datasource.url=jdbc:mysql://localhost:3306/rickandmortydb | ||
spring.datasource.username=root | ||
spring.datasource.password=Matepass1! | ||
spring.jpa.hibernate.ddl-auto=create-drop | ||
spring.jpa.show-sql=true | ||
spring.h2.console.enabled=true | ||
|
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 |
---|---|---|
|
@@ -9,5 +9,4 @@ class ApplicationTests { | |
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |