@@ -12,28 +12,66 @@ type pgsqlTodoRepository struct {
1212 db * sql.DB
1313}
1414
15- // NewPgsqlTodoRepository will create new an todoRepository object representation of TodoRepository interface
15+ // NewPgsqlTodoRepository will create a new todoRepository object representation of TodoRepository interface
1616func NewPgsqlTodoRepository (db * sql.DB ) * pgsqlTodoRepository {
1717 return & pgsqlTodoRepository {
1818 db : db ,
1919 }
2020}
2121
2222func (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+
2330 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+
2543 return
2644}
2745
2846func (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+
2954 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+
3162 return
3263}
3364
3465func (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+
3572 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+
3775 if err != nil {
3876 return todos , err
3977 }
@@ -50,12 +88,23 @@ func (r *pgsqlTodoRepository) Fetch(ctx context.Context) (todos []domain.Todo, e
5088 todos = append (todos , todo )
5189 }
5290
91+ if err = tx .Commit (); err != nil {
92+ return todos , fmt .Errorf ("failed to commit transaction: %v" , err )
93+ }
94+
5395 return todos , nil
5496}
5597
5698func (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+
57106 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 )
59108 if err != nil {
60109 return
61110 }
@@ -69,12 +118,24 @@ func (r *pgsqlTodoRepository) Update(ctx context.Context, todo *domain.Todo) (er
69118 err = fmt .Errorf ("weird behavior, total affected: %d" , affect )
70119 }
71120
121+ if err = tx .Commit (); err != nil {
122+ err = fmt .Errorf ("failed to commit transaction: %v" , err )
123+ return
124+ }
125+
72126 return
73127}
74128
75129func (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+
76137 query := "DELETE FROM todos WHERE id = $1"
77- res , err := r . db .ExecContext (ctx , query , id )
138+ res , err := tx .ExecContext (ctx , query , id )
78139 if err != nil {
79140 return
80141 }
@@ -88,5 +149,10 @@ func (r *pgsqlTodoRepository) Delete(ctx context.Context, id int64) (err error)
88149 err = fmt .Errorf ("weird behavior, total affected: %d" , affect )
89150 }
90151
152+ if err = tx .Commit (); err != nil {
153+ err = fmt .Errorf ("failed to commit transaction: %v" , err )
154+ return
155+ }
156+
91157 return
92158}
0 commit comments