Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f1fa939
postまで
k-ueki Aug 13, 2019
65eb34f
update
k-ueki Aug 13, 2019
75cc278
delete
k-ueki Aug 13, 2019
82f4380
get comments when /articles/:id
k-ueki Aug 13, 2019
3c4c03a
Merge pull request #1 from k-ueki/k-ueki
k-ueki Aug 13, 2019
56d858a
review
pei0804 Aug 13, 2019
8e3905b
fix
pei0804 Aug 13, 2019
1460d95
みすった
pei0804 Aug 13, 2019
2bc8d4a
fix
pei0804 Aug 13, 2019
b6aef0d
fix
pei0804 Aug 13, 2019
93afc82
Merge pull request #20 from voyagegroup/review
pei0804 Aug 13, 2019
0a00d9e
せい
pei0804 Aug 13, 2019
2f90e31
importきったね
pei0804 Aug 13, 2019
5e7d5a8
u
pei0804 Aug 13, 2019
6ed4726
Clientのsetupがhardなので最低限の動かせるような筋道を示す
yamachu Aug 14, 2019
026d70a
Merge pull request #22 from voyagegroup/front-docs
yamachu Aug 14, 2019
3811c32
Merge branch 'master' of https://github.com/voyagegroup/treasure-app
k-ueki Aug 14, 2019
97dbd50
fix
pei0804 Aug 14, 2019
4542f23
README !
yang-wei Aug 14, 2019
3bd72d8
...
yang-wei Aug 14, 2019
0d89105
Merge commit '3bd72d838de78d5be91d7914fb634cd2542ebcbf' into pei-work
pei0804 Aug 14, 2019
a2fb29f
Merge branch 'master' of https://github.com/voyagegroup/treasure-app
k-ueki Aug 14, 2019
f5610c1
api
k-ueki Aug 16, 2019
52e2777
crud
k-ueki Aug 16, 2019
1e1c74a
IINE,TAG
k-ueki Aug 17, 2019
c556b32
front error
k-ueki Aug 18, 2019
b744ec4
Merge branch 'master' into chukan-tmp-k-ueki
k-ueki Aug 18, 2019
7795177
Merge pull request #2 from k-ueki/chukan-tmp-k-ueki
k-ueki Aug 18, 2019
f586478
Merge branch 'master' of https://github.com/k-ueki/treasure-app
k-ueki Aug 18, 2019
38585b2
front error
k-ueki Aug 18, 2019
e78937d
iines
k-ueki Aug 18, 2019
11b35de
Merge pull request #3 from k-ueki/chukan-k-ueki2
k-ueki Aug 18, 2019
529f473
fix front
k-ueki Aug 19, 2019
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
44 changes: 43 additions & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# Slide

https://go-talks.appspot.com/github.com/voyagegroup/talks/2019/treasure-go-day2/intro.slide#1

# Getting Started

## 1. Database 立ち上げ

`../database` に構成などが定義されているので、読んでみてください。

以下のターゲットを叩くと、databaseの準備をします。

```console
❯ make -f integration.mk database-init
make -C ../database init
```

```console
which goose || GO111MODULE=off go get -u github.com/pressly/goose/cmd/goose
/Users/j-chikamori/go/bin/goose
docker-compose up -d
Expand Down Expand Up @@ -72,9 +80,14 @@ PORT=1991
使っているAPI: [Exchange custom token for an ID and refresh token](https://firebase.google.com/docs/reference/rest/auth/#section-refresh-token)
それぞれのキーについては、上記リンクを見れば大体分かる。

トークンの作成

```console
❯ make -f integration.mk create-token UID=demo
go run ./cmd/customtoken/main.go demo .idToken
```

```console
{
"kind": "identitytoolkit#VerifyCustomTokenResponse",
"idToken": "idtoken",
Expand All @@ -88,17 +101,27 @@ go run ./cmd/customtoken/main.go demo .idToken

## 5. Hello World

*サーバー立ち上げ*

```console
❯ make run
go run cmd/api/main.go
```

```console
2019/08/08 11:32:47 server.go:51: Listening on port 1991
```

サーバーを立ち上げた状態で、別シェルから以下を叩く。

*認証ありエンドポイントへのリクエスト*

```console
❯ make -f integration.mk req-private
curl -v -H "Authorization: Bearer Hoge" localhost:1991/private
```

```console
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 1991 (#0)
Expand All @@ -116,10 +139,16 @@ curl -v -H "Authorization: Bearer Hoge" localhost:1991/private
<
* Connection #0 to host localhost left intact
{"message":"Hello from private endpoint! Your firebase uuid is demo"}
```

*認証なしエンドポイントへのリクエスト*

```console
❯ make -f integration.mk req-public
curl -v localhost:1991/public
```

```console
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 1991 (#0)
Expand Down Expand Up @@ -149,16 +178,29 @@ curl -v localhost:1991/public

コードを書き換える度に `go run` し直すのは面倒くさいので、書き換えるとrebuildしてくれるコマンドを用意しているので活用してください。

realizeをgo getする

```console
❯ make dev-deps
GO111MODULE=off go get -u -v \
github.com/oxequa/realize
```

```
github.com/oxequa/realize (download)
github.com/oxequa/interact (download)
github.com/fatih/color (download)
github.com/fsnotify/fsnotify (download)
```

realizeを使って実行する

```console
❯ make refresh-run
realize start
```

``` console
[11:36:48][BACKEND] : Watching 21 file/s 14 folder/s
[11:36:48][BACKEND] : Install started
[11:36:51][BACKEND] : Install completed in 3.653 s
Expand Down
51 changes: 36 additions & 15 deletions backend/controller/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
)

