-
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
first commit #29
base: main
Are you sure you want to change the base?
first commit #29
Changes from 5 commits
3ec5a66
5c729e3
18854cf
1984f93
5b3e969
193f151
a4bac51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package mate.academy.rickandmorty.controllers; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import java.util.List; | ||
import mate.academy.rickandmorty.models.Character; | ||
import mate.academy.rickandmorty.service.CharacterService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
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 = "Character controller", description = "Endpoints for API") | ||
@RestController | ||
@RequestMapping(value = "/characters") | ||
public class CharacterController { | ||
@Autowired | ||
private CharacterService characterService; | ||
|
||
@Operation(summary = "Find all characters by name", description = "Get all characters by name") | ||
@GetMapping("/by-name") | ||
public List<Character> getAllByName(@RequestParam("name") String name) { | ||
return characterService.getAllCharactersByName(name); | ||
} | ||
|
||
@Operation(summary = "Find random character", description = "Get random character") | ||
@GetMapping("/random") | ||
public Character getRandomCharacter() { | ||
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. URI convention violation. |
||
return characterService.getRandomCharacter(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mate.academy.rickandmorty.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Data; | ||
|
||
@Data | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class CharacterDtoResponse { | ||
@JsonProperty(value = "id") | ||
private Long externalId; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package mate.academy.rickandmorty.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import java.util.List; | ||
import lombok.Data; | ||
|
||
@Data | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class CharactersDtoResponse { | ||
private List<CharacterDtoResponse> results; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mate.academy.rickandmorty.mappers; | ||
|
||
import mate.academy.rickandmorty.dto.CharacterDtoResponse; | ||
import mate.academy.rickandmorty.models.Character; | ||
|
||
public interface CharacterMapper { | ||
Character toModel(CharacterDtoResponse rickMortyDtoRequest); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package mate.academy.rickandmorty.mappers; | ||
|
||
import mate.academy.rickandmorty.dto.CharacterDtoResponse; | ||
import mate.academy.rickandmorty.models.Character; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CharacterMapperImpl implements CharacterMapper { | ||
public CharacterMapperImpl() { | ||
} | ||
|
||
public Character toModel(CharacterDtoResponse rickMortyDtoRequest) { | ||
if (rickMortyDtoRequest == null) { | ||
return null; | ||
} else { | ||
Character character = new Character(); | ||
if (rickMortyDtoRequest.getExternalId() != null) { | ||
character.setExternalId(rickMortyDtoRequest.getExternalId()); | ||
} | ||
|
||
if (rickMortyDtoRequest.getName() != null) { | ||
character.setName(rickMortyDtoRequest.getName()); | ||
} | ||
|
||
if (rickMortyDtoRequest.getStatus() != null) { | ||
character.setStatus(rickMortyDtoRequest.getStatus()); | ||
} | ||
|
||
if (rickMortyDtoRequest.getGender() != null) { | ||
character.setGender(rickMortyDtoRequest.getGender()); | ||
} | ||
|
||
return character; | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package mate.academy.rickandmorty.models; | ||
|
||
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; | ||
private Long externalId; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.models.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> { | ||
List<Character> findRickMortiesByNameContainingIgnoreCase(String name); | ||
|
||
Character findRickMortyById(Long externalId); | ||
|
||
@Query(value = "SELECT MAX(id) FROM characters;",nativeQuery = true) | ||
Long getMaxId(); | ||
} |
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.models.Character; | ||
|
||
public interface CharacterService { | ||
List<Character> getAllCharactersByName(String name); | ||
|
||
Character getRandomCharacter(); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,53 @@ | ||||||
package mate.academy.rickandmorty.service.implementation; | ||||||
|
||||||
import jakarta.annotation.PostConstruct; | ||||||
import java.util.List; | ||||||
import java.util.Random; | ||||||
import java.util.stream.Collectors; | ||||||
import mate.academy.rickandmorty.dto.CharactersDtoResponse; | ||||||
import mate.academy.rickandmorty.mappers.CharacterMapper; | ||||||
import mate.academy.rickandmorty.models.Character; | ||||||
import mate.academy.rickandmorty.repository.CharacterRepository; | ||||||
import mate.academy.rickandmorty.service.CharacterService; | ||||||
import org.springframework.beans.factory.annotation.Autowired; | ||||||
import org.springframework.stereotype.Service; | ||||||
import org.springframework.web.client.RestTemplate; | ||||||
|
||||||
@Service | ||||||
public class CharacterServiceImpl implements CharacterService { | ||||||
private static final String CHARACTER_URL = "https://rickandmortyapi.com/api/character"; | ||||||
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. Suggestion: move to property file.
Suggested change
|
||||||
private final Random random; | ||||||
private final RestTemplate restTemplate; | ||||||
private final CharacterRepository characterRepository; | ||||||
private final CharacterMapper rickMortyMapper; | ||||||
|
||||||
@Autowired | ||||||
public CharacterServiceImpl(RestTemplate restTemplate, | ||||||
CharacterRepository characterRepository, | ||||||
CharacterMapper rickMortyMapper) { | ||||||
this.restTemplate = restTemplate; | ||||||
this.characterRepository = characterRepository; | ||||||
this.rickMortyMapper = rickMortyMapper; | ||||||
random = new Random(); | ||||||
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. use Spring @bean for injection |
||||||
} | ||||||
|
||||||
@PostConstruct | ||||||
private void downloadToDB() { | ||||||
CharactersDtoResponse charactersDtoResponse = restTemplate.getForObject(CHARACTER_URL, | ||||||
CharactersDtoResponse.class); | ||||||
assert charactersDtoResponse != null; | ||||||
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. Using
Suggested change
|
||||||
characterRepository.saveAll(charactersDtoResponse.getResults() | ||||||
.stream() | ||||||
.map(rickMortyMapper::toModel) | ||||||
.collect(Collectors.toList())); | ||||||
} | ||||||
|
||||||
public List<Character> getAllCharactersByName(String name) { | ||||||
return characterRepository.findRickMortiesByNameContainingIgnoreCase(name); | ||||||
} | ||||||
|
||||||
public Character getRandomCharacter() { | ||||||
Long maxId = characterRepository.getMaxId(); | ||||||
return characterRepository.findRickMortyById(random.nextLong(1, maxId)); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1 +1,6 @@ | ||||
|
||||
spring.jpa.hibernate.ddl-auto=update | ||||
spring.datasource.url=jdbc:mysql://localhost:3306/rick_morty | ||||
spring.datasource.username=serg | ||||
spring.datasource.password=22062020 | ||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | ||||
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. Is this property required to be used?
Suggested change
|
||||
spring.jpa.show-sql= true |
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.
URI convention violation.
see:
https://restfulapi.net/resource-naming/