|
|
@ -2,15 +2,21 @@ package items |
|
|
|
|
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"git.aiterp.net/stufflog3/stufflog3/entities" |
|
|
|
"git.aiterp.net/stufflog3/stufflog3/models" |
|
|
|
"git.aiterp.net/stufflog3/stufflog3/usecases/auth" |
|
|
|
"git.aiterp.net/stufflog3/stufflog3/usecases/scopes" |
|
|
|
"git.aiterp.net/stufflog3/stufflog3/usecases/stats" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
type Service struct { |
|
|
|
Scopes *scopes.Service |
|
|
|
Stats *stats.Service |
|
|
|
Repository Repository |
|
|
|
Auth *auth.Service |
|
|
|
Scopes *scopes.Service |
|
|
|
Stats *stats.Service |
|
|
|
Repository Repository |
|
|
|
RequirementFetcher RequirementFetcher |
|
|
|
} |
|
|
|
|
|
|
|
func (s *Service) Find(ctx context.Context, id int) (*Result, error) { |
|
|
@ -24,12 +30,12 @@ func (s *Service) Find(ctx context.Context, id int) (*Result, error) { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
progresses, err := s.Repository.ListProgress(ctx, *item) |
|
|
|
itemStats, err := s.Repository.ListStat(ctx, *item) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
result := generateResult(*item, sc.Scope, progresses) |
|
|
|
result := generateResult(*item, sc.Scope, itemStats) |
|
|
|
return &result, nil |
|
|
|
} |
|
|
|
|
|
|
@ -67,7 +73,7 @@ func (s *Service) ListScoped(ctx context.Context, filter models.ItemFilter) ([]R |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
progresses, err := s.Repository.ListProgress(ctx, items...) |
|
|
|
progresses, err := s.Repository.ListStat(ctx, items...) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
@ -79,3 +85,124 @@ func (s *Service) ListScoped(ctx context.Context, filter models.ItemFilter) ([]R |
|
|
|
|
|
|
|
return res, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (s *Service) Create(ctx context.Context, item entities.Item, stats []entities.ItemStat) (*Result, error) { |
|
|
|
scope := s.Scopes.Context(ctx).Scope |
|
|
|
user := s.Auth.GetUser(ctx) |
|
|
|
|
|
|
|
item.Name = strings.Trim(item.Name, " \t\r\n") |
|
|
|
if item.Name == "" { |
|
|
|
return nil, models.BadInputError{ |
|
|
|
Object: "ProjectInput", |
|
|
|
Field: "name", |
|
|
|
Problem: "Empty name provided", |
|
|
|
} |
|
|
|
} |
|
|
|
if item.ProjectRequirementID != nil { |
|
|
|
reqs, _, err := s.RequirementFetcher.FetchRequirements(ctx, scope.ID, *item.ProjectRequirementID) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
if len(reqs) == 0 { |
|
|
|
return nil, models.NotFoundError("Requirement") |
|
|
|
} |
|
|
|
|
|
|
|
item.ProjectID = &reqs[0].ProjectID |
|
|
|
} |
|
|
|
item.ScopeID = scope.ID |
|
|
|
item.OwnerID = user.ID |
|
|
|
|
|
|
|
if item.CreatedTime.IsZero() { |
|
|
|
item.CreatedTime = time.Now() |
|
|
|
} |
|
|
|
|
|
|
|
newItem, err := s.Repository.Insert(ctx, item) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
goodStats := make([]entities.ItemStat, 0, len(stats)) |
|
|
|
for _, stat := range stats { |
|
|
|
stat.ItemID = newItem.ID |
|
|
|
if scope.Stat(stat.StatID) == nil { |
|
|
|
return nil, models.NotFoundError("Stat") |
|
|
|
} |
|
|
|
|
|
|
|
err := s.Repository.UpdateStat(ctx, stat) |
|
|
|
if err != nil { |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
goodStats = append(goodStats, stat) |
|
|
|
} |
|
|
|
|
|
|
|
results := generateResult(*newItem, scope, goodStats) |
|
|
|
return &results, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (s *Service) Update(ctx context.Context, id int, update models.ItemUpdate) (*Result, error) { |
|
|
|
item, err := s.Find(ctx, id) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
err = s.Repository.Update(ctx, item.Item, update) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
item.Item.ApplyUpdate(update) |
|
|
|
|
|
|
|
return item, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (s *Service) Delete(ctx context.Context, id int) (*Result, error) { |
|
|
|
item, err := s.Find(ctx, id) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
err = s.Repository.Delete(ctx, item.Item) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
return item, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (s *Service) UpdateStat(ctx context.Context, itemID int, stat entities.ItemStat) (*Result, error) { |
|
|
|
scope := s.Scopes.Context(ctx).Scope |
|
|
|
item, err := s.Find(ctx, itemID) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
stat.ItemID = itemID |
|
|
|
|
|
|
|
err = s.Repository.UpdateStat(ctx, stat) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
item.AddStat(scope, stat) |
|
|
|
|
|
|
|
return item, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (s *Service) DeleteStat(ctx context.Context, itemID int, statID int) (*Result, error) { |
|
|
|
item, err := s.Find(ctx, itemID) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
err = s.Repository.UpdateStat(ctx, entities.ItemStat{ |
|
|
|
ItemID: itemID, |
|
|
|
StatID: statID, |
|
|
|
Acquired: 0, |
|
|
|
Required: -1, |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
item.RemoveStat(statID) |
|
|
|
|
|
|
|
return item, nil |
|
|
|
} |