From dc37f5374a458d2d4de047843fe95bbf9f1e654e Mon Sep 17 00:00:00 2001 From: mi-bear Date: Tue, 14 May 2019 22:34:39 +0900 Subject: [PATCH 1/2] Add codelab contents taskmanager --- taskmanager/function.go | 49 ++++++++++++++++++++++++ taskmanager/task.go | 83 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 128 insertions(+), 4 deletions(-) diff --git a/taskmanager/function.go b/taskmanager/function.go index 5575dbf..9ab71a1 100644 --- a/taskmanager/function.go +++ b/taskmanager/function.go @@ -33,12 +33,61 @@ func TaskManager(w http.ResponseWriter, r *http.Request) { // 一覧取得 case http.MethodGet: + t, err := getAllTask() + if err != nil { + e := errors.Errorf("get error: %v", err) + responseWrite(w, http.StatusInternalServerError, e.Error(), e) + return + } + + if len(t) < 1 { + responseWrite(w, http.StatusOK, "0 tickets", nil) + return + } + + b, err := json.Marshal(t) + if err != nil { + e := errors.Errorf("json marshal error: %v", err) + responseWrite(w, http.StatusInternalServerError, e.Error(), e) + return + } + + w.WriteHeader(http.StatusOK) + w.Write(b) // ステータス変更 case http.MethodPatch: + param, code, err := getJSON(r.Header.Get("Content-Type"), r.Body) + if err != nil { + responseWrite(w, code, err.Error(), err) + return + } + + t := setTask(param.ID, param.Title, param.Status) + if err := t.update(); err != nil { + e := errors.Errorf("patch error: %v", err) + responseWrite(w, http.StatusInternalServerError, e.Error(), e) + return + } + msg := fmt.Sprintf("%v updated", t) + responseWrite(w, http.StatusOK, msg, nil) // 削除 case http.MethodDelete: + param, code, err := getJSON(r.Header.Get("Content-Type"), r.Body) + if err != nil { + responseWrite(w, code, err.Error(), err) + return + } + + t := setTask(param.ID, "", 0) + if err := t.delete(); err != nil { + e := errors.Errorf("delete error: %v", err) + responseWrite(w, http.StatusInternalServerError, e.Error(), e) + return + } + msg := fmt.Sprintf("%v deleted", t) + responseWrite(w, http.StatusOK, msg, nil) default: e := errors.New("method not allowed") diff --git a/taskmanager/task.go b/taskmanager/task.go index 1bad2e0..0496828 100644 --- a/taskmanager/task.go +++ b/taskmanager/task.go @@ -10,10 +10,10 @@ import ( // Task is the structure of the task. type Task struct { - ID int64 `datastore:"-"` - Title string `datastore:"title"` - Status Status `datastore:"status"` - CreatedAt time.Time `datastore:"createdAt"` + ID int64 `datastore:"-" json:"id"` + Title string `datastore:"title" json:"title"` + Status Status `datastore:"status" json:"status"` + CreatedAt time.Time `datastore:"createdAt" json:"createdAt"` } func newTask(title string) *Task { @@ -36,3 +36,78 @@ func (t Task) add() error { _, err = client.Put(ctx, newKey, &t) return err } + +func getAllTask() ([]Task, error) { + ctx := context.Background() + + client, err := datastore.NewClient(ctx, "wwgt-codelabs") + if err != nil { + return nil, err + } + + var t []Task + + q := datastore.NewQuery(os.Getenv("MY_CODE")) + keys, err := client.GetAll(ctx, q, &t) + if err != nil { + return nil, err + } + + for i, key := range keys { + t[i].ID = key.ID + } + return t, nil +} + +func setTask(id int64, title string, status Status) *Task { + return &Task{ + ID: id, + Title: title, + Status: status, + CreatedAt: time.Now(), + } +} + +func (t Task) update() error { + ctx := context.Background() + + client, err := datastore.NewClient(ctx, "wwgt-codelabs") + if err != nil { + return err + } + + key := datastore.IDKey(os.Getenv("MY_CODE"), t.ID, nil) + + _, err = client.RunInTransaction(ctx, func(tx *datastore.Transaction) error { + var gt Task + + if err := tx.Get(key, >); err != nil { + return err + } + + if t.Title == "" { + t.Title = gt.Title + } + + if err := t.Status.validate(); err != nil { + t.Status = gt.Status + } + + t.CreatedAt = gt.CreatedAt + + _, err := tx.Put(key, &t) + return err + }) + return err +} + +func (t Task) delete() error { + ctx := context.Background() + + client, err := datastore.NewClient(ctx, "wwgt-codelabs") + if err != nil { + return err + } + + return client.Delete(ctx, datastore.IDKey(os.Getenv("MY_CODE"), t.ID, nil)) +} From ff249337a71fa7e4e3dbbc0b16346717eb70b4c6 Mon Sep 17 00:00:00 2001 From: mi-bear Date: Fri, 17 May 2019 09:28:42 +0900 Subject: [PATCH 2/2] Fixed review --- taskmanager/task.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/taskmanager/task.go b/taskmanager/task.go index 0496828..f45e1f6 100644 --- a/taskmanager/task.go +++ b/taskmanager/task.go @@ -10,7 +10,7 @@ import ( // Task is the structure of the task. type Task struct { - ID int64 `datastore:"-" json:"id"` + ID int64 `datastore:"-" json:"ID"` Title string `datastore:"title" json:"title"` Status Status `datastore:"status" json:"status"` CreatedAt time.Time `datastore:"createdAt" json:"createdAt"`