-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_todo.go
58 lines (52 loc) · 1.3 KB
/
get_todo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package handler
import (
"fmt"
"net/http"
"strconv"
"github.com/go-openapi/runtime/middleware"
"github.com/go-xorm/xorm"
"github.com/laincloud/todomvc/dao"
"github.com/laincloud/todomvc/gen/models"
"github.com/laincloud/todomvc/gen/restapi/operations/todo"
"go.uber.org/zap"
)
// GetTodo 获取一个 todo 项
func GetTodo(params todo.GetTodoParams, db *xorm.Engine, logger *zap.Logger) middleware.Responder {
todoID, err := strconv.ParseInt(params.ID, 10, 64)
if err != nil {
message := err.Error()
return todo.NewGetTodoDefault(http.StatusBadRequest).WithPayload(
&models.Error{
Message: &message,
},
)
}
todoRecord := new(dao.TodoRecord)
ok, err := db.ID(todoID).Get(todoRecord)
if err != nil {
message := err.Error()
return todo.NewGetTodoDefault(http.StatusInternalServerError).WithPayload(
&models.Error{
Message: &message,
},
)
}
if !ok {
message := fmt.Sprintf("%d does not exist in database", todoID)
return todo.NewGetTodoDefault(http.StatusBadRequest).WithPayload(
&models.Error{
Message: &message,
},
)
}
logger.Info("db.ID().Get() succeed.",
zap.Any("TodoRecord", todoRecord),
)
return todo.NewGetTodoOK().WithPayload(
&models.Todo{
ID: todoRecord.ID,
Title: &todoRecord.Title,
Completed: &todoRecord.Completed,
},
)
}