I am using Spring Boot to create a basic chore tracker webapp. It renders as shown below, however I have hard coded in these digits so you can see what I am trying to achieve. I am trying to query the DB but cannot get it working yet. Here is the source if interested.
How can I make a custom CRUD operation to feed to my Thymeleaf html page? https://github.com/deemack/chores
The DB has 3x tables as shown, with the completion table just having the foreign keys of the person table and chore table.
Completion.java (model)
package org.bluegoat.chores.model;
import jakarta.persistence.*;
import lombok.Data;
@Data
@Entity
@Table(name="completion")
public class Completion {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long completion_id;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name="person_id", nullable = false)
private Person person;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name="chore_id", nullable = false)
private Chore chore;
}
CompletionRepository (repository)
I think I need to add a method to the CompletionRepository for the specific query
package org.bluegoat.chores.repository;
import org.bluegoat.chores.model.Completion;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CompletionRepository extends JpaRepository<Completion, Long> {
//something like this that gets a list of chores of a given id, done by a person of a given id, and then count the rows in the resultset?
List<Completion> getChoreByPerson(long person_id, long chore_id);
}
CompletionService (service interface)
public interface CompletionService {
// And add this to the CompletionService interface?
List<Completion> getChoreByPerson(long person_id, long chore_id);
}
And finally Override the method in the CompletionServiceImpl CompletionServiceImpl (service)
@Service
public class CompletionServiceImpl implements CompletionService {
private CompletionRepository completionRepository;
public CompletionServiceImpl(CompletionRepository completionRepository) {
super();
this.completionRepository = completionRepository;
}
@Override
public Completion getChoreByPerson(long person_id, long chore_id) {
Optional<Completion> completion = completionRepository.getChoreByPerson(person_id, chore_id);
// Not sure what I am putting in here
}
From the hours of videos and reading, I think I am on the right track, I just can't seem to get it to click.
There is something wrong about the person_id and chore_id being long and the Completion model fields being of type Person and Chore.