reprogl

Форк
0
/
email_subscription.go 
125 строк · 2.2 Кб
1
package repositories
2

3
import (
4
	"database/sql"
5
	"errors"
6
	"time"
7

8
	"xelbot.com/reprogl/models"
9
)
10

11
type EmailSubscriptionRepository struct {
12
	DB *sql.DB
13
}
14

15
func (es *EmailSubscriptionRepository) Find(id int) (*models.EmailSubscription, error) {
16
	query := `
17
		SELECT
18
			s.id,
19
			s.email,
20
			s.subs_type,
21
			s.block_sending
22
		FROM subscription_settings AS s
23
		WHERE s.id = ?`
24

25
	model := models.EmailSubscription{}
26
	err := es.DB.QueryRow(query, id).Scan(
27
		&model.ID,
28
		&model.Email,
29
		&model.Type,
30
		&model.BlockSending)
31

32
	if err != nil {
33
		if errors.Is(err, sql.ErrNoRows) {
34
			return nil, models.RecordNotFound
35
		} else {
36
			return nil, err
37
		}
38
	}
39

40
	return &model, nil
41
}
42

43
func (es *EmailSubscriptionRepository) FindOrCreate(email string, subscrType int) (*models.EmailSubscription, error) {
44
	query := `
45
		SELECT
46
			s.id,
47
			s.email,
48
			s.subs_type,
49
			s.block_sending
50
		FROM subscription_settings AS s
51
		WHERE
52
			s.email = ?
53
			AND s.subs_type = ?`
54

55
	model := models.EmailSubscription{}
56
	err := es.DB.QueryRow(query, email, subscrType).Scan(
57
		&model.ID,
58
		&model.Email,
59
		&model.Type,
60
		&model.BlockSending)
61

62
	if err != nil {
63
		if errors.Is(err, sql.ErrNoRows) {
64
			return es.Create(email, subscrType)
65
		} else {
66
			return nil, err
67
		}
68
	}
69

70
	return &model, nil
71
}
72

73
func (es *EmailSubscriptionRepository) Create(email string, subscrType int) (*models.EmailSubscription, error) {
74
	query := `INSERT INTO subscription_settings (email, subs_type, block_sending)
75
				VALUES (?, ?, ?)`
76

77
	stmtResult, err := es.DB.Exec(
78
		query,
79
		email,
80
		subscrType,
81
		0,
82
	)
83
	if err != nil {
84
		return nil, err
85
	}
86

87
	id, err := stmtResult.LastInsertId()
88
	if err != nil {
89
		return nil, err
90
	}
91

92
	return &models.EmailSubscription{
93
		ID:    int(id),
94
		Email: email,
95
		Type:  subscrType,
96
	}, nil
97
}
98

99
func (es *EmailSubscriptionRepository) Unsubscribe(id int) error {
100
	return es.changeBlockSending(id, 1)
101
}
102

103
func (es *EmailSubscriptionRepository) Subscribe(id int) error {
104
	return es.changeBlockSending(id, 0)
105
}
106

107
func (es *EmailSubscriptionRepository) changeBlockSending(id, value int) error {
108
	query := `
109
		UPDATE
110
			subscription_settings
111
		SET
112
			block_sending = ?,
113
			last_update = ?
114
		WHERE
115
			id = ?`
116

117
	_, err := es.DB.Exec(
118
		query,
119
		value,
120
		time.Now().Format("2006-01-02 15:04:05.000"),
121
		id,
122
	)
123

124
	return err
125
}
126

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.