Files
stufflog3/usecases/scopes/result.go
T
gisle a530bed697
continuous-integration/drone/push Build is passing
fix front page scope stat ordering.
2022-07-13 18:15:24 +02:00

92 lines
1.9 KiB
Go

package scopes
import (
"git.aiterp.net/stufflog3/stufflog3/entities"
"git.aiterp.net/stufflog3/stufflog3/models"
)
type Result struct {
entities.Scope
Members map[string]ResultMember `json:"members"`
Stats []ResultStat `json:"stats"`
}
func (r *Result) MemberName(id string) string {
if member, ok := r.Members[id]; ok {
return member.Name
}
return ""
}
func (r *Result) Stat(id int) *ResultStat {
for _, stat := range r.Stats {
if stat.ID == id {
return &stat
}
}
return nil
}
func (r *Result) HasStat(id int) bool {
for _, stat := range r.Stats {
if stat.ID == id {
return true
}
}
return false
}
type ResultMember struct {
ID string `json:"id"`
Name string `json:"name"`
Owner bool `json:"owner"`
}
type ResultStat struct {
ID int `json:"id"`
Name string `json:"name"`
Weight float64 `json:"weight"`
Primary bool `json:"primary"`
Description string `json:"description"`
AllowedAmounts []models.StatAllowedAmount `json:"allowedAmounts,omitempty"`
}
func generateResult(scope entities.Scope, members []entities.ScopeMember, stats []entities.Stat) *Result {
res := Result{Scope: scope, Members: make(map[string]ResultMember, len(members))}
for _, member := range members {
if member.ScopeID != scope.ID {
continue
}
res.Members[member.UserID] = ResultMember{
ID: member.UserID,
Name: member.Name,
Owner: member.Owner,
}
}
if stats != nil {
res.Stats = make([]ResultStat, 0, len(stats))
for _, stat := range stats {
if stat.ScopeID != scope.ID {
continue
}
res.Stats = append(res.Stats, ResultStat{
ID: stat.ID,
Name: stat.Name,
Weight: stat.Weight,
Primary: stat.Primary,
Description: stat.Description,
AllowedAmounts: stat.AllowedAmounts,
})
}
}
return &res
}