You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
3.8 KiB
175 lines
3.8 KiB
package mysql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"git.aiterp.net/stufflog3/stufflog3-api/internal/database/mysql/mysqlcore"
|
|
"git.aiterp.net/stufflog3/stufflog3-api/internal/models"
|
|
"git.aiterp.net/stufflog3/stufflog3-api/internal/slerrors"
|
|
"golang.org/x/sync/errgroup"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
type scopeRepository struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func (r *scopeRepository) Find(ctx context.Context, id int) (*models.Scope, error) {
|
|
q := mysqlcore.New(r.db)
|
|
scope, err := q.GetScope(ctx, id)
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return nil, slerrors.NotFound("Scope")
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
|
|
res := &models.Scope{
|
|
ScopeEntry: models.ScopeEntry{
|
|
ID: scope.ID,
|
|
Name: scope.Name,
|
|
Abbreviation: scope.Abbreviation,
|
|
},
|
|
}
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
eg.Go(func() error {
|
|
members, err := q.ListScopeMembers(ctx, id)
|
|
if err != nil && err != sql.ErrNoRows {
|
|
return err
|
|
}
|
|
|
|
for _, member := range members {
|
|
res.Members = append(res.Members, models.ScopeMember{
|
|
ID: member.UserID,
|
|
Name: member.Name,
|
|
Owner: member.Owner,
|
|
})
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
eg.Go(func() error {
|
|
projects, err := q.ListProjectEntries(ctx, id)
|
|
if err != nil && err != sql.ErrNoRows {
|
|
return err
|
|
}
|
|
|
|
for _, project := range projects {
|
|
res.Projects = append(res.Projects, models.ProjectEntry{
|
|
ID: project.ID,
|
|
Name: project.Name,
|
|
Status: models.Status(project.Status),
|
|
})
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
eg.Go(func() error {
|
|
stats, err := q.ListStats(ctx, id)
|
|
if err != nil && err != sql.ErrNoRows {
|
|
return err
|
|
}
|
|
|
|
for _, stat := range stats {
|
|
var amounts map[string]int
|
|
if stat.AllowedAmounts.Valid {
|
|
amounts = make(map[string]int)
|
|
err := json.Unmarshal(stat.AllowedAmounts.RawMessage, &amounts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
res.Stats = append(res.Stats, models.Stat{
|
|
StatEntry: models.StatEntry{
|
|
ID: stat.ID,
|
|
Name: stat.Name,
|
|
Weight: stat.Weight,
|
|
},
|
|
Description: stat.Description,
|
|
AllowedAmounts: amounts,
|
|
})
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
t := time.Now()
|
|
err = eg.Wait()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
log.Println(time.Since(t))
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func (r *scopeRepository) List(ctx context.Context) ([]models.ScopeEntry, error) {
|
|
q := mysqlcore.New(r.db)
|
|
scopes, err := q.ListScopes(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res := make([]models.ScopeEntry, 0, len(scopes))
|
|
for _, scope := range scopes {
|
|
res = append(res, models.ScopeEntry{
|
|
ID: scope.ID,
|
|
Name: scope.Name,
|
|
Abbreviation: scope.Abbreviation,
|
|
})
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func (r *scopeRepository) ListByUser(ctx context.Context, userID string) ([]models.ScopeEntry, error) {
|
|
q := mysqlcore.New(r.db)
|
|
scopes, err := q.ListScopesByUser(ctx, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res := make([]models.ScopeEntry, 0, len(scopes))
|
|
for _, scope := range scopes {
|
|
res = append(res, models.ScopeEntry{
|
|
ID: scope.ID,
|
|
Name: scope.Name,
|
|
Abbreviation: scope.Abbreviation,
|
|
})
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func (r *scopeRepository) Create(ctx context.Context, scope models.ScopeEntry, owner models.ScopeMember) (*models.Scope, error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (r *scopeRepository) Update(ctx context.Context, scope models.ScopeEntry) error {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (r *scopeRepository) Delete(ctx context.Context, scope models.Scope) error {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (r *scopeRepository) UpdateMember(ctx context.Context, scope models.Scope, member models.ScopeMember) (*models.Scope, error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (r *scopeRepository) DeleteMember(ctx context.Context, scope models.Scope, userID string) (*models.Scope, error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|