-
Notifications
You must be signed in to change notification settings - Fork 237
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
Created API with 2 methods #182
base: main
Are you sure you want to change the base?
Conversation
|
||
@Data | ||
public class CharacterDto { | ||
@NotBlank |
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.
@NotBlank | |
@NotNull |
public class CharacterDto { | ||
@NotBlank | ||
private Long id; | ||
@NotBlank |
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.
same
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@Column(nullable = false, name = "external_id") |
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.
@Column(nullable = false, name = "external_id") | |
@Column(nullable = false, unique = true) |
|
||
@Override | ||
public String getKey() { | ||
return "name"; |
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.
should be constant
Random random = new Random(); | ||
Long randomId = random.nextLong(characterRepository.count() - MIN_ID + 1) + MIN_ID; | ||
Character character = characterRepository.findById(randomId).orElseThrow(() -> | ||
new CharacterNotFoundException("Can`t find random character in database")); |
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.
new CharacterNotFoundException("Can`t find random character in database")); | |
new EntityNotFoundException("Can`t find random character in database")); |
characterDtoList.addAll(characterResponseDto.getResults()); | ||
nextPageUrl = characterResponseDto.getInfo().getNext(); | ||
} catch (URISyntaxException | IOException | InterruptedException e) { | ||
throw new FetchingDataException(e, "Can`t fetch data about characters. URL: " |
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.
throw new FetchingDataException(e, "Can`t fetch data about characters. URL: " | |
throw new CharacterExternalLoadException("Can`t fetch data about characters. URL: " + URL, e); |
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.
Good job overall!
name: status | ||
type: varchar(256) | ||
constraints: | ||
nullable: false |
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.
add empty line at EOF
|
||
@Override | ||
public CharacterDto getRandomCharacter() { | ||
Random random = new Random(); |
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.
create one random per class instance, not per each method invocation
@Override | ||
public CharacterDto getRandomCharacter() { | ||
Random random = new Random(); | ||
Long randomId = random.nextLong(characterRepository.count() - MIN_ID + 1) + MIN_ID; |
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.
Long randomId = random.nextLong(characterRepository.count() - MIN_ID + 1) + MIN_ID; | |
Long randomId = random.nextLong(characterRepository.count() - 1); |
sorry i don't get it
return characterRepository.findAll(characterSpecification) | ||
.stream() | ||
.map(characterMapper::toDto) | ||
.toList(); |
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.
btw you can declare mapping of list to list in mapstruct's interface
List<Character> characters = externalCharacterList.stream() | ||
.map(characterMapper::toModel) | ||
.toList(); |
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.
same, try to do it in interface
@Service | ||
@RequiredArgsConstructor | ||
public class CharacterServiceImpl implements CharacterService { | ||
public static final int MIN_ID = 1; |
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.
not needed, but why public?
|
||
@Component | ||
public class NameSpecificationProvider implements SpecificationProvider<Character> { | ||
public static final String SQL_WILDCARD = "%"; |
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 is not going to change in decades, okay to hardcode :)
or if you want a constant, create a string format with placeholder then
public Specification<Character> getSpecification(String param) { | ||
String pattern = SQL_WILDCARD + param + SQL_WILDCARD; | ||
return (root, query, criteriaBuilder) -> | ||
criteriaBuilder.like(criteriaBuilder.lower(root.get("name")), |
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.
criteriaBuilder.like(criteriaBuilder.lower(root.get("name")), | |
criteriaBuilder.like(criteriaBuilder.lower(root.get(NAME_KEY)), |
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.
Well done!
No description provided.