mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-28 11:14:54 -04:00
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:
parent
af412159ce
commit
e30d4ef5da
2 changed files with 16 additions and 12 deletions
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue