-
Notifications
You must be signed in to change notification settings - Fork 0
/
contacts-repository.go
75 lines (60 loc) · 1.59 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
69
70
71
72
73
74
75
package goautowp
import (
"context"
"database/sql"
"errors"
"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
err := s.autowpDB.QueryRowContext(
ctx,
"SELECT 1 FROM contact WHERE user_id = ? and contact_user_id = ?",
id, contactID,
).Scan(&v)
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func (s *ContactsRepository) create(ctx context.Context, id int64, contactID int64) error {
_, err := s.autowpDB.ExecContext(ctx, `
INSERT IGNORE INTO contact (user_id, contact_user_id, timestamp)
VALUES (?, ?, NOW())
`, id, contactID)
if err != nil {
return err
}
return nil
}
func (s *ContactsRepository) delete(ctx context.Context, id int64, contactID int64) error {
_, err := s.autowpDB.ExecContext(ctx, "DELETE FROM contact WHERE user_id = ? AND contact_user_id = ?", id, contactID)
if err != nil {
return err
}
return nil
}
func (s *ContactsRepository) deleteUserEverywhere(ctx context.Context, id int64) error {
_, err := s.autowpDB.ExecContext(ctx, "DELETE FROM contact WHERE user_id = ?", id)
if err != nil {
return err
}
_, err = s.autowpDB.ExecContext(ctx, "DELETE FROM contact WHERE contact_user_id = ?", id)
if err != nil {
return err
}
return nil
}