aboutsummaryrefslogtreecommitdiff
path: root/apid/dbmid.go
blob: 240263edd506262a8d8b64a2f47bc92e8cdcab37 (plain) (blame)
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
// Copyright 2021 SNIX LLC sina@snix.ir
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package apid

import (
	"goshkan/rgdb"
)

func (pr *apiMiddRegex) errorOrCancel(err error) {
	select {
	case pr.err <- err:
	case <-pr.ctx.Done():
	}
}

func (pr *apiMiddRegex) sendOnChannel(data interface{}) {
	select {
	case pr.dch <- data:
	case <-pr.ctx.Done():
	}
}

func (pr *apiMiddRegex) loadAllRegexCTX(h *rgdb.MariaSQLDB) {
	data, err := h.LoadAllRegex(pr.ctx)
	if err != nil {
		pr.errorOrCancel(err)
		return
	}
	pr.sendOnChannel(data)

}

func (pr *apiMiddRegex) saveRegexToDB(h *rgdb.MariaSQLDB, ptrn *string) {
	if err := h.RegexIFExist(pr.ctx, ptrn); err != nil {
		pr.errorOrCancel(err)
		return
	}

	if err := h.AddNewRegex(pr.ctx, ptrn); err != nil {
		pr.errorOrCancel(err)
		return
	}

	pr.sendOnChannel(nil)
}

func (pr *apiMiddRegex) deltRegexFromDB(h *rgdb.MariaSQLDB, regid uint) {
	regex, err := h.GetRegexByID(pr.ctx, regid)
	if err != nil {
		pr.errorOrCancel(err)
		return
	}

	if err := h.DeleteRegex(pr.ctx, regid); err != nil {
		pr.errorOrCancel(err)
		return
	}

	pr.sendOnChannel(regex)

}

Snix LLC Git Repository Holder Copyright(C) 2022 All Rights Reserved Email To Snix.IR