/
function.go
78 lines (64 loc) · 1.78 KB
/
function.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
76
77
78
package pendaftaran
import (
"context"
pmbulbi "github.com/PMBULBI/types/schemas"
"github.com/golang-module/dongle"
"gorm.io/gorm"
"math/rand"
"time"
)
func Encrypt(plaintext string, key string) (res string) {
res = dongle.Encrypt.FromString(plaintext).ByRc4(key).ToHexString()
return
}
func Decrypt(enc string, key string) (res string) {
res = dongle.Decrypt.FromHexString(enc).ByRc4(key).ToString()
return
}
const letterBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func GenerateRandomPassword(length int) string {
rand.NewSource(time.Now().UnixNano())
passwordBytes := make([]byte, length)
for i := 0; i < length; i++ {
passwordBytes[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(passwordBytes)
}
func TableMigrator(maria string, model interface{}) (res string, err error) {
err = CreateMariaGormConnection(maria).AutoMigrate(&model)
if err != nil {
return "Gagal Migrate", err
}
return
}
func TableMigratorPostgres(pstgrs string, model ...interface{}) (res string, err error) {
err = CreatePostgresConnection(pstgrs).AutoMigrate(model...)
if err != nil {
return "Gagal Migrate", err
}
return
}
func InsertDataPendaftar(conn *gorm.DB, ctx context.Context, val pmbulbi.Pendaftaran) (err error) {
err = conn.
WithContext(ctx).
Create(&val).
Error
return
}
func GetKotaLimits(conn *gorm.DB, ctx context.Context, page, perPage int) (val []pmbulbi.WilayahKota, err error) {
offset := (page - 1) * perPage
err = conn.
WithContext(ctx).
Limit(perPage).
Offset(offset).
Find(&val).
Error
return
}
func CheckUserExists(conn *gorm.DB, ctx context.Context, email string, nohp string) (user pmbulbi.Pendaftaran, err error) {
err = conn.WithContext(ctx).
Where("email_mhs = ? OR hp_mhs = ?", email, nohp).
First(&user).
Error
return user, err
}