package resolver import ( "context" "time" "git.aiterp.net/rpdata/api/model/change" "git.aiterp.net/rpdata/api/resolver/types" "git.aiterp.net/rpdata/api/internal/session" "git.aiterp.net/rpdata/api/model/log" ) // LogArgs is an arg type LogArgs struct { ID *string } // Log finds log func (r *QueryResolver) Log(ctx context.Context, args *LogArgs) (*types.LogResolver, error) { var l log.Log var err error switch { case args.ID != nil: l, err = log.FindID(*args.ID) default: err = ErrCannotResolve } if err != nil { return nil, err } return &types.LogResolver{L: l}, nil } // LogQueryArgs is args for the logs query type LogQueryArgs struct { Filter *struct { Search *string Characters *[]string Channels *[]string Events *[]string Open *bool Limit *int32 } } // Logs resolves the logs query func (r *QueryResolver) Logs(ctx context.Context, args *LogQueryArgs) ([]*types.LogResolver, error) { var logs []log.Log var err error filter := args.Filter if filter != nil { limit := 100 search := "" if filter.Search != nil { search = *filter.Search limit = 0 } channels := []string(nil) if filter.Channels != nil { channels = *filter.Channels limit = 0 } characters := []string(nil) if filter.Characters != nil { characters = *filter.Characters limit = 0 } events := []string(nil) if filter.Events != nil { events = *filter.Events limit = 0 } if filter.Limit != nil { limit = int(*filter.Limit) } open := filter.Open != nil && *filter.Open == true logs, err = log.ListSearch(search, channels, characters, events, open, limit) if err != nil { return nil, err } } else { logs, err = log.List(100) if err != nil { return nil, err } } resolvers := make([]*types.LogResolver, len(logs)) for i := range logs { resolvers[i] = &types.LogResolver{L: logs[i]} } return resolvers, nil } // LogAddArgs is args for the addLog mutation type LogAddArgs struct { Input *struct { Date string Channel string Title *string Open *bool Event *string Description *string } } // AddLog resolves the addLog mutation func (r *MutationResolver) AddLog(ctx context.Context, args *LogAddArgs) (*types.LogResolver, error) { input := args.Input user := session.FromContext(ctx).User() if user == nil || !user.Permitted("log.add") { return nil, ErrUnauthorized } date, err := time.Parse(time.RFC3339Nano, args.Input.Date) if err != nil { return nil, err } title := "" if input.Title != nil { title = *input.Title } event := "" if input.Event != nil { event = *input.Event } description := "" if input.Description != nil { description = *input.Description } open := input.Open != nil && *input.Open == true log, err := log.New(date, input.Channel, title, event, description, open) if err != nil { return nil, err } go change.Submit("Log", "add", user.ID, log.ID, map[string]interface{}{ "channel": log.Channel, "title": log.Title, "event": log.Event, "description": log.Description, "open": log.Open, }) return &types.LogResolver{L: log}, nil } // LogEditArgs is an input type LogEditArgs struct { Input *struct { ID string Title *string Event *string Description *string Open *bool } } // EditLog resolves the editLog mutation func (r *MutationResolver) EditLog(ctx context.Context, args *LogEditArgs) (*types.LogResolver, error) { input := args.Input user := session.FromContext(ctx).User() if user == nil || !user.Permitted("log.edit") { return nil, ErrUnauthorized } log, err := log.FindID(input.ID) if err != nil { return nil, err } err = log.Edit(input.Title, input.Event, input.Description, input.Open) if err != nil { return nil, err } go change.Submit("Log", "edit", user.ID, log.ID, map[string]interface{}{ "channel": log.Channel, "title": input.Title, "event": input.Event, "description": input.Description, "open": input.Open, }) return &types.LogResolver{L: log}, nil } // RemoveLog resolves the removeLog mutation func (r *MutationResolver) RemoveLog(ctx context.Context, args *struct{ ID string }) (*types.LogResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("log.remove") { return nil, ErrUnauthorized } log, err := log.FindID(args.ID) if err != nil { return nil, err } err = log.Remove() if err != nil { return nil, err } go change.Submit("Log", "remove", user.ID, log.ID, nil) return &types.LogResolver{L: log}, nil }