I am trying to get a list of skills sorted by their number of likes in Spring Boot and Thymeleaf with Pageable but I´m bumping into problems. I´m not sure how to do this. What would be a good way to approach this problem?
Edit. Error: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountController': Unsatisfied dependency expressed through field 'skillRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'skillRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List projekti.Skill.SkillRepository.findAllSkills(projekti.Account.Account,org.springframework.data.domain.Pageable)! No property findAllSkills found for type Skill!
AccountController.java
@GetMapping("/profile/{path}")
public String profile(Model model, @PathVariable String path) {
Account account = accountRepository.findByPath(path);
Pageable pageable = PageRequest.of(0, 5, Sort.by("likes").descending());
model.addAttribute("skills", skillRepository.findAllSkills(account, pageable));
return "profile";
}
Skill.java
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Skill extends AbstractPersistable<Long> {
@NotEmpty
@Size(min = 1, max = 50)
@Column
private String name;
@ManyToMany
@JoinTable(
name="who_liked",
joinColumns=
@JoinColumn(name="skill_id", referencedColumnName="id"),
inverseJoinColumns=
@JoinColumn(name="like_account_id", referencedColumnName="id"))
private List<Account> likes = new ArrayList<>();
}
SkillRepository.java
public interface SkillRepository extends JpaRepository<Skill, Long> {
List<Skill> findAllSkills(Account account, Pageable pageable);
}
Profile.html
<li th:each="skill : ${skills}">
<span th:text="${skill.name}">skill</span>
<form th:action="@{/profile/{path}/skill/{id}/like(path=${path}, id=${skill.id})}" method="POST">
<input type="hidden" id="skillLike" name="skillLike" value="skillLike"/>
<button type="submit"><span th:text="${#lists.size(skill.likes)}">likes</span></button>
<span> likes</span>
</form>
</li>