package queries import ( "context" "errors" "strings" "git.aiterp.net/rpdata/api/graph2/input" "git.aiterp.net/rpdata/api/internal/auth" "git.aiterp.net/rpdata/api/internal/loader" "git.aiterp.net/rpdata/api/models" "git.aiterp.net/rpdata/api/models/logs" "github.com/99designs/gqlgen/graphql" ) // Queries func (r *resolver) Log(ctx context.Context, id string) (models.Log, error) { return logs.FindID(id) } func (r *resolver) Logs(ctx context.Context, filter *logs.Filter) ([]models.Log, error) { logs, err := logs.List(filter) if err != nil { return nil, err } reqCtx := graphql.GetRequestContext(ctx) maybeCharacters := strings.Contains(reqCtx.RawQuery, "characters") maybeChannels := strings.Contains(reqCtx.RawQuery, "channels") if len(logs) >= 100 && (maybeCharacters || maybeChannels) { loader := loader.FromContext(ctx) if loader == nil { return nil, errors.New("no loader") } for _, log := range logs { if maybeChannels { loader.PrimeChannels("name", log.ChannelName) } if maybeCharacters { loader.PrimeCharacters("id", log.CharacterIDs...) } } } return logs, nil } // Mutations func (r *mutationResolver) AddLog(ctx context.Context, input input.LogAddInput) (models.Log, error) { token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.Permitted("log.add") { return models.Log{}, errors.New("You are not permitted to add logs") } open := input.Open != nil && *input.Open == true title := "" if input.Title != nil { title = *input.Title } event := "" if input.Event != nil { event = *input.Event } description := "" if input.Description != nil { description = *input.Description } return logs.Add(input.Date, input.Channel, title, event, description, open) } func (r *mutationResolver) EditLog(ctx context.Context, input input.LogEditInput) (models.Log, error) { token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.Permitted("log.edit") { return models.Log{}, errors.New("You are not permitted to edit logs") } log, err := logs.FindID(input.ID) if err != nil { return models.Log{}, errors.New("Log not found") } return logs.Edit(log, input.Title, input.Event, input.Description, input.Open) } func (r *mutationResolver) RemoveLog(ctx context.Context, input input.LogRemoveInput) (models.Log, error) { token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.Permitted("log.remove") { return models.Log{}, errors.New("You are not permitted to remove logs") } log, err := logs.FindID(input.ID) if err != nil { return models.Log{}, errors.New("Log not found") } return logs.Remove(log) }