-
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
init commit #151
Open
TonyH277
wants to merge
4
commits into
mate-academy:main
Choose a base branch
from
TonyH277: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
init commit #151
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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
28 changes: 28 additions & 0 deletions
28
src/main/java/mate/academy/rickandmorty/config/DataLoader.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,28 @@ | ||
package mate.academy.rickandmorty.config; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.mapper.CharacterMapper; | ||
import mate.academy.rickandmorty.model.Character; | ||
import mate.academy.rickandmorty.repository.CharacterRepository; | ||
import mate.academy.rickandmorty.service.impl.DataFetchServiceImpl; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class DataLoader implements ApplicationRunner { | ||
private final DataFetchServiceImpl dataFetcher; | ||
private final CharacterMapper characterMapper; | ||
private final CharacterRepository characterRepository; | ||
|
||
@Override | ||
public void run(ApplicationArguments args) { | ||
List<Character> characters = dataFetcher.fetch() | ||
.stream() | ||
.map(characterMapper::toModel) | ||
.toList(); | ||
characterRepository.saveAll(characters); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
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,12 @@ | ||
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 MapperConfig { | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/mate/academy/rickandmorty/config/RestTemplateConfig.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,31 @@ | ||
package mate.academy.rickandmorty.config; | ||
|
||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
import org.apache.hc.client5.http.impl.classic.HttpClients; | ||
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class RestTemplateConfig { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
HttpComponentsClientHttpRequestFactory httpRequestFactory | ||
= new HttpComponentsClientHttpRequestFactory(); | ||
httpRequestFactory.setHttpClient(httpClient()); | ||
return new RestTemplate(httpRequestFactory); | ||
} | ||
|
||
@Bean | ||
public CloseableHttpClient httpClient() { | ||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); | ||
connManager.setMaxTotal(100); | ||
connManager.setDefaultMaxPerRoute(20); | ||
return HttpClients.custom() | ||
.setConnectionManager(connManager) | ||
.build(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
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.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.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "Rick and Morty character management", | ||
description = "Endpoints for managing characters") | ||
@RestController | ||
@RequestMapping("/characters") | ||
@RequiredArgsConstructor | ||
public class CharacterController { | ||
private final CharacterService characterService; | ||
|
||
@Operation(summary = "Get one character", | ||
description = "Get random character from Rick and Morty universe") | ||
@GetMapping | ||
public Character getRandomCharacter() { | ||
return characterService.generateRandomCharacter(); | ||
} | ||
|
||
@Operation(summary = "Get list of characters by name contains 'row'", | ||
description = "Get list of character from Rick and Morty universe " | ||
+ "where character name like '%:row%'") | ||
@GetMapping("/byNameContains") | ||
public List<Character> findCharacterNameContains(@RequestParam String row) { | ||
return characterService.findCharacterNameContains(row); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/mate/academy/rickandmorty/dto/FetchedDataDto.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; | ||
|
||
import java.util.List; | ||
import lombok.Data; | ||
import mate.academy.rickandmorty.dto.jsonmapping.CharacterMappingDto; | ||
import mate.academy.rickandmorty.dto.jsonmapping.InfoMappingDto; | ||
|
||
@Data | ||
public class FetchedDataDto { | ||
private InfoMappingDto info; | ||
private List<CharacterMappingDto> results; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/rickandmorty/dto/jsonmapping/CharacterMappingDto.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.jsonmapping; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class CharacterMappingDto { | ||
private int id; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/rickandmorty/dto/jsonmapping/InfoMappingDto.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.jsonmapping; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class InfoMappingDto { | ||
private int count; | ||
private int pages; | ||
private String next; | ||
private String prev; | ||
} |
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.MapperConfig; | ||
import mate.academy.rickandmorty.dto.jsonmapping.CharacterMappingDto; | ||
import mate.academy.rickandmorty.model.Character; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface CharacterMapper { | ||
@Mapping(target = "externalId", source = "id") | ||
@Mapping(target = "id", ignore = true) | ||
Character toModel(CharacterMappingDto characterDto); | ||
} |
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
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.Getter; | ||
import lombok.Setter; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
@Table(name = "characters") | ||
public class Character { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@Column(name = "external_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/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,13 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.model.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> { | ||
@Query("SELECT c FROM Character c WHERE LOWER(c.name) LIKE %:row%") | ||
List<Character> findByNameContainsIgnoreCase(String row); | ||
} |
10 changes: 10 additions & 0 deletions
10
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,10 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.model.Character; | ||
|
||
public interface CharacterService { | ||
Character generateRandomCharacter(); | ||
|
||
List<Character> findCharacterNameContains(String row); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/mate/academy/rickandmorty/service/DataFetchService.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.service; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.dto.jsonmapping.CharacterMappingDto; | ||
|
||
public interface DataFetchService { | ||
List<CharacterMappingDto> fetch(); | ||
} |
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
package mate.academy.rickandmorty.service.impl; | ||
|
||
import jakarta.transaction.Transactional; | ||
import java.util.List; | ||
import java.util.Random; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.model.Character; | ||
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 CharacterRepository characterRepository; | ||
private Random random = new Random(); | ||
|
||
@Override | ||
@Transactional | ||
public Character generateRandomCharacter() { | ||
long charactersAmount = characterRepository.count(); | ||
long id = random.nextLong(charactersAmount); | ||
return characterRepository.findById(id) | ||
.orElseThrow(() -> new IllegalStateException("Character not found with id: " + id)); | ||
} | ||
|
||
@Override | ||
public List<Character> findCharacterNameContains(String row) { | ||
return characterRepository.findByNameContainsIgnoreCase(row); | ||
} | ||
} |
Oops, something went wrong.
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.
This beans should be created by spring, no need to create them manually, if you want to configure them somehow you can you application properties
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.
In my case I add those, because spring said
"Parameter 0 of constructor in mate.academy.rickandmorty.config.DataFetcher required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.
"