HEAVY: migrate accounts and logs to service/repo architecture
This commit is contained in:
parent
5c255fb34b
commit
49e14b5bc5
37 changed files with 1019 additions and 687 deletions
9
repository/log/interface.go
Normal file
9
repository/log/interface.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package log
|
||||
|
||||
import "arimelody-web/model"
|
||||
|
||||
type LogRepository interface {
|
||||
Create(logLevel model.LogLevel, logType string, content string) error
|
||||
Get(id string) (*model.Log, error)
|
||||
Search(levelFilters []model.LogLevel, typeFilters []string, content string, limit int, offset int) ([]*model.Log, error)
|
||||
}
|
||||
110
repository/log/postgres.go
Normal file
110
repository/log/postgres.go
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
package log
|
||||
|
||||
import (
|
||||
"arimelody-web/model"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type (
|
||||
LogRepositoryPostgres struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
)
|
||||
|
||||
var _ LogRepository = new(LogRepositoryPostgres)
|
||||
|
||||
func NewLogRepositoryPostgres(db *sqlx.DB) *LogRepositoryPostgres {
|
||||
return &LogRepositoryPostgres{ db: db }
|
||||
}
|
||||
|
||||
func (repo *LogRepositoryPostgres) Create(logLevel model.LogLevel, logType string, content string) error {
|
||||
_, err := repo.db.Exec(
|
||||
"INSERT INTO auditlog (level, type, content) VALUES ($1,$2,$3)",
|
||||
logLevel,
|
||||
logType,
|
||||
content,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (repo *LogRepositoryPostgres) Get(id string) (*model.Log, error) {
|
||||
log := model.Log{}
|
||||
err := repo.db.Get(&log, "SELECT * FROM auditlog WHERE id=$1", id)
|
||||
return &log, err
|
||||
}
|
||||
|
||||
func (repo *LogRepositoryPostgres) Search(
|
||||
levelFilters []model.LogLevel,
|
||||
typeFilters []string,
|
||||
content string,
|
||||
limit int,
|
||||
offset int,
|
||||
) ([]*model.Log, error) {
|
||||
logs := []*model.Log{}
|
||||
|
||||
params := []any{ limit, offset }
|
||||
conditions := ""
|
||||
|
||||
if len(content) > 0 {
|
||||
content = "%" + content + "%"
|
||||
conditions += " WHERE content LIKE $3"
|
||||
params = append(params, content)
|
||||
}
|
||||
|
||||
if len(levelFilters) > 0 {
|
||||
if len(conditions) > 0 {
|
||||
conditions += " AND level IN ("
|
||||
} else {
|
||||
conditions += " WHERE level IN ("
|
||||
}
|
||||
for i := range levelFilters {
|
||||
conditions += fmt.Sprintf("$%d", len(params) + 1)
|
||||
if i < len(levelFilters) - 1 {
|
||||
conditions += ","
|
||||
}
|
||||
params = append(params, levelFilters[i])
|
||||
}
|
||||
conditions += ")"
|
||||
}
|
||||
|
||||
if len(typeFilters) > 0 {
|
||||
if len(conditions) > 0 {
|
||||
conditions += " AND type IN ("
|
||||
} else {
|
||||
conditions += " WHERE type IN ("
|
||||
}
|
||||
for i := range typeFilters {
|
||||
conditions += fmt.Sprintf("$%d", len(params) + 1)
|
||||
if i < len(typeFilters) - 1 {
|
||||
conditions += ","
|
||||
}
|
||||
params = append(params, typeFilters[i])
|
||||
}
|
||||
conditions += ")"
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(
|
||||
"SELECT * FROM auditlog%s ORDER BY created_at DESC LIMIT $1 OFFSET $2",
|
||||
conditions,
|
||||
)
|
||||
|
||||
/*
|
||||
fmt.Printf("%s (", query)
|
||||
for i, param := range params {
|
||||
fmt.Print(param)
|
||||
if i < len(params) - 1 {
|
||||
fmt.Print(", ")
|
||||
}
|
||||
}
|
||||
fmt.Print(")\n")
|
||||
*/
|
||||
|
||||
err := repo.db.Select(&logs, query, params...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return logs, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue