Todo와 TodoService 만들기

Soo·2024년 3월 12일

Todo

  • id
  • username
  • description
  • targetDate
  • done

할 일을 저장할 객체를 만들겠습니다.

간단하게 id와 이름, 설명, 완료 시점, 완료 유무를 속성으로 가지는 객체입니다.

Todo.class

import java.time.LocalDate;

//Database(MySQL)
//Static List of todos => Database (H2, MySQL)
public class Todo {

    private int id;
    private String username;
    private String description;
    private LocalDate targetDate;
    private boolean done;

    public Todo(int id, String username, String description, LocalDate targetDate, boolean done) {
        this.id = id;
        this.username = username;
        this.description = description;
        this.targetDate = targetDate;
        this.done = done;
    }

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LocalDate getTargetDate() {
        return targetDate;
    }

    public void setTargetDate(LocalDate targetDate) {
        this.targetDate = targetDate;
    }

    public boolean isDone() {
        return done;
    }

    public void setDone(boolean done) {
        this.done = done;
    }

    @Override
    public String toString() {
        return "Todo{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", description='" + description + '\'' +
                ", targetDate=" + targetDate +
                ", done=" + done +
                '}';
    }
}

객체를 만들었으니 서비스를 만들어서 사용해보겠습니다.

DB를 사용하기 전에 간단하게 List 를 사용해서 객체를 저장하겠습니다.

그리고, ‘지금’은 username을 받아서 모든 Todo 객체를 반환하는 메소드가 있습니다.

TodoService.class

import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class TodoService {
    private static List<Todo> todos = new ArrayList<>();

    static {
        todos.add(new Todo(1, "test1", "Learn AWS",
                LocalDate.now().plusYears(1), false));
        todos.add(new Todo(2, "test1", "Learn DevOps",
                LocalDate.now().plusYears(2), false));
        todos.add(new Todo(3, "test1", "Learn Full Stack Development",
                LocalDate.now().plusYears(3), false));
    }

    public List<Todo> findByUsername(String username) {
        return todos;
    }

}

0개의 댓글