refactor: clean code

This commit is contained in:
Fu Diwei
2024-12-28 16:26:01 +08:00
parent 86133ba52b
commit 416f5e0986
12 changed files with 55 additions and 49 deletions

View File

@@ -31,8 +31,8 @@ func (a *AccessRepository) GetById(ctx context.Context, id string) (*domain.Acce
UpdatedAt: record.GetTime("updated"),
},
Name: record.GetString("name"),
Config: record.GetString("config"),
ConfigType: record.GetString("configType"),
Config: record.GetString("config"),
Usage: record.GetString("usage"),
}
return rs, nil

View File

@@ -13,7 +13,7 @@ func NewCertificateRepository() *CertificateRepository {
return &CertificateRepository{}
}
func (c *CertificateRepository) GetExpireSoon(ctx context.Context) ([]domain.Certificate, error) {
func (c *CertificateRepository) ListExpireSoon(ctx context.Context) ([]domain.Certificate, error) {
rs := []domain.Certificate{}
if err := app.GetApp().Dao().DB().
NewQuery("select * from certificate where expireAt > datetime('now') and expireAt < datetime('now', '+20 days')").

View File

@@ -8,13 +8,13 @@ import (
"github.com/usual2970/certimate/internal/domain"
)
type SettingRepository struct{}
type SettingsRepository struct{}
func NewSettingRepository() *SettingRepository {
return &SettingRepository{}
func NewSettingsRepository() *SettingsRepository {
return &SettingsRepository{}
}
func (s *SettingRepository) GetByName(ctx context.Context, name string) (*domain.Settings, error) {
func (s *SettingsRepository) GetByName(ctx context.Context, name string) (*domain.Settings, error) {
resp, err := app.GetApp().Dao().FindFirstRecordByFilter("settings", "name={:name}", dbx.Params{"name": name})
if err != nil {
return nil, err

View File

@@ -13,21 +13,21 @@ func NewStatisticsRepository() *StatisticsRepository {
return &StatisticsRepository{}
}
type totalResp struct {
Total int `json:"total" db:"total"`
}
func (r *StatisticsRepository) Get(ctx context.Context) (*domain.Statistics, error) {
rs := &domain.Statistics{}
// 所有证书
certTotal := totalResp{}
certTotal := struct {
Total int `db:"total"`
}{}
if err := app.GetApp().Dao().DB().NewQuery("select count(*) as total from certificate").One(&certTotal); err != nil {
return nil, err
}
rs.CertificateTotal = certTotal.Total
// 即将过期证书
certExpireSoonTotal := totalResp{}
certExpireSoonTotal := struct {
Total int `db:"total"`
}{}
if err := app.GetApp().Dao().DB().
NewQuery("select count(*) as total from certificate where expireAt > datetime('now') and expireAt < datetime('now', '+20 days')").
One(&certExpireSoonTotal); err != nil {
@@ -36,7 +36,9 @@ func (r *StatisticsRepository) Get(ctx context.Context) (*domain.Statistics, err
rs.CertificateExpireSoon = certExpireSoonTotal.Total
// 已过期证书
certExpiredTotal := totalResp{}
certExpiredTotal := struct {
Total int `db:"total"`
}{}
if err := app.GetApp().Dao().DB().
NewQuery("select count(*) as total from certificate where expireAt < datetime('now')").
One(&certExpiredTotal); err != nil {
@@ -45,14 +47,18 @@ func (r *StatisticsRepository) Get(ctx context.Context) (*domain.Statistics, err
rs.CertificateExpired = certExpiredTotal.Total
// 所有工作流
workflowTotal := totalResp{}
workflowTotal := struct {
Total int `db:"total"`
}{}
if err := app.GetApp().Dao().DB().NewQuery("select count(*) as total from workflow").One(&workflowTotal); err != nil {
return nil, err
}
rs.WorkflowTotal = workflowTotal.Total
// 已启用工作流
workflowEnabledTotal := totalResp{}
workflowEnabledTotal := struct {
Total int `db:"total"`
}{}
if err := app.GetApp().Dao().DB().NewQuery("select count(*) as total from workflow where enabled is TRUE").One(&workflowEnabledTotal); err != nil {
return nil, err
}