Skip to content

Commit

Permalink
fix: section 75
Browse files Browse the repository at this point in the history
  • Loading branch information
gleaming9 committed Nov 18, 2024
1 parent df4131e commit dd59f45
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 13 deletions.
17 changes: 5 additions & 12 deletions handler/add_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

type AddTask struct {
// - Store, + DB, Repo
DB *sqlx.DB
Repo *store.Repository
Validator *validator.Validate
Expand All @@ -28,7 +27,7 @@ func (at *AddTask) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// JSON 파싱 중 오류가 발생하면 500 상태 코드와 오류 메시지를 반환
RespondJSON(ctx, w, &ErrResponse{
Message: err.Error(),
}, http.StatusBadRequest)
}, http.StatusInternalServerError)
return
}
// Validator를 사용하여 Title 필드가 있는지 검증
Expand All @@ -41,27 +40,21 @@ func (at *AddTask) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

// Task 구조체를 생성하여, Title, Status, Created 필드를 설정
t := &entity.Task{
/*Title: b.Title, // 입력받은 Title을 설정
Status: "todo", // 초기 상태는 "todo"로 설정
Created: time.Now(), // 생성된 시간을 현재 시간으로 설정*/
Title: b.Title,
Status: entity.TaskStatusTodo,
Title: b.Title, // 요청에서 받은 Title을 Task 구조체에 대입
Status: entity.TaskStatusTodo, // Task의 상태를 "todo"로 설정
}
//id, err := store.Tasks.Add(t)
err := at.Repo.AddTask(ctx, at.DB, t)

if err != nil {
// Task 추가 중 오류가 발생하면 500 상태 코드와 오류 메시지를 반환
RespondJSON(ctx, w, &ErrResponse{
Message: err.Error(),
}, http.StatusInternalServerError)
return
}
// Task의 ID를 JSON 응답으로 반환
rsp := struct {
ID int `json:"id"` // JSON 응답에서 ID 필드의 이름을 "id"로 설정
}{ID: int(t.ID)} //ID: int(id)
ID entity.TaskID `json:"id"` // JSON 응답에서 ID 필드의 이름을 "id"로 설정
}{ID: t.ID}
RespondJSON(ctx, w, rsp, http.StatusOK)
}
2 changes: 1 addition & 1 deletion handler/testdata/add_task/bad_req.json.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"error": "Invalid request parameters"
"title": "Implement a handler"
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func run(ctx context.Context) error {
// 오류가 반환돼도 cleanup 함수를 실행한다.
defer cleanup()
if err != nil {
return err
}

s := NewServer(l, mux)
Expand Down
1 change: 1 addition & 0 deletions store/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package store

import (
"context"
_ "github.com/go-sql-driver/mysql" // 추가 해야 오류 안남
"go_todo_app/entity"
)

Expand Down
Binary file removed tmp/main
Binary file not shown.

0 comments on commit dd59f45

Please sign in to comment.