package mutations import ( "context" "strings" "git.aiterp.net/rpdata/api/graphql/resolver/types" "git.aiterp.net/rpdata/api/internal/session" "git.aiterp.net/rpdata/api/model/change" "git.aiterp.net/rpdata/api/model/character" "git.aiterp.net/rpdata/api/model/log" ) // AddCharacterInput is args for the addCharacter mutation type AddCharacterInput struct { Nick string Name string ShortName *string Author *string Description *string } // AddCharacter resolves the addCharacter mutation func (r *MutationResolver) AddCharacter(ctx context.Context, args struct{ Input *AddCharacterInput }) (*types.CharacterResolver, error) { input := args.Input user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "character.add") { return nil, ErrUnauthorized } nick := input.Nick name := input.Name shortName := "" if input.ShortName != nil { shortName = *input.ShortName } else { shortName = strings.SplitN(input.Name, " ", 2)[0] } author := user.ID if input.Author != nil { author = *input.Author if author != user.ID && !user.Permitted("character.add") { return nil, ErrPermissionDenied } } description := "" if input.Description != nil { description = *input.Description } character, err := character.New(nick, name, shortName, author, description) if err != nil { return nil, err } go change.Submit("Character", "add", user.ID, character.ID, map[string]interface{}{ "name": character.Name, "nick": character.Nicks[0], "author": character.Author, }) log.ScheduleCharacterUpdate() return &types.CharacterResolver{C: character}, nil }