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