-
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.
Implemented random character generator, and ability to search by name
- Loading branch information
1 parent
c3bbe60
commit f3c1c57
Showing
18 changed files
with
419 additions
and
7 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/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.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 { | ||
} |
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
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.CartoonCharacterDto; | ||
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 Mort API", | ||
description = "Endpoints for random character or finding a " | ||
+ "character that contains specific name") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping(value = "/api") | ||
public class CharacterController { | ||
|
||
private final CharacterService characterService; | ||
|
||
@GetMapping("/random") | ||
@Operation( | ||
summary = "Generate random character", | ||
description = "Generates a wiki of a random character from Rick and Morty universe") | ||
public CartoonCharacterDto getRandomCharacter() { | ||
return characterService.getRandomCharacter(); | ||
} | ||
|
||
@GetMapping("/search") | ||
@Operation( | ||
summary = "Search by name", | ||
description = "Returns list of characters containing the search name") | ||
public List<CartoonCharacterDto> searchByName(@RequestParam String name) { | ||
return characterService.searchByName(name); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/mate/academy/rickandmorty/dto/external/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,20 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.List; | ||
|
||
public record CharacterDto( | ||
@JsonProperty("id") | ||
Long externalId, | ||
String name, | ||
String status, | ||
String species, | ||
String type, | ||
String gender, | ||
String image, | ||
String url, | ||
String created, | ||
OriginDto origin, | ||
LocationDto location, | ||
List<String> episode | ||
) {} |
11 changes: 11 additions & 0 deletions
11
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,11 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class CharacterMetadataDto { | ||
private int count; | ||
private int pages; | ||
private String next; | ||
private 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 lombok.Data; | ||
|
||
@Data | ||
public class CharacterResponseDataDto { | ||
|
||
private CharacterMetadataDto info; | ||
private List<CharacterDto> results; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/mate/academy/rickandmorty/dto/external/LocationDto.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,6 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
public record LocationDto( | ||
String name, | ||
String url | ||
) {} |
6 changes: 6 additions & 0 deletions
6
src/main/java/mate/academy/rickandmorty/dto/external/OriginDto.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,6 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
public record OriginDto( | ||
String name, | ||
String url | ||
) {} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/rickandmorty/dto/internal/CartoonCharacterDto.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.internal; | ||
|
||
public record CartoonCharacterDto( | ||
Long id, | ||
String externalId, | ||
String name, | ||
String status, | ||
String type, | ||
String gender, | ||
String image | ||
) {} |
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
package mate.academy.rickandmorty.mapper; | ||
|
||
import mate.academy.rickandmorty.config.MapperConfig; | ||
import mate.academy.rickandmorty.dto.external.CharacterDto; | ||
import mate.academy.rickandmorty.dto.internal.CartoonCharacterDto; | ||
import mate.academy.rickandmorty.model.Character; | ||
import org.mapstruct.Mapper; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface CharacterMapper { | ||
|
||
CartoonCharacterDto toDto(Character character); | ||
|
||
Character toModel(CharacterDto characterDto); | ||
} |
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
package mate.academy.rickandmorty.model; | ||
|
||
import jakarta.persistence.CascadeType; | ||
import jakarta.persistence.ElementCollection; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import java.util.List; | ||
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 species; | ||
private String type; | ||
private String gender; | ||
private String image; | ||
private String url; | ||
private String created; | ||
|
||
@ManyToOne(cascade = CascadeType.PERSIST) | ||
@JoinColumn(name = "origins_id") | ||
private Origin origin; | ||
|
||
@ManyToOne(cascade = CascadeType.PERSIST) | ||
@JoinColumn(name = "locations_id") | ||
private Location location; | ||
|
||
@ElementCollection(fetch = FetchType.EAGER) | ||
private List<String> episode; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/mate/academy/rickandmorty/model/Location.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,21 @@ | ||
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; | ||
|
||
@Entity | ||
@Data | ||
@Table(name = "locations") | ||
public class Location { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String name; | ||
private String url; | ||
} |
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,21 @@ | ||
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; | ||
|
||
@Entity | ||
@Data | ||
@Table(name = "origins") | ||
public class Origin { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String name; | ||
private String url; | ||
} |
10 changes: 10 additions & 0 deletions
10
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,10 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.model.Character; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CharacterRepository extends JpaRepository<Character, Long> { | ||
|
||
List<Character> findAllByNameContainsIgnoreCase(String name); | ||
} |
Oops, something went wrong.