-
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
Home work #132
base: main
Are you sure you want to change the base?
Home work #132
Changes from all commits
72e4428
533453d
feff513
4362cf6
533dee5
7ccc8cb
29d5026
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 |
---|---|---|
@@ -1,12 +1,24 @@ | ||
package mate.academy.rickandmorty; | ||
|
||
import mate.academy.rickandmorty.service.CharacterService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
@Autowired | ||
private CharacterService characterService; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
|
||
} | ||
|
||
@Bean | ||
public CommandLineRunner commandLineRunner() { | ||
return args -> characterService.saveCharactersToDb(); | ||
} | ||
Comment on lines
+20
to
23
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. Remember to move Bean initialisations into @configuration class |
||
} |
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 { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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.CharacterResponseDto; | ||
import mate.academy.rickandmorty.service.CharacterService; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
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 = "For managing characters") | ||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/characters") | ||
public class CharacterController { | ||
private final CharacterService characterService; | ||
|
||
@GetMapping("/random") | ||
@Operation(summary = "Get random character", | ||
description = "You get a random character") | ||
public CharacterResponseDto getRandomCharacters() { | ||
return characterService.getRandomCharacter(); | ||
} | ||
|
||
@GetMapping("/by-name") | ||
@Operation(summary = "Find characters by name", | ||
description = "You can find characters by name") | ||
public List<CharacterResponseDto> findAllByName(@RequestParam String name) { | ||
return characterService.findAllByName(name); | ||
} | ||
|
||
@GetMapping | ||
@Operation(summary = "Get all characters", | ||
description = "You get all characters, default parameters: page=0, size=20, sort=id") | ||
public List<CharacterResponseDto> findAll(Pageable pageable) { | ||
return characterService.findAll(pageable); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
@Operation(summary = "Find character by id", | ||
description = "You can find character by id") | ||
public CharacterResponseDto findById(@PathVariable Long id) { | ||
return characterService.findById(id); | ||
} | ||
} |
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 ApiCharacterDto { | ||
private Long id; | ||
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.external; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class ApiInfoDto { | ||
private Long count; | ||
private Long pages; | ||
private String next; | ||
private String prev; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.List; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class ApiResponseDto { | ||
@JsonProperty("info") | ||
private ApiInfoDto apiInfoDto; | ||
private List<ApiCharacterDto> results; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mate.academy.rickandmorty.dto.internal; | ||
|
||
import lombok.Data; | ||
import mate.academy.rickandmorty.model.Gender; | ||
import mate.academy.rickandmorty.model.Status; | ||
|
||
@Data | ||
public class CharacterResponseDto { | ||
private Long id; | ||
private Long externalId; | ||
private String name; | ||
private Status status; | ||
private Gender gender; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,30 @@ | ||||||
package mate.academy.rickandmorty.mapper; | ||||||
|
||||||
import mate.academy.rickandmorty.dto.external.ApiCharacterDto; | ||||||
import mate.academy.rickandmorty.dto.internal.CharacterResponseDto; | ||||||
import mate.academy.rickandmorty.model.Character; | ||||||
import mate.academy.rickandmorty.model.Gender; | ||||||
import mate.academy.rickandmorty.model.Status; | ||||||
import org.springframework.stereotype.Component; | ||||||
|
||||||
@Component | ||||||
public class CharacterMapper { | ||||||
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. Consider using MapStruct to remove all boiler code :) |
||||||
public CharacterResponseDto toResponseDto(Character 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. be consistant with naming. It's either toCharacterResponseDto() and to Character() or toDto() and toModel()
Suggested change
|
||||||
CharacterResponseDto characterDto = new CharacterResponseDto(); | ||||||
characterDto.setId(character.getId()); | ||||||
characterDto.setExternalId(character.getExternalId()); | ||||||
characterDto.setName(character.getName()); | ||||||
characterDto.setStatus(character.getStatus()); | ||||||
characterDto.setGender(character.getGender()); | ||||||
return characterDto; | ||||||
} | ||||||
|
||||||
public Character toModel(ApiCharacterDto dtoForDb) { | ||||||
Character character = new Character(); | ||||||
character.setExternalId(dtoForDb.getId()); | ||||||
character.setName(dtoForDb.getName()); | ||||||
character.setGender(Gender.valueOf(dtoForDb.getGender().toUpperCase())); | ||||||
character.setStatus(Status.valueOf(dtoForDb.getStatus().toUpperCase())); | ||||||
return character; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,74 @@ | ||||||||||||
package mate.academy.rickandmorty.model; | ||||||||||||
|
||||||||||||
import jakarta.persistence.Entity; | ||||||||||||
import jakarta.persistence.EnumType; | ||||||||||||
import jakarta.persistence.Enumerated; | ||||||||||||
import jakarta.persistence.GeneratedValue; | ||||||||||||
import jakarta.persistence.GenerationType; | ||||||||||||
import jakarta.persistence.Id; | ||||||||||||
import jakarta.persistence.Table; | ||||||||||||
|
||||||||||||
@Entity | ||||||||||||
@Table(name = "characters") | ||||||||||||
public class 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.
Suggested change
|
||||||||||||
@Id | ||||||||||||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||||||||||||
private Long id; | ||||||||||||
private Long externalId; | ||||||||||||
private String name; | ||||||||||||
@Enumerated(EnumType.STRING) | ||||||||||||
private Status status; | ||||||||||||
@Enumerated(EnumType.STRING) | ||||||||||||
private Gender gender; | ||||||||||||
|
||||||||||||
public Long getId() { | ||||||||||||
return id; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public void setId(Long id) { | ||||||||||||
this.id = id; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public Long getExternalId() { | ||||||||||||
return externalId; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public void setExternalId(Long externalId) { | ||||||||||||
this.externalId = externalId; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public String getName() { | ||||||||||||
return name; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public void setName(String name) { | ||||||||||||
this.name = name; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public Status getStatus() { | ||||||||||||
return status; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public void setStatus(Status status) { | ||||||||||||
this.status = status; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public Gender getGender() { | ||||||||||||
return gender; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public void setGender(Gender gender) { | ||||||||||||
this.gender = gender; | ||||||||||||
} | ||||||||||||
|
||||||||||||
@Override | ||||||||||||
public String toString() { | ||||||||||||
return "Character{" | ||||||||||||
+ "id=" + id | ||||||||||||
+ ", externalId=" + externalId | ||||||||||||
+ ", name='" + name + '\'' | ||||||||||||
+ ", status=" + status | ||||||||||||
+ ", gender=" + gender | ||||||||||||
+ '}'; | ||||||||||||
} | ||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mate.academy.rickandmorty.model; | ||
|
||
public enum Gender { | ||
FEMALE("Female"), | ||
MALE("Male"), | ||
GENDERLESS("Genderless"), | ||
UNKNOWN("unknown"); | ||
private String value; | ||
|
||
Gender(String value) { | ||
this.value = value; | ||
} | ||
} |
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.