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

#140 #53

Merged
merged 1 commit into from Jun 18, 2022
Merged

#140 #53

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 41 additions & 6 deletions main.go
Expand Up @@ -107,6 +107,41 @@ func (m *Memo) Validate() []*ErrorMessage {
// [333] => {ID:1111, Title:mytitle .... }
var memos map[int]*Memo = map[int]*Memo{}

var memosVersion2 *Memos = NewMemos()

type Memos struct {
Memos []*Memo
}

func NewMemos() *Memos {
return &Memos{
Memos: []*Memo{},
}
}

func (ms *Memos) AddMemo(m *Memo) []*ErrorMessage {
errMsg := m.Validate()

for _, memo := range ms.Memos {
if m.Title == memo.Title {
//タイトルが同じなのでエラーにする
errMsg = append(
errMsg,
NewErrorMessage("TItleIsDuplicated", "タイトルが重複しています。"),
)
break
}
}

if len(errMsg) > 0 {
return errMsg
}

ms.Memos = append(ms.Memos, m)

return nil
}

//Go言語では構造体や関数などの名前の先頭を大文字にするか小文字にするかで、
//プログラムの挙動が変わります。
//メモ帳アプリでは基本的に大文字にした方がよいです。
Expand Down Expand Up @@ -173,24 +208,24 @@ func addMemo(w http.ResponseWriter, r *http.Request) {
return
}

errMsgs := m.Validate()
//メモを保存する。
errMsgs := memosVersion2.AddMemo(m)

if len(errMsgs) > 0 {
// クライアント側から送信されるリクエストに問題があるので、
// HTTP Status 400 を返す。
// Go言語では定数としてHTTP Statusが用意されているので、
// それを利用するのがいいと思います。
// https://developer.mozilla.org/ja/docs/Web/HTTP/Status/400

WarningLog(fmt.Sprintf("invalid memo, error = %v", errMsgs))
RespondError(w, http.StatusBadRequest, errMsgs)
return
}

//メモをmemosに保存する。
//Memo.IDをキーにしているので、IDが同じメモは上書きされる。
memos[m.ID] = m

//HTTP Response は空にするので、nilを指定する。
//len()は配列やマップなどの長さを出力することができる関数です。
fmt.Fprintln(w, len(memos))
fmt.Fprintln(w, len(memosVersion2.Memos))
}

func RespondInternalServerError(w http.ResponseWriter, errorLogMessage string) {
Expand Down