3

In my Spring boot application, I have collection of Todos and a collection of Courses. In the view of the application, I return the collection of courses and display whatever course I need. The Todos are stored as 1 list which represents all the current Todos. What I would like to do is return a list of Todos for each course. So when the view is opened, the application would display the the course plus the individual todo list for that course.

Is there a way I can use the existing code to incorporate the new functionality. I have created the front end logic and would like to keep that. My initial idea was to add the the course id to the Todo.java, but that did not work.

Todo.java

@Document(collection="todos")
public class Todo {
@Id
private String id;

@NotBlank
@Size(max=250)
@Indexed(unique=true)
private String title;

private Boolean completed = false;  

private Date createdAt = new Date();

public Todo() {
    super();
}

public Todo(String title) {
    this.title = title;
}   

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Boolean getCompleted() {
    return completed;
}

public void setCompleted(Boolean completed) {
    this.completed = completed;
}

public Date getCreatedAt() {
    return createdAt;
}

public void setCreatedAt(Date createdAt) {
    this.createdAt = createdAt;
}

@Override
public String toString() {
    return String.format(
            "Todo[id=%s, title='%s', completed='%s']",
            id, title, completed);
}       
}

TodoRepository.java

@Repository
public interface TodoRepository extends MongoRepository<Todo, String> {
    public List<Todo> findAll();
    public Todo findOne(String id);
    public Todo save(Todo todo);
    public void delete(Todo todo);
}

Courses

@Document(collection = "courses")
public class Courses {
    @Id
    private String id;
    private String name;
    private String lecturer;
    private String picture;
    private String video;
    private String description;
    private String enroled;

    public Courses(){}

    public Courses(String name, String lecturer, String picture, String video, String description,String enroled) {
        this.name = name;
        this.lecturer = lecturer;
        this.picture = picture;
        this.video = video;
        this.description = description;
        this.enroled = enroled;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLecturer() {
        return lecturer;
    }
    public void setLecturer(String lecturer) {
        this.lecturer = lecturer;
    }
    public String getPicture() {
        return picture;
    }
    public void setPicture(String picture) {
        this.picture = picture;
    }
    public String getVideo() {
        return video;
    }
    public void setVideo(String video) {
        this.video = video;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getEnroled() {
        return enroled;
    }
    public void setEnroled(String enroled) {
        this.enroled = enroled;
    }

    @Override
    public String toString() {
        return "Courses{" +
                "id='" + id + "'" +
                ", name='" + name + "'" +
                ", lecturer='" + lecturer + "'" +
                ", description='" + description + "'" +
                '}';
    }
}
DevRight
  • 345
  • 1
  • 6
  • 25

0 Answers0