59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/luxsin/app-api/internal/model"
|
|
)
|
|
|
|
type ShareCodeRepository struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewShareCodeRepository(db *sql.DB) *ShareCodeRepository {
|
|
return &ShareCodeRepository{db: db}
|
|
}
|
|
|
|
func (r *ShareCodeRepository) InsertLog(ctx context.Context, log model.ShareCodeLog) error {
|
|
const query = `INSERT INTO share_code_log (mac_addr, share_code, action, model, ip_addr, eq_data, expire_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
|
|
var expireAt any
|
|
if log.ExpireAt != nil {
|
|
expireAt = *log.ExpireAt
|
|
}
|
|
|
|
_, err := r.db.ExecContext(ctx, query,
|
|
log.MacAddr, log.ShareCode, log.Action, log.Model, log.IpAddr, log.EqData, expireAt,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("insert share_code_log: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *ShareCodeRepository) HasExportLog(ctx context.Context, shareCode string) (bool, error) {
|
|
const query = `SELECT 1 FROM share_code_log WHERE share_code = ? AND action = 'export' LIMIT 1`
|
|
|
|
var one int
|
|
err := r.db.QueryRowContext(ctx, query, shareCode).Scan(&one)
|
|
if err == sql.ErrNoRows {
|
|
return false, nil
|
|
}
|
|
if err != nil {
|
|
return false, fmt.Errorf("query share export log: %w", err)
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func ParseShareExpireAt(expireAt time.Time) *time.Time {
|
|
if expireAt.IsZero() {
|
|
return nil
|
|
}
|
|
t := expireAt
|
|
return &t
|
|
}
|