|
|
@ -11,13 +11,19 @@ type SQLiteUserAgentRepository struct {
|
|
|
|
db *sql.DB
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewSQLiteUserAgentRepository(db *sql.DB) *SQLiteUserAgentRepository {
|
|
|
|
func NewSQLiteUserAgentRepository(filename string) *SQLiteUserAgentRepository {
|
|
|
|
return &SQLiteUserAgentRepository{
|
|
|
|
db, err := sql.Open("sqlite3", filename)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := SQLiteUserAgentRepository{
|
|
|
|
db: db,
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.Migrate()
|
|
|
|
|
|
|
|
return &repo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *SQLiteUserAgentRepository) Migrate() error {
|
|
|
|
func (r SQLiteUserAgentRepository) Migrate() error {
|
|
|
|
query := `
|
|
|
|
query := `
|
|
|
|
CREATE TABLE IF NOT EXISTS useragent(
|
|
|
|
CREATE TABLE IF NOT EXISTS useragent(
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
@ -28,12 +34,11 @@ func (r *SQLiteUserAgentRepository) Migrate() error {
|
|
|
|
quota_usage_kb bigint
|
|
|
|
quota_usage_kb bigint
|
|
|
|
);
|
|
|
|
);
|
|
|
|
`
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
_, err := r.db.Exec(query)
|
|
|
|
_, err := r.db.Exec(query)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *SQLiteUserAgentRepository) Create(useragent domain.UserAgent) (*domain.UserAgent, error) {
|
|
|
|
func (r SQLiteUserAgentRepository) Create(useragent domain.UserAgent) (*domain.UserAgent, error) {
|
|
|
|
var record, err = fromEntity(useragent)
|
|
|
|
var record, err = fromEntity(useragent)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
|
|
|
@ -62,7 +67,7 @@ func (r *SQLiteUserAgentRepository) Create(useragent domain.UserAgent) (*domain.
|
|
|
|
return &useragent, nil
|
|
|
|
return &useragent, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *SQLiteUserAgentRepository) All() ([]domain.UserAgent, error) {
|
|
|
|
func (r SQLiteUserAgentRepository) All() ([]domain.UserAgent, error) {
|
|
|
|
rows, err := r.db.Query("SELECT * FROM useragent")
|
|
|
|
rows, err := r.db.Query("SELECT * FROM useragent")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
@ -85,8 +90,8 @@ func (r *SQLiteUserAgentRepository) All() ([]domain.UserAgent, error) {
|
|
|
|
return all, nil
|
|
|
|
return all, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *SQLiteUserAgentRepository) GetByName(name string) (*domain.UserAgent, error) {
|
|
|
|
func (r SQLiteUserAgentRepository) GetByName(name string) (*domain.UserAgent, error) {
|
|
|
|
row := r.db.QueryRow("SELECT id, name, lastseen, allowance_time, allowance_kb FROM useragent WHERE name = ?", name)
|
|
|
|
row := r.db.QueryRow("SELECT id, name, lastseen, allowance_time, quota_kb, quota_usage_kb FROM useragent WHERE name = ?", name)
|
|
|
|
|
|
|
|
|
|
|
|
var record UserAgentDBRecord
|
|
|
|
var record UserAgentDBRecord
|
|
|
|
if err := row.Scan(&record.ID, &record.Name, &record.LastSeen, &record.AllowanceSeconds, &record.QuotaKB, &record.QuotaUsedKB); err != nil {
|
|
|
|
if err := row.Scan(&record.ID, &record.Name, &record.LastSeen, &record.AllowanceSeconds, &record.QuotaKB, &record.QuotaUsedKB); err != nil {
|
|
|
@ -104,7 +109,7 @@ func (r *SQLiteUserAgentRepository) GetByName(name string) (*domain.UserAgent, e
|
|
|
|
return useragent, nil
|
|
|
|
return useragent, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *SQLiteUserAgentRepository) Update(id int64, updated domain.UserAgent) (*domain.UserAgent, error) {
|
|
|
|
func (r SQLiteUserAgentRepository) Update(id int64, updated domain.UserAgent) (*domain.UserAgent, error) {
|
|
|
|
if id == 0 {
|
|
|
|
if id == 0 {
|
|
|
|
return nil, errors.New("invalid updated ID")
|
|
|
|
return nil, errors.New("invalid updated ID")
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -130,7 +135,7 @@ func (r *SQLiteUserAgentRepository) Update(id int64, updated domain.UserAgent) (
|
|
|
|
return &updated, nil
|
|
|
|
return &updated, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *SQLiteUserAgentRepository) Delete(id int64) error {
|
|
|
|
func (r SQLiteUserAgentRepository) Delete(id int64) error {
|
|
|
|
res, err := r.db.Exec("DELETE FROM useragent WHERE id = ?", id)
|
|
|
|
res, err := r.db.Exec("DELETE FROM useragent WHERE id = ?", id)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|