type Article struct {
dbx *sqlx.DB
db *sqlx.DB
}

func NewArticle(dbx *sqlx.DB) *Article {
return &Article{dbx: dbx}
func NewArticle(db *sqlx.DB) *Article {
return &Article{db: db}
}

func (a *Article) Index(w http.ResponseWriter, r *http.Request) (int, interface{}, error) {
articles, err := repository.AllArticle(a.dbx)
articles, err := repository.AllArticle(a.db)
if err != nil {
return http.StatusInternalServerError, nil, err
}
Expand All @@ -45,17 +45,19 @@ func (a *Article) Show(w http.ResponseWriter, r *http.Request) (int, interface{}
return http.StatusBadRequest, nil, err
}

article, err := repository.FindArticle(a.dbx, aid)
articleService := service.NewArticle(a.db)
articleDetail, err := articleService.FindArticleDetail(aid)
if err != nil && err == sql.ErrNoRows {
return http.StatusNotFound, nil, err
} else if err != nil {
return http.StatusInternalServerError, nil, err
}

return http.StatusCreated, article, nil
return http.StatusCreated, articleDetail, nil
}

func (a *Article) Create(w http.ResponseWriter, r *http.Request) (int, interface{}, error) {

newArticle := &model.Article{}
contextUser, err := httputil.GetUserFromContext(r.Context())
if err != nil {
Expand All @@ -73,17 +75,36 @@ func (a *Article) Create(w http.ResponseWriter, r *http.Request) (int, interface

if err := json.NewDecoder(r.Body).Decode(&newArticle); err != nil {
fmt.Println("Decode")

return http.StatusBadRequest, nil, err
}

articleService := service.NewArticleService(a.dbx)
id, err := articleService.Create(newArticle)
user, err := httputil.GetUserFromContext(r.Context())
if err != nil {
return http.StatusInternalServerError, nil, err
}

createArticle := &model.Article{
Title: reqParam.Title,
Body: reqParam.Body,
UserID: &user.ID,
}

articleService := service.NewArticle(a.db)
id, err := articleService.Create(createArticle, reqParam.TagIDs)
if err != nil {
return http.StatusInternalServerError, nil, err
}
newArticle.ID = id

return http.StatusCreated, newArticle, nil
respParam := &model.ResponseCreateArticle{
ID: id,
Title: createArticle.Title,
Body: createArticle.Body,
UserID: createArticle.UserID,
TagIDs: reqParam.TagIDs,
}

return http.StatusCreated, respParam, nil
}

func (a *Article) Update(w http.ResponseWriter, r *http.Request) (int, interface{}, error) {
Expand All @@ -98,13 +119,13 @@ func (a *Article) Update(w http.ResponseWriter, r *http.Request) (int, interface
return http.StatusBadRequest, nil, err
}

reqArticle := &model.Article{}
if err := json.NewDecoder(r.Body).Decode(&reqArticle); err != nil {
reqParam := &model.Article{}
if err := json.NewDecoder(r.Body).Decode(&reqParam); err != nil {
return http.StatusBadRequest, nil, err
}

articleService := service.NewArticleService(a.dbx)
err = articleService.Update(aid, reqArticle)
articleService := service.NewArticle(a.db)
err = articleService.Update(aid, reqParam)
if err != nil && errors.Cause(err) == sql.ErrNoRows {
return http.StatusNotFound, nil, err
} else if err != nil {
Expand All @@ -126,7 +147,7 @@ func (a *Article) Destroy(w http.ResponseWriter, r *http.Request) (int, interfac
return http.StatusBadRequest, nil, err
}

articleService := service.NewArticleService(a.dbx)
articleService := service.NewArticle(a.db)
err = articleService.Destroy(aid)
if err != nil && errors.Cause(err) == sql.ErrNoRows {
return http.StatusNotFound, nil, err
Expand Down
62 changes: 62 additions & 0 deletions backend/controller/article_comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package controller

import (
"database/sql"
"encoding/json"
"net/http"
"strconv"

"github.com/gorilla/mux"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"github.com/voyagegroup/treasure-app/httputil"
"github.com/voyagegroup/treasure-app/model"
"github.com/voyagegroup/treasure-app/service"
)

type ArticleComment struct {
dbx *sqlx.DB
}

func NewArticleComment(dbx *sqlx.DB) *ArticleComment {
return &ArticleComment{dbx: dbx}
}

func (ac *ArticleComment) Create(w http.ResponseWriter, r *http.Request) (int, interface{}, error) {
vars := mux.Vars(r)
id, ok := vars["article_id"]
if !ok {
return http.StatusBadRequest, nil, &httputil.HTTPError{Message: "invalid path parameter"}
}

aid, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return http.StatusBadRequest, nil, err
}
reqParam := &model.CreateRequestArticleComment{}
if err := json.NewDecoder(r.Body).Decode(&reqParam); err != nil {
return http.StatusBadRequest, nil, err
}

user, err := httputil.GetUserFromContext(r.Context())
if err != nil {
return http.StatusInternalServerError, nil, err
}

createArticleComment := &model.ArticleComment{
UserID: user.ID,
ArticleID: aid,
Body: reqParam.Body,
}

articleCommentService := service.NewArticleCommentService(ac.dbx)
createdId, err := articleCommentService.Create(createArticleComment)
if err != nil && errors.Cause(err) == sql.ErrNoRows {
return http.StatusNotFound, nil, err
} else if err != nil {
return http.StatusInternalServerError, nil, err
}
createArticleComment.ID = createdId

return http.StatusCreated, createArticleComment, nil
}
Loading