forked from bozaro/tech-db-hello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_pgsql.go
55 lines (46 loc) · 1.54 KB
/
hello_pgsql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package service
import (
"fmt"
"github.com/mailcourses/technopark-dbms-init/golang/restapi/operations"
"github.com/go-openapi/runtime/middleware"
_ "github.com/lib/pq"
)
type HelloPgSQL struct {
HelloGeneric
}
func NewHelloPgSQL(dataSourceName string) HelloHandler {
return HelloPgSQL{HelloGeneric: NewHelloGeneric("postgres", dataSourceName)}
}
func (self HelloPgSQL) AddMulti(params operations.AddMultiParams) middleware.Responder {
posts := []Post{}
if len(params.Body) > 0 {
tx := self.db.MustBegin()
defer tx.Rollback()
query := "INSERT INTO tasks (description, completed) VALUES"
args := []interface{}{}
for idx, item := range params.Body {
if idx > 0 {
query += ","
}
query += fmt.Sprintf(" ($%d, $%d)", len(args)+1, len(args)+2)
args = append(args, item.Description, item.Completed)
}
query += " RETURNING id, description, completed"
check(tx.Select(&posts, query, args...))
check(tx.Commit())
}
return operations.NewAddMultiCreated().WithPayload(PostsPayload(posts))
}
func (self HelloPgSQL) UpdateOne(params operations.UpdateOneParams) middleware.Responder {
tx := self.db.MustBegin()
defer tx.Rollback()
posts := []Post{}
check(tx.Select(&posts, "UPDATE tasks SET description = $1, completed = $2 WHERE id = $3 RETURNING id, description, completed", params.Body.Description, params.Body.Completed, params.ID))
check(tx.Commit())
if len(posts) == 0 {
return operations.NewUpdateOneNotFound()
}
post := *params.Body
post.ID = params.ID
return operations.NewUpdateOneOK().WithPayload(posts[0].Payload())
}