@@ -12,28 +12,66 @@ type pgsqlTodoRepository struct {
12
12
db * sql.DB
13
13
}
14
14
15
- // NewPgsqlTodoRepository will create new an todoRepository object representation of TodoRepository interface
15
+ // NewPgsqlTodoRepository will create a new todoRepository object representation of TodoRepository interface
16
16
func NewPgsqlTodoRepository (db * sql.DB ) * pgsqlTodoRepository {
17
17
return & pgsqlTodoRepository {
18
18
db : db ,
19
19
}
20
20
}
21
21
22
22
func (r * pgsqlTodoRepository ) Create (ctx context.Context , todo * domain.Todo ) (err error ) {
23
+ tx , err := r .db .BeginTx (ctx , nil )
24
+ if err != nil {
25
+ err = fmt .Errorf ("failed to begin transaction: %v" , err )
26
+ return
27
+ }
28
+ defer tx .Rollback ()
29
+
23
30
query := "INSERT INTO todos (name, created_at, updated_at) VALUES ($1, $2, $3)"
24
- _ , err = r .db .ExecContext (ctx , query , todo .Name , todo .CreatedAt , todo .UpdatedAt )
31
+ _ , err = tx .ExecContext (ctx , query , todo .Name , todo .CreatedAt , todo .UpdatedAt )
32
+
33
+ if err != nil {
34
+ err = fmt .Errorf ("failed to insert todo: %v" , err )
35
+ return
36
+ }
37
+
38
+ if err = tx .Commit (); err != nil {
39
+ err = fmt .Errorf ("failed to commit transaction: %v" , err )
40
+ return
41
+ }
42
+
25
43
return
26
44
}
27
45
28
46
func (r * pgsqlTodoRepository ) GetByID (ctx context.Context , id int64 ) (todo domain.Todo , err error ) {
47
+ tx , err := r .db .BeginTx (ctx , nil )
48
+ if err != nil {
49
+ err = fmt .Errorf ("failed to begin transaction: %v" , err )
50
+ return
51
+ }
52
+ defer tx .Rollback ()
53
+
29
54
query := "SELECT id, name, created_at, updated_at FROM todos WHERE id = $1"
30
- err = r .db .QueryRowContext (ctx , query , id ).Scan (& todo .ID , & todo .Name , & todo .CreatedAt , & todo .UpdatedAt )
55
+ err = tx .QueryRowContext (ctx , query , id ).Scan (& todo .ID , & todo .Name , & todo .CreatedAt , & todo .UpdatedAt )
56
+
57
+ if err = tx .Commit (); err != nil {
58
+ err = fmt .Errorf ("failed to commit transaction: %v" , err )
59
+ return
60
+ }
61
+
31
62
return
32
63
}
33
64
34
65
func (r * pgsqlTodoRepository ) Fetch (ctx context.Context ) (todos []domain.Todo , err error ) {
66
+ tx , err := r .db .BeginTx (ctx , nil )
67
+ if err != nil {
68
+ err = fmt .Errorf ("failed to begin transaction: %v" , err )
69
+ return
70
+ }
71
+
35
72
query := "SELECT id, name, created_at, updated_at FROM todos"
36
- rows , err := r .db .QueryContext (ctx , query )
73
+ rows , err := tx .QueryContext (ctx , query )
74
+
37
75
if err != nil {
38
76
return todos , err
39
77
}
@@ -50,12 +88,23 @@ func (r *pgsqlTodoRepository) Fetch(ctx context.Context) (todos []domain.Todo, e
50
88
todos = append (todos , todo )
51
89
}
52
90
91
+ if err = tx .Commit (); err != nil {
92
+ return todos , fmt .Errorf ("failed to commit transaction: %v" , err )
93
+ }
94
+
53
95
return todos , nil
54
96
}
55
97
56
98
func (r * pgsqlTodoRepository ) Update (ctx context.Context , todo * domain.Todo ) (err error ) {
99
+ tx , err := r .db .BeginTx (ctx , nil )
100
+ if err != nil {
101
+ err = fmt .Errorf ("failed to begin transaction: %v" , err )
102
+ return
103
+ }
104
+ defer tx .Rollback ()
105
+
57
106
query := "UPDATE todos SET name = $1, updated_at = $2 WHERE id = $3"
58
- res , err := r . db .ExecContext (ctx , query , todo .Name , todo .UpdatedAt , todo .ID )
107
+ res , err := tx .ExecContext (ctx , query , todo .Name , todo .UpdatedAt , todo .ID )
59
108
if err != nil {
60
109
return
61
110
}
@@ -69,12 +118,24 @@ func (r *pgsqlTodoRepository) Update(ctx context.Context, todo *domain.Todo) (er
69
118
err = fmt .Errorf ("weird behavior, total affected: %d" , affect )
70
119
}
71
120
121
+ if err = tx .Commit (); err != nil {
122
+ err = fmt .Errorf ("failed to commit transaction: %v" , err )
123
+ return
124
+ }
125
+
72
126
return
73
127
}
74
128
75
129
func (r * pgsqlTodoRepository ) Delete (ctx context.Context , id int64 ) (err error ) {
130
+ tx , err := r .db .BeginTx (ctx , nil )
131
+ if err != nil {
132
+ err = fmt .Errorf ("failed to begin transaction: %v" , err )
133
+ return
134
+ }
135
+ defer tx .Rollback ()
136
+
76
137
query := "DELETE FROM todos WHERE id = $1"
77
- res , err := r . db .ExecContext (ctx , query , id )
138
+ res , err := tx .ExecContext (ctx , query , id )
78
139
if err != nil {
79
140
return
80
141
}
@@ -88,5 +149,10 @@ func (r *pgsqlTodoRepository) Delete(ctx context.Context, id int64) (err error)
88
149
err = fmt .Errorf ("weird behavior, total affected: %d" , affect )
89
150
}
90
151
152
+ if err = tx .Commit (); err != nil {
153
+ err = fmt .Errorf ("failed to commit transaction: %v" , err )
154
+ return
155
+ }
156
+
91
157
return
92
158
}
0 commit comments