-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Saved necessary data from an external DB and implemented two methods …
…in controller.
- Loading branch information
Showing
10 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/rickandmorty/config/MapperConfig.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.controller.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 MapperConfig { | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/mate/academy/rickandmorty/controller/RickAndMortyController.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,2 @@ | ||
package mate.academy.rickandmorty.controller;public class RickAndMortyController { | ||
} |
5 changes: 5 additions & 0 deletions
5
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,5 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
public class CharacterResponseDto { | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/rickandmorty/dto/external/RickAndMortyCharacterDto.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 lombok.Data; | ||
|
||
@Data | ||
public class RickAndMortyCharacter { | ||
private Long id; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/mate/academy/rickandmorty/dto/internal/RickAndMortyCharacter.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,22 @@ | ||
package mate.academy.rickandmorty.model; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Entity | ||
@Table(name = "rick_and_morty_characters") | ||
public class RickAndMortyCharacter { | ||
@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/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.controller.mapper; | ||
|
||
import mate.academy.rickandmorty.controller.config.MapperConfig; | ||
import mate.academy.rickandmorty.dto.external.RickAndMortyCharacterDto; | ||
import mate.academy.rickandmorty.model.RickAndMortyCharacter; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface CharacterMapper { | ||
@Mapping(target = "id", ignore = true) | ||
@Mapping(target = "externalId", source = "id") | ||
RickAndMortyCharacter toModel(RickAndMortyCharacterDto characterDto); | ||
} |
10 changes: 10 additions & 0 deletions
10
.../java/mate/academy/rickandmorty/repository/character/RickAndMortyCharacterRepository.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.repository; | ||
|
||
import mate.academy.rickandmorty.model.RickAndMortyCharacter; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||
|
||
public interface RickAndMortyCharacterRepository | ||
extends JpaRepository<RickAndMortyCharacter, Long>, | ||
JpaSpecificationExecutor<RickAndMortyCharacter> { | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/mate/academy/rickandmorty/service/RickAndMortyClient.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,2 @@ | ||
package mate.academy.rickandmorty.service;public class RickAndMortyClient { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/rickandmorty/service/character/RickAndMortyCharacterService.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,7 @@ | ||
package mate.academy.rickandmorty.service.rickandmortcharacter; | ||
|
||
import mate.academy.rickandmorty.model.RickAndMortyCharacter; | ||
|
||
public interface RickAndMortyCharacterService { | ||
RickAndMortyCharacter getRandomCharacter(); | ||
} |
37 changes: 37 additions & 0 deletions
37
...in/java/mate/academy/rickandmorty/service/character/RickAndMortyCharacterServiceImpl.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.rickandmortcharacter; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.controller.mapper.CharacterMapper; | ||
import mate.academy.rickandmorty.dto.external.RickAndMortyCharacterDto; | ||
import mate.academy.rickandmorty.model.RickAndMortyCharacter; | ||
import mate.academy.rickandmorty.repository.RickAndMortyCharacterRepository; | ||
import mate.academy.rickandmorty.service.RickAndMortyClient; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Random; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class RickAndMortyCharacterServiceImpl implements RickAndMortyCharacterService { | ||
private final RickAndMortyClient client; | ||
private final RickAndMortyCharacterRepository repository; | ||
private final CharacterMapper characterMapper; | ||
|
||
@PostConstruct | ||
public void saveAllCharacters() { | ||
List<RickAndMortyCharacter> list = | ||
client.getAllCharacter() | ||
.stream() | ||
.map(characterMapper::toModel) | ||
.toList(); | ||
repository.saveAll(list); | ||
} | ||
|
||
@Override | ||
public RickAndMortyCharacter getRandomCharacter() { | ||
List<RickAndMortyCharacter> all = repository.findAll(); | ||
return all.get(new Random().nextInt(all.size())); | ||
} | ||
} |