-
Notifications
You must be signed in to change notification settings - Fork 0
/
contacts-repository.go
68 lines (55 loc) · 1.78 KB
/
contacts-repository.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
56
57
58
59
60
61
62
63
64
65
66
67
68
package goautowp
import (
"context"
"github.com/autowp/goautowp/schema"
"github.com/doug-martin/goqu/v9"
)
// ContactsRepository Main Object.
type ContactsRepository struct {
autowpDB *goqu.Database
}
// NewContactsRepository constructor.
func NewContactsRepository(db *goqu.Database) *ContactsRepository {
return &ContactsRepository{
autowpDB: db,
}
}
func (s *ContactsRepository) isExists(ctx context.Context, id int64, contactID int64) (bool, error) {
v := 0
return s.autowpDB.Select(goqu.V(1)).
From(schema.ContactTable).
Where(schema.ContactTableUserIDCol.Eq(id), schema.ContactTableContactUserIDCol.Eq(contactID)).
ScanValContext(ctx, &v)
}
func (s *ContactsRepository) create(ctx context.Context, id int64, contactID int64) error {
_, err := s.autowpDB.Insert(schema.ContactTable).Rows(goqu.Record{
schema.ContactTableUserIDColName: id,
schema.ContactTableContactUserIDColName: contactID,
schema.ContactTableTimestampColName: goqu.Func("NOW"),
}).OnConflict(goqu.DoNothing()).Executor().ExecContext(ctx)
if err != nil {
return err
}
return nil
}
func (s *ContactsRepository) delete(ctx context.Context, id int64, contactID int64) error {
_, err := s.autowpDB.Delete(schema.ContactTable).
Where(schema.ContactTableUserIDCol.Eq(id), schema.ContactTableContactUserIDCol.Eq(contactID)).
Executor().ExecContext(ctx)
if err != nil {
return err
}
return nil
}
func (s *ContactsRepository) deleteUserEverywhere(ctx context.Context, id int64) error {
_, err := s.autowpDB.Delete(schema.ContactTable).
Where(schema.ContactTableUserIDCol.Eq(id)).
Executor().ExecContext(ctx)
if err != nil {
return err
}
_, err = s.autowpDB.Delete(schema.ContactTable).
Where(schema.ContactTableContactUserIDCol.Eq(id)).
Executor().ExecContext(ctx)
return err
}