|
|
@ -16,13 +16,23 @@ import ( |
|
|
|
|
|
|
|
// Queries
|
|
|
|
|
|
|
|
func (r *resolver) Character(ctx context.Context, id *string, nick *string) (models.Character, error) { |
|
|
|
func (r *resolver) Character(ctx context.Context, id *string, nick *string) (*models.Character, error) { |
|
|
|
if id != nil { |
|
|
|
return characters.FindID(*id) |
|
|
|
character, err := characters.FindID(*id) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
return &character, nil |
|
|
|
} else if nick != nil { |
|
|
|
return characters.FindNick(*nick) |
|
|
|
character, err := characters.FindNick(*nick) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
return &character, nil |
|
|
|
} else { |
|
|
|
return models.Character{}, errors.New("You must specify either an ID or a nick") |
|
|
|
return nil, errors.New("You must specify either an ID or a nick") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -32,13 +42,13 @@ func (r *resolver) Characters(ctx context.Context, filter *characters.Filter) ([ |
|
|
|
|
|
|
|
// Mutations
|
|
|
|
|
|
|
|
func (r *mutationResolver) AddCharacter(ctx context.Context, input input.CharacterAddInput) (models.Character, error) { |
|
|
|
func (r *mutationResolver) AddCharacter(ctx context.Context, input input.CharacterAddInput) (*models.Character, error) { |
|
|
|
token := auth.TokenFromContext(ctx) |
|
|
|
if !token.Permitted("member", "character.add") { |
|
|
|
return models.Character{}, errors.New("You are not permitted to add characters") |
|
|
|
return nil, errors.New("You are not permitted to add characters") |
|
|
|
} |
|
|
|
if len(input.Name) < 2 || len(input.Nick) < 2 { |
|
|
|
return models.Character{}, errors.New("You need to provide a name and a nick (min length: 2)") |
|
|
|
return nil, errors.New("You need to provide a name and a nick (min length: 2)") |
|
|
|
} |
|
|
|
|
|
|
|
shortName := "" |
|
|
@ -56,7 +66,7 @@ func (r *mutationResolver) AddCharacter(ctx context.Context, input input.Charact |
|
|
|
author := token.UserID |
|
|
|
if input.Author != nil && *input.Author != author { |
|
|
|
if !token.Permitted("character.add") { |
|
|
|
return models.Character{}, errors.New("You are only permitted to add your own characters") |
|
|
|
return nil, errors.New("You are only permitted to add your own characters") |
|
|
|
} |
|
|
|
|
|
|
|
author = *input.Author |
|
|
@ -66,109 +76,109 @@ func (r *mutationResolver) AddCharacter(ctx context.Context, input input.Charact |
|
|
|
|
|
|
|
character, err := characters.Add(input.Nick, input.Name, shortName, author, description) |
|
|
|
if err != nil { |
|
|
|
return models.Character{}, errors.New("Adding character failed: " + err.Error()) |
|
|
|
return nil, errors.New("Adding character failed: " + err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
go changes.Submit("Character", "add", token.UserID, true, changekeys.Listed(character), character) |
|
|
|
|
|
|
|
return character, nil |
|
|
|
return &character, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (r *mutationResolver) AddCharacterNick(ctx context.Context, input input.CharacterNickInput) (models.Character, error) { |
|
|
|
func (r *mutationResolver) AddCharacterNick(ctx context.Context, input input.CharacterNickInput) (*models.Character, error) { |
|
|
|
character, err := characters.FindID(input.ID) |
|
|
|
if err != nil { |
|
|
|
return models.Character{}, errors.New("Character not found") |
|
|
|
return nil, errors.New("Character not found") |
|
|
|
} |
|
|
|
|
|
|
|
if len(input.Nick) < 2 { |
|
|
|
return models.Character{}, errors.New("You need to provide a valid nick (min length: 2)") |
|
|
|
return nil, errors.New("You need to provide a valid nick (min length: 2)") |
|
|
|
} |
|
|
|
|
|
|
|
token := auth.TokenFromContext(ctx) |
|
|
|
if !token.PermittedUser(character.Author, "member", "character.edit") { |
|
|
|
return models.Character{}, errors.New("You are not permitted to edit this character") |
|
|
|
return nil, errors.New("You are not permitted to edit this character") |
|
|
|
} |
|
|
|
|
|
|
|
logs.ScheduleFullUpdate() |
|
|
|
|
|
|
|
character, err = characters.AddNick(character, input.Nick) |
|
|
|
if err != nil { |
|
|
|
return models.Character{}, errors.New("Failed to add nick: " + err.Error()) |
|
|
|
return nil, errors.New("Failed to add nick: " + err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
go logs.ScheduleFullUpdate() |
|
|
|
go changes.Submit("Character", "edit", token.UserID, true, changekeys.Listed(character), character) |
|
|
|
|
|
|
|
return character, nil |
|
|
|
return &character, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (r *mutationResolver) RemoveCharacterNick(ctx context.Context, input input.CharacterNickInput) (models.Character, error) { |
|
|
|
func (r *mutationResolver) RemoveCharacterNick(ctx context.Context, input input.CharacterNickInput) (*models.Character, error) { |
|
|
|
character, err := characters.FindID(input.ID) |
|
|
|
if err != nil { |
|
|
|
return models.Character{}, errors.New("Character not found") |
|
|
|
return nil, errors.New("Character not found") |
|
|
|
} |
|
|
|
|
|
|
|
token := auth.TokenFromContext(ctx) |
|
|
|
if !token.PermittedUser(character.Author, "member", "character.edit") { |
|
|
|
return models.Character{}, errors.New("You are not permitted to edit this character") |
|
|
|
return nil, errors.New("You are not permitted to edit this character") |
|
|
|
} |
|
|
|
|
|
|
|
character, err = characters.RemoveNick(character, input.Nick) |
|
|
|
if err != nil { |
|
|
|
return models.Character{}, errors.New("Failed to remove nick: " + err.Error()) |
|
|
|
return nil, errors.New("Failed to remove nick: " + err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
go logs.ScheduleFullUpdate() |
|
|
|
go changes.Submit("Character", "edit", token.UserID, true, changekeys.Listed(character), character) |
|
|
|
|
|
|
|
return character, nil |
|
|
|
return &character, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (r *mutationResolver) EditCharacter(ctx context.Context, input input.CharacterEditInput) (models.Character, error) { |
|
|
|
func (r *mutationResolver) EditCharacter(ctx context.Context, input input.CharacterEditInput) (*models.Character, error) { |
|
|
|
character, err := characters.FindID(input.ID) |
|
|
|
if err != nil { |
|
|
|
return models.Character{}, errors.New("Character not found") |
|
|
|
return nil, errors.New("Character not found") |
|
|
|
} |
|
|
|
|
|
|
|
if input.Name != nil && len(*input.Name) < 2 { |
|
|
|
return models.Character{}, errors.New("You need to provide a valid name (min length: 2)") |
|
|
|
return nil, errors.New("You need to provide a valid name (min length: 2)") |
|
|
|
} |
|
|
|
if input.ShortName != nil && len(*input.ShortName) < 2 { |
|
|
|
return models.Character{}, errors.New("You need to provide a valid short name (min length: 2)") |
|
|
|
return nil, errors.New("You need to provide a valid short name (min length: 2)") |
|
|
|
} |
|
|
|
|
|
|
|
token := auth.TokenFromContext(ctx) |
|
|
|
if !token.PermittedUser(character.Author, "member", "character.edit") { |
|
|
|
return models.Character{}, errors.New("You are not permitted to edit this character") |
|
|
|
return nil, errors.New("You are not permitted to edit this character") |
|
|
|
} |
|
|
|
|
|
|
|
character, err = characters.Edit(character, input.Name, input.ShortName, input.Description) |
|
|
|
if err != nil { |
|
|
|
return models.Character{}, errors.New("Failed to edit character: " + err.Error()) |
|
|
|
return nil, errors.New("Failed to edit character: " + err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
go changes.Submit("Character", "edit", token.UserID, true, changekeys.Listed(character), character) |
|
|
|
|
|
|
|
return character, nil |
|
|
|
return &character, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (r *mutationResolver) RemoveCharacter(ctx context.Context, input input.CharacterRemoveInput) (models.Character, error) { |
|
|
|
func (r *mutationResolver) RemoveCharacter(ctx context.Context, input input.CharacterRemoveInput) (*models.Character, error) { |
|
|
|
character, err := characters.FindID(input.ID) |
|
|
|
if err != nil { |
|
|
|
return models.Character{}, errors.New("Character not found") |
|
|
|
return nil, errors.New("Character not found") |
|
|
|
} |
|
|
|
|
|
|
|
token := auth.TokenFromContext(ctx) |
|
|
|
if !token.PermittedUser(character.Author, "member", "character.remove") { |
|
|
|
return models.Character{}, errors.New("You are not permitted to remove this character") |
|
|
|
return nil, errors.New("You are not permitted to remove this character") |
|
|
|
} |
|
|
|
|
|
|
|
character, err = characters.Remove(character) |
|
|
|
if err != nil { |
|
|
|
return models.Character{}, errors.New("Failed to remove character: " + err.Error()) |
|
|
|
return nil, errors.New("Failed to remove character: " + err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
go changes.Submit("Character", "remove", token.UserID, true, changekeys.Listed(character), character) |
|
|
|
|
|
|
|
return character, nil |
|
|
|
return &character, nil |
|
|
|
} |