Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add codelab contents taskmanager #19

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions taskmanager/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
83 changes: 79 additions & 4 deletions taskmanager/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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, &gt); 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))
}