/
workplace.go
36 lines (30 loc) · 1.03 KB
/
workplace.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
package repositories
import (
"encoding/json"
"github.com/alex0206/workplace-accounting/internal/model"
"github.com/alex0206/workplace-accounting/internal/pg"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// WorkplaceRepository describe workplace repository
type WorkplaceRepository struct {
db *pg.DB
}
// NewWorkplaceRepository create a new repository
func NewWorkplaceRepository(db *pg.DB) *WorkplaceRepository {
return &WorkplaceRepository{db: db}
}
// Add adding or updating a workplace
func (r *WorkplaceRepository) Add(ctx context.Context, entity *model.WorkplaceInfo) error {
data, err := json.Marshal(entity)
if err != nil {
return errors.Wrap(err, "error to marshal workplace info")
}
_, err = r.db.Conn.Exec(ctx, "INSERT INTO workplace(info) VALUES($1) ON CONFLICT (info) DO UPDATE SET updated_at=now()", data)
return err
}
// Delete deleting a workplace
func (r *WorkplaceRepository) Delete(ctx context.Context, id int) error {
_, err := r.db.Conn.Exec(ctx, "DELETE FROM workplace WHERE id=$1", id)
return err
}