Renamed index to position for Post model and API
This commit is contained in:
+9
-9
@@ -203,7 +203,7 @@ func (log *Log) Posts(kinds ...string) ([]Post, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sort.SliceStable(posts, func(i, j int) bool {
|
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
|
return posts, nil
|
||||||
@@ -218,19 +218,19 @@ func (log *Log) NewPost(time time.Time, kind, nick, text string) (Post, error) {
|
|||||||
postMutex.RLock()
|
postMutex.RLock()
|
||||||
defer postMutex.RUnlock()
|
defer postMutex.RUnlock()
|
||||||
|
|
||||||
index, err := counter.Next("next_post_id", log.ShortID)
|
position, err := counter.Next("next_post_id", log.ShortID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Post{}, err
|
return Post{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
post := Post{
|
post := Post{
|
||||||
ID: MakePostID(time),
|
ID: MakePostID(time),
|
||||||
Index: index,
|
Position: position,
|
||||||
LogID: log.ShortID,
|
LogID: log.ShortID,
|
||||||
Time: time,
|
Time: time,
|
||||||
Kind: kind,
|
Kind: kind,
|
||||||
Nick: nick,
|
Nick: nick,
|
||||||
Text: text,
|
Text: text,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = postCollection.Insert(post)
|
err = postCollection.Insert(post)
|
||||||
|
|||||||
+27
-27
@@ -17,13 +17,13 @@ var postCollection *mgo.Collection
|
|||||||
|
|
||||||
// A Post is a part of a log file.
|
// A Post is a part of a log file.
|
||||||
type Post struct {
|
type Post struct {
|
||||||
ID string `bson:"_id"`
|
ID string `bson:"_id"`
|
||||||
LogID string `bson:"logId"`
|
LogID string `bson:"logId"`
|
||||||
Time time.Time `bson:"time"`
|
Time time.Time `bson:"time"`
|
||||||
Kind string `bson:"kind"`
|
Kind string `bson:"kind"`
|
||||||
Nick string `bson:"nick"`
|
Nick string `bson:"nick"`
|
||||||
Text string `bson:"text"`
|
Text string `bson:"text"`
|
||||||
Index int `bson:"index"`
|
Position int `bson:"position"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Edit the post
|
// Edit the post
|
||||||
@@ -67,40 +67,40 @@ func (post *Post) Edit(time *time.Time, kind *string, nick *string, text *string
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Move the post
|
// Move the post
|
||||||
func (post *Post) Move(targetIndex int) error {
|
func (post *Post) Move(toPosition int) error {
|
||||||
if targetIndex < 1 {
|
if toPosition < 1 {
|
||||||
return errors.New("Invalid index")
|
return errors.New("Invalid position")
|
||||||
}
|
}
|
||||||
|
|
||||||
postMutex.Lock()
|
postMutex.Lock()
|
||||||
defer postMutex.Unlock()
|
defer postMutex.Unlock()
|
||||||
|
|
||||||
// To avoid problems, only allow target indices that are allowed. If it's 1, then there is bound to
|
// 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.
|
// be a post at the position.
|
||||||
if targetIndex > 1 {
|
if toPosition > 1 {
|
||||||
existingPost := Post{}
|
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 {
|
if err != nil || existingPost.Position != toPosition {
|
||||||
return errors.New("No post found at the index")
|
return errors.New("No post found at the position")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query := bson.M{"logId": post.LogID}
|
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{
|
query["$and"] = []bson.M{
|
||||||
bson.M{"index": bson.M{"$gte": targetIndex}},
|
bson.M{"position": bson.M{"$gte": toPosition}},
|
||||||
bson.M{"index": bson.M{"$lt": post.Index}},
|
bson.M{"position": bson.M{"$lt": post.Position}},
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
query["$and"] = []bson.M{
|
query["$and"] = []bson.M{
|
||||||
bson.M{"index": bson.M{"$gt": post.Index}},
|
bson.M{"position": bson.M{"$gt": post.Position}},
|
||||||
bson.M{"index": bson.M{"$lte": targetIndex}},
|
bson.M{"position": bson.M{"$lte": toPosition}},
|
||||||
}
|
}
|
||||||
|
|
||||||
operation["$inc"] = bson.M{"index": -1}
|
operation["$inc"] = bson.M{"position": -1}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := postCollection.UpdateAll(query, operation)
|
_, err := postCollection.UpdateAll(query, operation)
|
||||||
@@ -108,12 +108,12 @@ func (post *Post) Move(targetIndex int) error {
|
|||||||
return errors.New("moving others: " + err.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 {
|
if err != nil {
|
||||||
return errors.New("moving: " + err.Error())
|
return errors.New("moving: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
post.Index = targetIndex
|
post.Position = toPosition
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -128,7 +128,7 @@ func ListPostIDs(ids ...string) ([]Post, error) {
|
|||||||
return listPosts(bson.M{"_id": bson.M{"$in": ids}})
|
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) {
|
func RemovePost(id string) (Post, error) {
|
||||||
postMutex.Lock()
|
postMutex.Lock()
|
||||||
defer postMutex.Unlock()
|
defer postMutex.Unlock()
|
||||||
@@ -143,7 +143,7 @@ func RemovePost(id string) (Post, error) {
|
|||||||
return Post{}, err
|
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 {
|
if err != nil {
|
||||||
return Post{}, err
|
return Post{}, err
|
||||||
}
|
}
|
||||||
@@ -186,7 +186,7 @@ func init() {
|
|||||||
postCollection.EnsureIndexKey("logId")
|
postCollection.EnsureIndexKey("logId")
|
||||||
postCollection.EnsureIndexKey("time")
|
postCollection.EnsureIndexKey("time")
|
||||||
postCollection.EnsureIndexKey("kind")
|
postCollection.EnsureIndexKey("kind")
|
||||||
postCollection.EnsureIndexKey("index")
|
postCollection.EnsureIndexKey("position")
|
||||||
|
|
||||||
err := postCollection.EnsureIndex(mgo.Index{
|
err := postCollection.EnsureIndex(mgo.Index{
|
||||||
Key: []string{"$text:text"},
|
Key: []string{"$text:text"},
|
||||||
|
|||||||
+13
-13
@@ -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{}{
|
change.Submit("Post", "add", user.ID, post.ID, map[string]interface{}{
|
||||||
"logId": post.LogID,
|
"logId": post.LogID,
|
||||||
"time": post.Time,
|
"time": post.Time,
|
||||||
"kind": post.Kind,
|
"kind": post.Kind,
|
||||||
"nick": post.Nick,
|
"nick": post.Nick,
|
||||||
"text": post.Text,
|
"text": post.Text,
|
||||||
"index": post.Index,
|
"position": post.Position,
|
||||||
})
|
})
|
||||||
|
|
||||||
go log.UpdateCharacters()
|
go log.UpdateCharacters()
|
||||||
@@ -142,8 +142,8 @@ func (r *MutationResolver) EditPost(ctx context.Context, args struct{ Input *Pos
|
|||||||
|
|
||||||
// PostMoveInput is an input
|
// PostMoveInput is an input
|
||||||
type PostMoveInput struct {
|
type PostMoveInput struct {
|
||||||
ID string
|
ID string
|
||||||
TargetIndex int32
|
ToPosition int32
|
||||||
}
|
}
|
||||||
|
|
||||||
// MovePost resolves the movePost mutation
|
// MovePost resolves the movePost mutation
|
||||||
@@ -158,14 +158,14 @@ func (r *MutationResolver) MovePost(ctx context.Context, args struct{ Input *Pos
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = post.Move(int(args.Input.TargetIndex))
|
err = post.Move(int(args.Input.ToPosition))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
change.Submit("Post", "move", user.ID, post.ID, map[string]interface{}{
|
change.Submit("Post", "move", user.ID, post.ID, map[string]interface{}{
|
||||||
"logId": post.LogID,
|
"logId": post.LogID,
|
||||||
"targetIndex": args.Input.TargetIndex,
|
"targetIndex": args.Input.ToPosition,
|
||||||
})
|
})
|
||||||
|
|
||||||
return &PostResolver{P: post}, nil
|
return &PostResolver{P: post}, nil
|
||||||
@@ -227,7 +227,7 @@ func (r *PostResolver) Text() string {
|
|||||||
return r.P.Text
|
return r.P.Text
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index resolves Post.text
|
// Position resolves Post.text
|
||||||
func (r *PostResolver) Index() int32 {
|
func (r *PostResolver) Position() int32 {
|
||||||
return int32(r.P.Index)
|
return int32(r.P.Position)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ type Post {
|
|||||||
# The post's text, which purpose depends on the kind
|
# The post's text, which purpose depends on the kind
|
||||||
text: String!
|
text: String!
|
||||||
|
|
||||||
# The post's index, which is used to sort posts
|
# The post's position, used for reordering
|
||||||
index: Int!
|
position: Int!
|
||||||
}
|
}
|
||||||
|
|
||||||
# Input for the addPost mutation
|
# Input for the addPost mutation
|
||||||
@@ -64,5 +64,5 @@ input MovePostInput {
|
|||||||
id: String!
|
id: String!
|
||||||
|
|
||||||
# Target index
|
# Target index
|
||||||
targetIndex: Int!
|
toPosition: Int!
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user