|
| 1 | +package cassandra |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/gocql/gocql" |
| 10 | +) |
| 11 | + |
| 12 | +type PasscodeService struct { |
| 13 | + db *gocql.ClusterConfig |
| 14 | + tableName string |
| 15 | + idName string |
| 16 | + passcodeName string |
| 17 | + expiredAtName string |
| 18 | +} |
| 19 | + |
| 20 | +func NewPasscodeRepository(db *gocql.ClusterConfig, tableName string, options ...string) *PasscodeService { |
| 21 | + var idName, passcodeName, expiredAtName string |
| 22 | + if len(options) >= 1 && len(options[0]) > 0 { |
| 23 | + expiredAtName = options[0] |
| 24 | + } else { |
| 25 | + expiredAtName = "expiredat" |
| 26 | + } |
| 27 | + if len(options) >= 2 && len(options[1]) > 0 { |
| 28 | + idName = options[1] |
| 29 | + } else { |
| 30 | + idName = "id" |
| 31 | + } |
| 32 | + if len(options) >= 3 && len(options[2]) > 0 { |
| 33 | + passcodeName = options[2] |
| 34 | + } else { |
| 35 | + passcodeName = "passcode" |
| 36 | + } |
| 37 | + return &PasscodeService{ |
| 38 | + db: db, |
| 39 | + tableName: strings.ToLower(tableName), |
| 40 | + idName: strings.ToLower(idName), |
| 41 | + passcodeName: strings.ToLower(passcodeName), |
| 42 | + expiredAtName: strings.ToLower(expiredAtName), |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (p *PasscodeService) Save(ctx context.Context, id string, passcode string, expiredAt time.Time) (int64, error) { |
| 47 | + session, er0 := p.db.CreateSession() |
| 48 | + columns := []string{p.idName, p.passcodeName, p.expiredAtName} |
| 49 | + if er0 != nil { |
| 50 | + return 0, er0 |
| 51 | + } |
| 52 | + queryString := fmt.Sprintf("INSERT INTO %s (%s) VALUES (? ,? ,?)", |
| 53 | + p.tableName, |
| 54 | + strings.Join(columns, ","), |
| 55 | + ) |
| 56 | + |
| 57 | + err := session.Query(queryString, id, passcode, expiredAt).Exec() |
| 58 | + if err != nil { |
| 59 | + return 0, err |
| 60 | + } |
| 61 | + defer session.Close() |
| 62 | + return 1, nil |
| 63 | +} |
| 64 | + |
| 65 | +func (p *PasscodeService) Load(ctx context.Context, id string) (string, time.Time, error) { |
| 66 | + session, er0 := p.db.CreateSession() |
| 67 | + // var returnId strng |
| 68 | + var code string |
| 69 | + var expiredAt time.Time |
| 70 | + if er0 != nil { |
| 71 | + return "", time.Now().Add(-24 * time.Hour), er0 |
| 72 | + } |
| 73 | + strSql := fmt.Sprintf(`SELECT %s, %s FROM `, p.passcodeName, p.expiredAtName) + p.tableName + ` WHERE ` + p.idName + ` =? ALLOW FILTERING` |
| 74 | + er1 := session.Query(strSql, id).Scan(&code, &expiredAt) |
| 75 | + if er1 != nil { |
| 76 | + return "", time.Now().Add(-24 * time.Hour), er1 |
| 77 | + } |
| 78 | + return code, expiredAt, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (p *PasscodeService) Delete(ctx context.Context, id string) (int64, error) { |
| 82 | + session, er0 := p.db.CreateSession() |
| 83 | + if er0 != nil { |
| 84 | + return 0, er0 |
| 85 | + } |
| 86 | + query := "delete from " + p.tableName + " where " + p.idName + " = ?" |
| 87 | + er1 := session.Query(query, id).Exec() |
| 88 | + if er1 != nil { |
| 89 | + return 0, er1 |
| 90 | + } |
| 91 | + return 1, nil |
| 92 | +} |
0 commit comments