chore: refactor loading primary language attribute

We can expect it to either not exist or a single primary language to
exist.
This commit is contained in:
Gusted 2026-05-25 14:55:51 +02:00
parent af412159ce
commit e30d4ef5da
No known key found for this signature in database
GPG key ID: FD821B732837125F
2 changed files with 16 additions and 12 deletions

View file

@ -33,13 +33,18 @@ func init() {
db.RegisterModel(new(LanguageStat))
}
// LoadAttributes loads attributes
func (stat *LanguageStat) LoadAttributes() {
stat.Color = enry.GetColor(stat.Language)
}
// LanguageStatList defines a list of language statistics
type LanguageStatList []*LanguageStat
// LoadAttributes loads attributes
func (stats LanguageStatList) LoadAttributes() {
for i := range stats {
stats[i].Color = enry.GetColor(stats[i].Language)
stats[i].LoadAttributes()
}
}

View file

@ -300,20 +300,19 @@ func (repo *Repository) LoadAttributes(ctx context.Context) error {
return fmt.Errorf("load owner: %w", err)
}
// Load primary language
stats := make(LanguageStatList, 0, 1)
if err := db.GetEngine(ctx).
// Load the primary language.
var stat LanguageStat
has, err := db.GetEngine(ctx).
Where("`repo_id` = ? AND `is_primary` = ? AND `language` != ?", repo.ID, true, "other").
Find(&stats); err != nil {
return fmt.Errorf("find primary languages: %w", err)
Get(&stat)
if err != nil {
return fmt.Errorf("unable to find the primary languages: %w", repo.ID, err)
}
stats.LoadAttributes()
for _, st := range stats {
if st.RepoID == repo.ID {
repo.PrimaryLanguage = st
break
}
if has {
stat.LoadAttributes()
repo.PrimaryLanguage = &stat
}
return nil
}