Browse Source

Renamed index to position for Post model and API

1.0
Gisle Aune 6 years ago
parent
commit
91bd25ddf7
  1. 18
      model/log/log.go
  2. 54
      model/log/post.go
  3. 26
      resolver/post.go
  4. 6
      schema/types/post.graphql

18
model/log/log.go

@ -203,7 +203,7 @@ func (log *Log) Posts(kinds ...string) ([]Post, error) {
}
sort.SliceStable(posts, func(i, j int) bool {
return posts[i].Index < posts[j].Index
return posts[i].Position < posts[j].Position
})
return posts, nil
@ -218,19 +218,19 @@ func (log *Log) NewPost(time time.Time, kind, nick, text string) (Post, error) {
postMutex.RLock()
defer postMutex.RUnlock()
index, err := counter.Next("next_post_id", log.ShortID)
position, err := counter.Next("next_post_id", log.ShortID)
if err != nil {
return Post{}, err
}
post := Post{
ID: MakePostID(time),
Index: index,
LogID: log.ShortID,
Time: time,
Kind: kind,
Nick: nick,
Text: text,
ID: MakePostID(time),
Position: position,
LogID: log.ShortID,
Time: time,
Kind: kind,
Nick: nick,
Text: text,
}
err = postCollection.Insert(post)

54
model/log/post.go

@ -17,13 +17,13 @@ var postCollection *mgo.Collection
// A Post is a part of a log file.
type Post struct {
ID string `bson:"_id"`
LogID string `bson:"logId"`
Time time.Time `bson:"time"`
Kind string `bson:"kind"`
Nick string `bson:"nick"`
Text string `bson:"text"`
Index int `bson:"index"`
ID string `bson:"_id"`
LogID string `bson:"logId"`
Time time.Time `bson:"time"`
Kind string `bson:"kind"`
Nick string `bson:"nick"`
Text string `bson:"text"`
Position int `bson:"position"`
}
// Edit the post
@ -67,40 +67,40 @@ func (post *Post) Edit(time *time.Time, kind *string, nick *string, text *string
}
// Move the post
func (post *Post) Move(targetIndex int) error {
if targetIndex < 1 {
return errors.New("Invalid index")
func (post *Post) Move(toPosition int) error {
if toPosition < 1 {
return errors.New("Invalid position")
}
postMutex.Lock()
defer postMutex.Unlock()
// To avoid problems, only allow target indices that are allowed. If it's 1, then there is bound to
// be a post at the index.
if targetIndex > 1 {
// be a post at the position.
if toPosition > 1 {
existingPost := Post{}
err := postCollection.Find(bson.M{"logId": post.LogID, "index": targetIndex}).One(&existingPost)
err := postCollection.Find(bson.M{"logId": post.LogID, "position": toPosition}).One(&existingPost)
if err != nil || existingPost.Index != targetIndex {
return errors.New("No post found at the index")
if err != nil || existingPost.Position != toPosition {
return errors.New("No post found at the position")
}
}
query := bson.M{"logId": post.LogID}
operation := bson.M{"$inc": bson.M{"index": 1}}
operation := bson.M{"$inc": bson.M{"position": 1}}
if targetIndex < post.Index {
if toPosition < post.Position {
query["$and"] = []bson.M{
bson.M{"index": bson.M{"$gte": targetIndex}},
bson.M{"index": bson.M{"$lt": post.Index}},
bson.M{"position": bson.M{"$gte": toPosition}},
bson.M{"position": bson.M{"$lt": post.Position}},
}
} else {
query["$and"] = []bson.M{
bson.M{"index": bson.M{"$gt": post.Index}},
bson.M{"index": bson.M{"$lte": targetIndex}},
bson.M{"position": bson.M{"$gt": post.Position}},
bson.M{"position": bson.M{"$lte": toPosition}},
}
operation["$inc"] = bson.M{"index": -1}
operation["$inc"] = bson.M{"position": -1}
}
_, err := postCollection.UpdateAll(query, operation)
@ -108,12 +108,12 @@ func (post *Post) Move(targetIndex int) error {
return errors.New("moving others: " + err.Error())
}
err = postCollection.UpdateId(post.ID, bson.M{"$set": bson.M{"index": targetIndex}})
err = postCollection.UpdateId(post.ID, bson.M{"$set": bson.M{"position": toPosition}})
if err != nil {
return errors.New("moving: " + err.Error())
}
post.Index = targetIndex
post.Position = toPosition
return nil
}
@ -128,7 +128,7 @@ func ListPostIDs(ids ...string) ([]Post, error) {
return listPosts(bson.M{"_id": bson.M{"$in": ids}})
}
// RemovePost removes a post, moving all subsequent post up one index
// RemovePost removes a post, moving all subsequent post up one position
func RemovePost(id string) (Post, error) {
postMutex.Lock()
defer postMutex.Unlock()
@ -143,7 +143,7 @@ func RemovePost(id string) (Post, error) {
return Post{}, err
}
_, err = postCollection.UpdateAll(bson.M{"logId": post.LogID, "index": bson.M{"$gt": post.Index}}, bson.M{"$inc": bson.M{"index": -1}})
_, err = postCollection.UpdateAll(bson.M{"logId": post.LogID, "position": bson.M{"$gt": post.Position}}, bson.M{"$inc": bson.M{"position": -1}})
if err != nil {
return Post{}, err
}
@ -186,7 +186,7 @@ func init() {
postCollection.EnsureIndexKey("logId")
postCollection.EnsureIndexKey("time")
postCollection.EnsureIndexKey("kind")
postCollection.EnsureIndexKey("index")
postCollection.EnsureIndexKey("position")
err := postCollection.EnsureIndex(mgo.Index{
Key: []string{"$text:text"},

26
resolver/post.go

@ -79,12 +79,12 @@ func (r *MutationResolver) AddPost(ctx context.Context, args struct{ Input *Post
}
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,
"index": post.Index,
"logId": post.LogID,
"time": post.Time,
"kind": post.Kind,
"nick": post.Nick,
"text": post.Text,
"position": post.Position,
})
go log.UpdateCharacters()
@ -142,8 +142,8 @@ func (r *MutationResolver) EditPost(ctx context.Context, args struct{ Input *Pos
// PostMoveInput is an input
type PostMoveInput struct {
ID string
TargetIndex int32
ID string
ToPosition int32
}
// MovePost resolves the movePost mutation
@ -158,14 +158,14 @@ func (r *MutationResolver) MovePost(ctx context.Context, args struct{ Input *Pos
return nil, err
}
err = post.Move(int(args.Input.TargetIndex))
err = post.Move(int(args.Input.ToPosition))
if err != nil {
return nil, err
}
change.Submit("Post", "move", user.ID, post.ID, map[string]interface{}{
"logId": post.LogID,
"targetIndex": args.Input.TargetIndex,
"targetIndex": args.Input.ToPosition,
})
return &PostResolver{P: post}, nil
@ -227,7 +227,7 @@ func (r *PostResolver) Text() string {
return r.P.Text
}
// Index resolves Post.text
func (r *PostResolver) Index() int32 {
return int32(r.P.Index)
// Position resolves Post.text
func (r *PostResolver) Position() int32 {
return int32(r.P.Position)
}

6
schema/types/post.graphql

@ -18,8 +18,8 @@ type Post {
# The post's text, which purpose depends on the kind
text: String!
# The post's index, which is used to sort posts
index: Int!
# The post's position, used for reordering
position: Int!
}
# Input for the addPost mutation
@ -64,5 +64,5 @@ input MovePostInput {
id: String!
# Target index
targetIndex: Int!
toPosition: Int!
}
Loading…
Cancel
Save