|
|
package resolver
import ( "context" "time"
"git.aiterp.net/rpdata/api/internal/session" "git.aiterp.net/rpdata/api/model/change" "git.aiterp.net/rpdata/api/model/log" "git.aiterp.net/rpdata/api/resolver/types" )
// PostArgs is an arg
type PostArgs struct { ID string }
// Post implements the post query
func (r *QueryResolver) Post(ctx context.Context, args *PostArgs) (*types.PostResolver, error) { post, err := log.FindPostID(args.ID) if err != nil { return nil, err }
return &types.PostResolver{P: post}, nil }
// PostsArgs is an arg
type PostsArgs struct { IDs []string }
// Posts implements the posts query
func (r *QueryResolver) Posts(ctx context.Context, args *PostsArgs) ([]*types.PostResolver, error) { posts, err := log.ListPostIDs(args.IDs...) if err != nil { return nil, err }
resolvers := make([]*types.PostResolver, len(posts)) for i := range resolvers { resolvers[i] = &types.PostResolver{P: posts[i]} }
return resolvers, nil }
// PostAddArgs is args for addPost mutation
type PostAddArgs struct { Input *struct { LogID string Time string Kind string Nick string Text string } }
// AddPost resolves the addPost mutation
func (r *MutationResolver) AddPost(ctx context.Context, args *PostAddArgs) (*types.PostResolver, error) { input := args.Input
user := session.FromContext(ctx).User() if user == nil || !user.Permitted("post.add") { return nil, ErrUnauthorized }
postTime, err := time.Parse(time.RFC3339Nano, input.Time) if err != nil { return nil, err }
log, err := log.FindID(input.LogID) if err != nil { return nil, err }
post, err := log.NewPost(postTime, input.Kind, input.Nick, input.Text) if err != nil { return nil, err }
go change.Submit("Post", "add", user.ID, post.ID, map[string]interface{}{ "logId": post.LogID, "time": post.Time, "kind": post.Kind, "nick": post.Nick, "text": post.Text, "position": post.Position, })
go log.UpdateCharacters()
return &types.PostResolver{P: post}, nil }
// PostEditArgs is args for the editPost mutation
type PostEditArgs struct { Input *struct { ID string Time *string Kind *string Nick *string Text *string } }
// EditPost resolves the editPost mutation
func (r *MutationResolver) EditPost(ctx context.Context, args *PostEditArgs) (*types.PostResolver, error) { input := args.Input
user := session.FromContext(ctx).User() if user == nil || !user.Permitted("post.edit") { return nil, ErrUnauthorized }
postTime := (*time.Time)(nil) if args.Input.Time != nil { t, err := time.Parse(time.RFC3339Nano, *input.Time) if err != nil { return nil, err }
postTime = &t }
post, err := log.FindPostID(input.ID) if err != nil { return nil, err }
err = post.Edit(postTime, input.Kind, input.Nick, input.Text) if err != nil { return nil, err }
go change.Submit("Post", "edit", user.ID, post.ID, map[string]interface{}{ "time": postTime, "kind": input.Kind, "nick": input.Nick, "text": input.Text, })
go log.UpdateCharacters(post.LogID)
return &types.PostResolver{P: post}, nil }
// PostMoveArgs is args for movePost mutation
type PostMoveArgs struct { Input *struct { ID string ToPosition int32 } }
// MovePost resolves the movePost mutation
func (r *MutationResolver) MovePost(ctx context.Context, args *PostMoveArgs) (*types.PostResolver, error) { input := args.Input
user := session.FromContext(ctx).User() if user == nil || !user.Permitted("post.move") { return nil, ErrUnauthorized }
post, err := log.FindPostID(input.ID) if err != nil { return nil, err }
err = post.Move(int(input.ToPosition)) if err != nil { return nil, err }
go change.Submit("Post", "move", user.ID, post.ID, map[string]interface{}{ "logId": post.LogID, "targetIndex": input.ToPosition, })
return &types.PostResolver{P: post}, nil }
// PostRemoveArgs is an arg
type PostRemoveArgs struct { ID string }
// RemovePost resolves the removePost mutation
func (r *MutationResolver) RemovePost(ctx context.Context, args PostRemoveArgs) (*types.PostResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("post.remove") { return nil, ErrUnauthorized }
post, err := log.RemovePost(args.ID) if err != nil { return nil, err }
go change.Submit("Post", "remove", user.ID, post.ID, map[string]interface{}{ "logId": post.LogID, })
go log.UpdateCharacters(post.LogID)
return &types.PostResolver{P: post}, nil }
|