-
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 4 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.RickMortyService; | ||||||
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 RickMortyService rickMortyService; | ||||||
|
||||||
@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 rickMortyService.getAllCharactersByName(name); | ||||||
} | ||||||
|
||||||
@Operation(summary = "Find random character", description = "Get random character") | ||||||
@GetMapping("/random-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. /characters/random-character ? Too many "character"
Suggested change
|
||||||
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 rickMortyService.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 RickMortyMapper { | ||||||
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.
Suggested change
|
||||||
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 RickMortyMapperImpl implements RickMortyMapper { | ||
public RickMortyMapperImpl() { | ||
} | ||
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. Why do you need it? 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. I don't undestand the question! I need this implementation because we must convert responce object to model)) 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. mapstruct should create Mapper impl - or why do you need its dependency in pom.xml? 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. I have a problem with this!)) I did with Mapper, but during maven test I have checkstyle error in target package, MapperImpl class(which generates in the runtime) and I can't fix this! |
||
|
||
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 RickMortyRepository extends JpaRepository<Character, Long> { | ||||||
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.
Suggested change
|
||||||
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 RickMortyService { | ||||||
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.
Suggested change
|
||||||
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.RickMortyMapper; | ||
import mate.academy.rickandmorty.models.Character; | ||
import mate.academy.rickandmorty.repository.RickMortyRepository; | ||
import mate.academy.rickandmorty.service.RickMortyService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Service | ||
public class RickMortyServiceImpl implements RickMortyService { | ||
private static final String CHARACTER_URL = "https://rickandmortyapi.com/api/character"; | ||
private final Random random; | ||
private final RestTemplate restTemplate; | ||
private final RickMortyRepository rickMortyRepository; | ||
private final RickMortyMapper rickMortyMapper; | ||
|
||
@Autowired | ||
public RickMortyServiceImpl(RestTemplate restTemplate, | ||
RickMortyRepository rickMortyRepository, | ||
RickMortyMapper rickMortyMapper) { | ||
this.restTemplate = restTemplate; | ||
this.rickMortyRepository = rickMortyRepository; | ||
this.rickMortyMapper = rickMortyMapper; | ||
random = new Random(); | ||
} | ||
|
||
@PostConstruct | ||
private void downloadToDB() { | ||
CharactersDtoResponse charactersDtoResponse = restTemplate.getForObject(CHARACTER_URL, | ||
CharactersDtoResponse.class); | ||
assert charactersDtoResponse != null; | ||
rickMortyRepository.saveAll(charactersDtoResponse.getResults() | ||
.stream() | ||
.map(rickMortyMapper::toModel) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
public List<Character> getAllCharactersByName(String name) { | ||
return rickMortyRepository.findRickMortiesByNameContainingIgnoreCase(name); | ||
} | ||
|
||
public Character getRandomCharacter() { | ||
Long maxId = rickMortyRepository.getMaxId(); | ||
return rickMortyRepository.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/