refac: removed ugly multi-line method chaining
This commit is contained in:
1 parent
e8dff6bb5f
commit
ce9c876db6
6 files changed
+72
-67
No files matched your search
@@ -64,22 +64,24 @@ func (a *APIKeyRepository) list(query *gorm.DB) ([]domainAPIKey.APIKey, error) {
|
||||
}
|
||||
|
||||
func (a *APIKeyRepository) Revoke(id uint32, at time.Time) error {
|
||||
result := a.db.Model(&entity.APIKey{}).
|
||||
Where("id = ? AND revoked_at IS NULL", id).
|
||||
Update("revoked_at", at)
|
||||
query := a.db.Model(&entity.APIKey{})
|
||||
query = query.Where("id = ? AND revoked_at IS NULL", id)
|
||||
result := query.Update("revoked_at", at)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
func (a *APIKeyRepository) RevokeAllByUser(userId uint32, at time.Time) error {
|
||||
return a.db.Model(&entity.APIKey{}).
|
||||
Where("user_id = ? AND revoked_at IS NULL", userId).
|
||||
Update("revoked_at", at).Error
|
||||
query := a.db.Model(&entity.APIKey{})
|
||||
query = query.Where("user_id = ? AND revoked_at IS NULL", userId)
|
||||
result := query.Update("revoked_at", at)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
func (a *APIKeyRepository) TouchLastUsed(id uint32, at time.Time) error {
|
||||
return a.db.Model(&entity.APIKey{}).
|
||||
Where("id = ?", id).
|
||||
Update("last_used_at", at).Error
|
||||
query := a.db.Model(&entity.APIKey{})
|
||||
query = query.Where("id = ?", id)
|
||||
result := query.Update("last_used_at", at)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
var _ domainAPIKey.Repository = &APIKeyRepository{}
|
||||
@@ -131,21 +131,21 @@ type countsRow struct {
|
||||
|
||||
func (a *AuditEventRepository) countsBetween(from time.Time, to time.Time) (countsRow, error) {
|
||||
var row countsRow
|
||||
err := a.db.Model(&entity.AuditEvent{}).
|
||||
Select("COUNT(*) AS total, "+
|
||||
"COALESCE(SUM(CASE WHEN outcome = ? THEN 1 ELSE 0 END), 0) AS failed, "+
|
||||
"COALESCE(SUM(CASE WHEN security = ? THEN 1 ELSE 0 END), 0) AS security", string(audit.OutcomeFailed), true).
|
||||
Where("created_at >= ? AND created_at < ?", from.UTC(), to.UTC()).
|
||||
Scan(&row).Error
|
||||
query := a.db.Model(&entity.AuditEvent{})
|
||||
query = query.Select("COUNT(*) AS total, "+
|
||||
"COALESCE(SUM(CASE WHEN outcome = ? THEN 1 ELSE 0 END), 0) AS failed, "+
|
||||
"COALESCE(SUM(CASE WHEN security = ? THEN 1 ELSE 0 END), 0) AS security", string(audit.OutcomeFailed), true)
|
||||
query = query.Where("created_at >= ? AND created_at < ?", from.UTC(), to.UTC())
|
||||
err := query.Scan(&row).Error
|
||||
return row, err
|
||||
}
|
||||
|
||||
func (a *AuditEventRepository) distinctActors(actorType audit.ActorType, from time.Time, to time.Time) (int, error) {
|
||||
var count int64
|
||||
err := a.db.Model(&entity.AuditEvent{}).
|
||||
Where("actor_type = ? AND created_at >= ? AND created_at < ?", string(actorType), from.UTC(), to.UTC()).
|
||||
Distinct("actor_id").
|
||||
Count(&count).Error
|
||||
query := a.db.Model(&entity.AuditEvent{})
|
||||
query = query.Where("actor_type = ? AND created_at >= ? AND created_at < ?", string(actorType), from.UTC(), to.UTC())
|
||||
query = query.Distinct("actor_id")
|
||||
err := query.Count(&count).Error
|
||||
return int(count), err
|
||||
}
|
||||
|
||||
@@ -215,11 +215,11 @@ func (a *AuditEventRepository) DeleteOldest(limit int, before time.Time) (int, e
|
||||
return 0, nil
|
||||
}
|
||||
var ids []uint64
|
||||
err := a.db.Model(&entity.AuditEvent{}).
|
||||
Where("created_at < ?", before.UTC()).
|
||||
Order("created_at ASC, id ASC").
|
||||
Limit(limit).
|
||||
Pluck("id", &ids).Error
|
||||
query := a.db.Model(&entity.AuditEvent{})
|
||||
query = query.Where("created_at < ?", before.UTC())
|
||||
query = query.Order("created_at ASC, id ASC")
|
||||
query = query.Limit(limit)
|
||||
err := query.Pluck("id", &ids).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@@ -123,11 +123,12 @@ func (i ImageRepository) ListByStoredNames(storedNames []string) ([]domain.Image
|
||||
}
|
||||
|
||||
func (i ImageRepository) duplicatedHashes() *gorm.DB {
|
||||
return i.db.Model(&entity.Image{}).
|
||||
Select("sha256").
|
||||
Where("sha256 <> ''").
|
||||
Group("sha256").
|
||||
Having("COUNT(*) > 1")
|
||||
query := i.db.Model(&entity.Image{})
|
||||
query = query.Select("sha256")
|
||||
query = query.Where("sha256 <> ''")
|
||||
query = query.Group("sha256")
|
||||
query = query.Having("COUNT(*) > 1")
|
||||
return query
|
||||
}
|
||||
|
||||
func imagesToDomain(models []entity.Image) []domain.Image {
|
||||
|
||||
@@ -36,10 +36,12 @@ func (v *VariantFileRepository) Find(variantName string, sourceName string) (dom
|
||||
func (v *VariantFileRepository) Save(file domain.VariantFile) (domain.VariantFile, error) {
|
||||
model := mappers.VariantFileToModel(file)
|
||||
model.ID = 0
|
||||
err := v.db.Clauses(clause.OnConflict{
|
||||
upsert := clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "variant_name"}, {Name: "source_name"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"cached_name", "content_type", "size", "fingerprint"}),
|
||||
}).Create(&model).Error
|
||||
}
|
||||
query := v.db.Clauses(upsert)
|
||||
err := query.Create(&model).Error
|
||||
if err != nil {
|
||||
return domain.VariantFile{}, err
|
||||
}
|
||||
@@ -77,10 +79,10 @@ func (v *VariantFileRepository) TotalsBySource() (map[string]domain.VariantFileT
|
||||
Files int
|
||||
Bytes int64
|
||||
}
|
||||
err := v.db.Model(&entity.ImageVariantFile{}).
|
||||
Select("source_name, COUNT(*) AS files, COALESCE(SUM(size), 0) AS bytes").
|
||||
Group("source_name").
|
||||
Scan(&rows).Error
|
||||
query := v.db.Model(&entity.ImageVariantFile{})
|
||||
query = query.Select("source_name, COUNT(*) AS files, COALESCE(SUM(size), 0) AS bytes")
|
||||
query = query.Group("source_name")
|
||||
err := query.Scan(&rows).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -104,10 +106,10 @@ func (v *VariantFileRepository) UnusedTotals(filter domain.UnusedFilter) (domain
|
||||
Files int
|
||||
Bytes int64
|
||||
}
|
||||
err := v.db.Model(&entity.ImageVariantFile{}).
|
||||
Select("COUNT(*) AS files, COALESCE(SUM(size), 0) AS bytes").
|
||||
Where(unusedCondition, filter.VariantNames, filter.Cutoff.UTC()).
|
||||
Scan(&row).Error
|
||||
query := v.db.Model(&entity.ImageVariantFile{})
|
||||
query = query.Select("COUNT(*) AS files, COALESCE(SUM(size), 0) AS bytes")
|
||||
query = query.Where(unusedCondition, filter.VariantNames, filter.Cutoff.UTC())
|
||||
err := query.Scan(&row).Error
|
||||
if err != nil {
|
||||
return domain.VariantFileTotals{}, err
|
||||
}
|
||||
@@ -120,12 +122,12 @@ func (v *VariantFileRepository) TopSources(limit int) ([]domain.SourceTotals, er
|
||||
Files int
|
||||
Bytes int64
|
||||
}
|
||||
err := v.db.Model(&entity.ImageVariantFile{}).
|
||||
Select("source_name, COUNT(*) AS files, COALESCE(SUM(size), 0) AS bytes").
|
||||
Group("source_name").
|
||||
Order(topSourcesOrder).
|
||||
Limit(limit).
|
||||
Scan(&rows).Error
|
||||
query := v.db.Model(&entity.ImageVariantFile{})
|
||||
query = query.Select("source_name, COUNT(*) AS files, COALESCE(SUM(size), 0) AS bytes")
|
||||
query = query.Group("source_name")
|
||||
query = query.Order(topSourcesOrder)
|
||||
query = query.Limit(limit)
|
||||
err := query.Scan(&rows).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -167,10 +169,10 @@ func (v *VariantFileRepository) Totals() (map[string]domain.VariantFileTotals, e
|
||||
Files int
|
||||
Bytes int64
|
||||
}
|
||||
err := v.db.Model(&entity.ImageVariantFile{}).
|
||||
Select("variant_name, COUNT(*) AS files, COALESCE(SUM(size), 0) AS bytes").
|
||||
Group("variant_name").
|
||||
Scan(&rows).Error
|
||||
query := v.db.Model(&entity.ImageVariantFile{})
|
||||
query = query.Select("variant_name, COUNT(*) AS files, COALESCE(SUM(size), 0) AS bytes")
|
||||
query = query.Group("variant_name")
|
||||
err := query.Scan(&rows).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -40,10 +40,10 @@ func (v *VariantUsageRepository) Record(batch domain.VariantUsageBatch) error {
|
||||
|
||||
func recordHit(tx *gorm.DB, hit domain.VariantHit) error {
|
||||
at := hit.At.UTC()
|
||||
return tx.Model(&entity.ImageVariantFile{}).
|
||||
Where("variant_name = ? AND source_name = ?", hit.VariantName, hit.SourceName).
|
||||
Where("last_requested_at IS NULL OR last_requested_at < ?", at).
|
||||
Update("last_requested_at", at).Error
|
||||
query := tx.Model(&entity.ImageVariantFile{})
|
||||
query = query.Where("variant_name = ? AND source_name = ?", hit.VariantName, hit.SourceName)
|
||||
query = query.Where("last_requested_at IS NULL OR last_requested_at < ?", at)
|
||||
return query.Update("last_requested_at", at).Error
|
||||
}
|
||||
|
||||
func recordRejections(tx *gorm.DB, rejection domain.RejectionCount) error {
|
||||
@@ -56,9 +56,9 @@ func recordRejections(tx *gorm.DB, rejection domain.RejectionCount) error {
|
||||
|
||||
func (v *VariantUsageRepository) RejectedSince(day time.Time) (int, error) {
|
||||
var total int
|
||||
err := v.db.Model(&entity.ImageVariantRejectionDay{}).
|
||||
Select("COALESCE(SUM(count), 0)").
|
||||
Where("day >= ?", day.UTC().Format(domain.RejectionDayLayout)).
|
||||
Scan(&total).Error
|
||||
query := v.db.Model(&entity.ImageVariantRejectionDay{})
|
||||
query = query.Select("COALESCE(SUM(count), 0)")
|
||||
query = query.Where("day >= ?", day.UTC().Format(domain.RejectionDayLayout))
|
||||
err := query.Scan(&total).Error
|
||||
return total, err
|
||||
}
|
||||
@@ -46,13 +46,13 @@ func (i *InviteRepository) Delete(id uint32) error {
|
||||
}
|
||||
|
||||
func (i *InviteRepository) Renew(id uint32, hashedCode string, expiresAt time.Time, createdAt time.Time) (domainInvite.Invite, error) {
|
||||
result := i.db.Model(&entity.Invite{}).
|
||||
Where("id = ? AND accepted_at IS NULL", id).
|
||||
Updates(map[string]any{
|
||||
"hashed_code": hashedCode,
|
||||
"expires_at": expiresAt,
|
||||
"created_at": createdAt,
|
||||
})
|
||||
query := i.db.Model(&entity.Invite{})
|
||||
query = query.Where("id = ? AND accepted_at IS NULL", id)
|
||||
result := query.Updates(map[string]any{
|
||||
"hashed_code": hashedCode,
|
||||
"expires_at": expiresAt,
|
||||
"created_at": createdAt,
|
||||
})
|
||||
if err := deleteOrNotFound(result, domainInvite.ErrInviteNotFound); err != nil {
|
||||
return domainInvite.Invite{}, err
|
||||
}
|
||||
@@ -67,9 +67,9 @@ func (i *InviteRepository) Renew(id uint32, hashedCode string, expiresAt time.Ti
|
||||
func (i *InviteRepository) AcceptWithUser(inviteID uint32, acceptedAt time.Time, newUser domainUser.User) (domainUser.User, error) {
|
||||
createdModel := mappers.UserToModel(newUser)
|
||||
err := i.db.Transaction(func(tx *gorm.DB) error {
|
||||
marked := tx.Model(&entity.Invite{}).
|
||||
Where("id = ? AND accepted_at IS NULL", inviteID).
|
||||
Update("accepted_at", acceptedAt)
|
||||
query := tx.Model(&entity.Invite{})
|
||||
query = query.Where("id = ? AND accepted_at IS NULL", inviteID)
|
||||
marked := query.Update("accepted_at", acceptedAt)
|
||||
if err := deleteOrNotFound(marked, domainInvite.ErrInviteNotFound); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user