Skip to content

Commit 647ed2b

Browse files
author
Onur Menal
committed
PostComment service
1 parent 51d3033 commit 647ed2b

File tree

7 files changed

+45
-7
lines changed

7 files changed

+45
-7
lines changed

build/Dockerfile Dockerfile

File renamed without changes.

build/Taskfile.yml Taskfile.yml

File renamed without changes.

cmd/server/main.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ func Run() error {
2727
}
2828

2929
cmtService := comment.NewService(db)
30-
fmt.Println(cmtService.GetComment(
31-
context.Background(),
32-
"904a8e95-b6a0-4f80-9142-5cf0a5895d25",
33-
))
30+
31+
fmt.Println(cmtService.PostComment(context.Background(), comment.Comment{
32+
Slug: "testslug",
33+
Body: "testbody",
34+
Author: "onur menal",
35+
}))
3436

3537
fmt.Println("Successfully connected and pinged database")
3638

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ require (
77
github.com/google/go-github/v35 v35.2.0 // indirect
88
github.com/jmoiron/sqlx v1.3.5 // indirect
99
github.com/lib/pq v1.10.6 // indirect
10+
github.com/satori/go.uuid v1.2.0 // indirect
1011
)

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfF
946946
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
947947
github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4=
948948
github.com/safchain/ethtool v0.0.0-20210803160452-9aa261dae9b1/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4=
949+
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
949950
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
950951
github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZKJ48R1S7H23Ji7oFO5Bw=
951952
github.com/sclevine/spec v1.2.0/go.mod h1:W4J29eT/Kzv7/b9IWLB055Z+qvVC9vt0Arko24q7p+U=

internal/comment/comment.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Comment struct {
2020

2121
type Store interface {
2222
GetComment(context.Context, string) (Comment, error)
23+
PostComment(context.Context, Comment) (Comment, error)
2324
}
2425

2526
type Service struct {
@@ -46,10 +47,15 @@ func (s *Service) UpdateComment(ctx context.Context, cmt Comment) error {
4647
return ErrorNotImplemented
4748
}
4849

49-
func (*Service) DeleteCommment(ctx context.Context, id string) error {
50+
func (s *Service) DeleteCommment(ctx context.Context, id string) error {
5051
return ErrorNotImplemented
5152
}
5253

53-
func (*Service) CreateComment(ctx context.Context, cmt Comment) (Comment, error) {
54-
return Comment{}, ErrorNotImplemented
54+
func (s *Service) PostComment(ctx context.Context, cmt Comment) (Comment, error) {
55+
fmt.Println("posting a comment")
56+
insertedComment, err := s.Store.PostComment(ctx, cmt)
57+
if err != nil {
58+
return Comment{}, ErrorMessage
59+
}
60+
return insertedComment, nil
5561
}

internal/db/comment.go

+28
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77

88
"github.com/onurmenal/go-rest-api/internal/comment"
9+
uuid "github.com/satori/go.uuid"
910
)
1011

1112
type CommentRow struct {
@@ -44,3 +45,30 @@ func (d *Database) GetComment(
4445

4546
return convertCommentRowToComment(cmtRow), nil
4647
}
48+
49+
func (d *Database) PostComment(ctx context.Context, cmt comment.Comment) (comment.Comment, error) {
50+
cmt.ID = uuid.NewV4().String()
51+
postRow := CommentRow{
52+
ID: cmt.ID,
53+
Slug: sql.NullString{String: cmt.Slug, Valid: true},
54+
Author: sql.NullString{String: cmt.Author, Valid: true},
55+
Body: sql.NullString{String: cmt.Body, Valid: true},
56+
}
57+
58+
rows, err := d.Client.NamedQueryContext(
59+
ctx,
60+
`INSERT INTO comments
61+
(id,slug,author,body)
62+
VALUES
63+
(:id, :slug, :author, :body)`,
64+
postRow,
65+
)
66+
if err != nil {
67+
return comment.Comment{}, fmt.Errorf("failed to insert comment: %w", err)
68+
}
69+
if err := rows.Close(); err != nil {
70+
return comment.Comment{}, fmt.Errorf("failed to close rows: %w", err)
71+
}
72+
73+
return cmt, nil
74+
}

0 commit comments

Comments
 (0)