mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-17 13:24:56 +00:00
@@ -39,8 +39,8 @@ type Posts struct {
|
||||
}
|
||||
|
||||
func (p *Posts) Save(ctx context.Context, req *posts.SaveRequest, rsp *posts.SaveResponse) error {
|
||||
if len(req.Id) == 0 || len(req.Title) == 0 || len(req.Content) == 0 {
|
||||
return errors.BadRequest("posts.save.input-check", "Id, title or content is missing")
|
||||
if len(req.Id) == 0 {
|
||||
return errors.BadRequest("posts.save.input-check", "Id is missing")
|
||||
}
|
||||
|
||||
// read by post
|
||||
@@ -71,27 +71,48 @@ func (p *Posts) Save(ctx context.Context, req *posts.SaveRequest, rsp *posts.Sav
|
||||
if err != nil {
|
||||
return errors.InternalServerError("posts.save.unmarshal", "Failed to unmarshal old post: %v", err.Error())
|
||||
}
|
||||
|
||||
post := &Post{
|
||||
ID: req.Id,
|
||||
Title: req.Title,
|
||||
Content: req.Content,
|
||||
Slug: postSlug,
|
||||
Tags: req.Tags,
|
||||
Title: oldPost.Title,
|
||||
Content: oldPost.Content,
|
||||
Slug: oldPost.Slug,
|
||||
Tags: oldPost.Tags,
|
||||
CreateTimestamp: oldPost.CreateTimestamp,
|
||||
UpdateTimestamp: time.Now().Unix(),
|
||||
}
|
||||
if len(req.Title) > 0 {
|
||||
post.Title = req.Title
|
||||
post.Slug = slug.Make(post.Title)
|
||||
}
|
||||
if len(req.Slug) > 0 {
|
||||
post.Slug = req.Slug
|
||||
}
|
||||
if len(req.Content) > 0 {
|
||||
post.Content = req.Content
|
||||
}
|
||||
if len(req.Tags) > 0 {
|
||||
// Handle the special case of deletion
|
||||
if len(req.Tags) == 0 && req.Tags[0] == "" {
|
||||
post.Tags = []string{}
|
||||
} else {
|
||||
post.Tags = req.Tags
|
||||
}
|
||||
}
|
||||
|
||||
// Check if slug exists
|
||||
recordsBySlug, err := store.Read(fmt.Sprintf("%v:%v", slugPrefix, postSlug))
|
||||
if err != nil && err != store.ErrNotFound {
|
||||
return errors.InternalServerError("posts.save.store-read", "Failed to read post by slug: %v", err.Error())
|
||||
}
|
||||
otherSlugPost := &Post{}
|
||||
err = json.Unmarshal(record.Value, otherSlugPost)
|
||||
if err != nil {
|
||||
return errors.InternalServerError("posts.save.slug-unmarshal", "Error unmarshaling other post with same slug: %v", err.Error())
|
||||
}
|
||||
if len(recordsBySlug) > 0 && oldPost.ID != otherSlugPost.ID {
|
||||
if len(recordsBySlug) > 0 {
|
||||
otherSlugPost := &Post{}
|
||||
err = json.Unmarshal(recordsBySlug[0].Value, otherSlugPost)
|
||||
if oldPost.ID != otherSlugPost.ID {
|
||||
if err != nil {
|
||||
return errors.InternalServerError("posts.save.slug-unmarshal", "Error unmarshaling other post with same slug: %v", err.Error())
|
||||
}
|
||||
}
|
||||
return errors.BadRequest("posts.save.slug-check", "An other post with this slug already exists")
|
||||
}
|
||||
|
||||
@@ -135,9 +156,9 @@ func (p *Posts) savePost(ctx context.Context, oldPost, post *Post) error {
|
||||
if oldPost == nil {
|
||||
for _, tagName := range post.Tags {
|
||||
_, err := p.Tags.Add(ctx, &tags.AddRequest{
|
||||
ParentID: post.ID,
|
||||
Type: tagType,
|
||||
Title: tagName,
|
||||
ResourceID: post.ID,
|
||||
Type: tagType,
|
||||
Title: tagName,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -161,9 +182,9 @@ func (p *Posts) diffTags(ctx context.Context, parentID string, oldTagNames, newT
|
||||
_, stillThere := newTags[i]
|
||||
if !stillThere {
|
||||
_, err := p.Tags.Remove(ctx, &tags.RemoveRequest{
|
||||
ParentID: parentID,
|
||||
Type: tagType,
|
||||
Title: i,
|
||||
ResourceID: parentID,
|
||||
Type: tagType,
|
||||
Title: i,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Errorf("Error decreasing count for tag '%v' with type '%v' for parent '%v'", i, tagType, parentID)
|
||||
@@ -174,9 +195,9 @@ func (p *Posts) diffTags(ctx context.Context, parentID string, oldTagNames, newT
|
||||
_, newlyAdded := oldTags[i]
|
||||
if newlyAdded {
|
||||
_, err := p.Tags.Add(ctx, &tags.AddRequest{
|
||||
ParentID: parentID,
|
||||
Type: tagType,
|
||||
Title: i,
|
||||
ResourceID: parentID,
|
||||
Type: tagType,
|
||||
Title: i,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Errorf("Error increasing count for tag '%v' with type '%v' for parent '%v': %v", i, tagType, parentID, err)
|
||||
@@ -193,6 +214,10 @@ func (p *Posts) Query(ctx context.Context, req *pb.QueryRequest, rsp *pb.QueryRe
|
||||
key := fmt.Sprintf("%v:%v", slugPrefix, req.Slug)
|
||||
logger.Infof("Reading post by slug: %v", req.Slug)
|
||||
records, err = store.Read("", store.Prefix(key))
|
||||
} else if len(req.Id) > 0 {
|
||||
key := fmt.Sprintf("%v:%v", idPrefix, req.Id)
|
||||
logger.Infof("Reading post by id: %v", req.Id)
|
||||
records, err = store.Read("", store.Prefix(key))
|
||||
} else {
|
||||
key := fmt.Sprintf("%v:", timeStampPrefix)
|
||||
var limit uint
|
||||
|
||||
@@ -115,10 +115,13 @@ func (m *Post) GetTags() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Query posts. Acts as a listing when no id or slug provided.
|
||||
// Gets a single post by id or slug if any of them provided.
|
||||
type QueryRequest struct {
|
||||
Slug string `protobuf:"bytes,1,opt,name=slug,proto3" json:"slug,omitempty"`
|
||||
Offset int64 `protobuf:"varint,2,opt,name=offset,proto3" json:"offset,omitempty"`
|
||||
Limit int64 `protobuf:"varint,3,opt,name=limit,proto3" json:"limit,omitempty"`
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Slug string `protobuf:"bytes,2,opt,name=slug,proto3" json:"slug,omitempty"`
|
||||
Offset int64 `protobuf:"varint,3,opt,name=offset,proto3" json:"offset,omitempty"`
|
||||
Limit int64 `protobuf:"varint,4,opt,name=limit,proto3" json:"limit,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
@@ -149,6 +152,13 @@ func (m *QueryRequest) XXX_DiscardUnknown() {
|
||||
|
||||
var xxx_messageInfo_QueryRequest proto.InternalMessageInfo
|
||||
|
||||
func (m *QueryRequest) GetId() string {
|
||||
if m != nil {
|
||||
return m.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *QueryRequest) GetSlug() string {
|
||||
if m != nil {
|
||||
return m.Slug
|
||||
@@ -210,11 +220,13 @@ func (m *QueryResponse) GetPosts() []*Post {
|
||||
}
|
||||
|
||||
type SaveRequest struct {
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"`
|
||||
Slug string `protobuf:"bytes,3,opt,name=slug,proto3" json:"slug,omitempty"`
|
||||
Content string `protobuf:"bytes,4,opt,name=content,proto3" json:"content,omitempty"`
|
||||
Timestamp int64 `protobuf:"varint,5,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"`
|
||||
Slug string `protobuf:"bytes,3,opt,name=slug,proto3" json:"slug,omitempty"`
|
||||
Content string `protobuf:"bytes,4,opt,name=content,proto3" json:"content,omitempty"`
|
||||
Timestamp int64 `protobuf:"varint,5,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
|
||||
// When updating a post and wanting to delete all tags,
|
||||
// send a list of tags with only one member being an empty string [""]
|
||||
Tags []string `protobuf:"bytes,6,rep,name=tags,proto3" json:"tags,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
@@ -412,28 +424,28 @@ func init() {
|
||||
}
|
||||
|
||||
var fileDescriptor_a1e4efc789192621 = []byte{
|
||||
// 363 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xb4, 0x52, 0x5d, 0x4e, 0xf3, 0x30,
|
||||
// 365 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xac, 0x52, 0x5d, 0x4e, 0xf3, 0x30,
|
||||
0x10, 0xfc, 0xd2, 0xfc, 0xf4, 0xeb, 0xf6, 0x47, 0x68, 0x5b, 0xc0, 0xaa, 0x10, 0x94, 0x3c, 0xf5,
|
||||
0xa9, 0x88, 0x82, 0xc4, 0x05, 0x38, 0x40, 0x09, 0x27, 0x08, 0xd4, 0x2d, 0x91, 0xd2, 0x26, 0xc4,
|
||||
0x0e, 0x12, 0xe7, 0xe0, 0x14, 0x5c, 0x81, 0xd3, 0x61, 0xaf, 0xed, 0x92, 0x56, 0xe2, 0x91, 0x97,
|
||||
0x68, 0x67, 0x36, 0x1e, 0xcf, 0x4c, 0x02, 0xa7, 0x65, 0x55, 0xc8, 0xe2, 0xaa, 0x2c, 0x84, 0x14,
|
||||
0xe6, 0x39, 0x23, 0x06, 0x43, 0x02, 0xf1, 0x97, 0x07, 0xc1, 0x42, 0x4d, 0x38, 0x80, 0x56, 0xb6,
|
||||
0x64, 0xde, 0xc4, 0x9b, 0x76, 0x12, 0x35, 0xe1, 0x08, 0x42, 0x99, 0xc9, 0x9c, 0xb3, 0x16, 0x51,
|
||||
0x06, 0x20, 0x42, 0x20, 0xf2, 0x7a, 0xcd, 0x7c, 0x22, 0x69, 0x46, 0x06, 0xed, 0xe7, 0x62, 0x2b,
|
||||
0xf9, 0x56, 0xb2, 0x80, 0x68, 0x07, 0x69, 0x53, 0xf1, 0x54, 0xf2, 0x25, 0x0b, 0xd5, 0xc6, 0x4f,
|
||||
0x1c, 0xd4, 0x9b, 0xba, 0x5c, 0xd2, 0x26, 0x32, 0x1b, 0x0b, 0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3, 0x3d, 0x6e, 0x77, 0xe4, 0x0e, 0x22, 0x63, 0x02, 0x9d,
|
||||
0xe8, 0x9e, 0xe9, 0xf1, 0xf1, 0x01, 0xeb, 0x0e, 0x3e, 0x45, 0xf4, 0x97, 0xdf, 0x7c, 0x07, 0x00,
|
||||
0x00, 0xff, 0xff, 0xed, 0x03, 0x8f, 0x37, 0x00, 0x03, 0x00, 0x00,
|
||||
0xa9, 0x88, 0x82, 0xc4, 0x05, 0x38, 0x00, 0x84, 0x0b, 0x10, 0xa8, 0x5b, 0x22, 0xa5, 0x75, 0x88,
|
||||
0x1d, 0x24, 0xce, 0xc1, 0x29, 0xb8, 0x02, 0xa7, 0x23, 0x5e, 0x3b, 0x25, 0xa9, 0xe8, 0x1b, 0x2f,
|
||||
0xd1, 0xce, 0x6c, 0x76, 0x77, 0x66, 0x12, 0x38, 0xce, 0x72, 0xa1, 0xc4, 0x45, 0x26, 0xa4, 0x92,
|
||||
0xe6, 0x39, 0x23, 0x06, 0x7d, 0x02, 0xe1, 0x97, 0x03, 0xde, 0x5d, 0x59, 0xe1, 0x00, 0x5a, 0xc9,
|
||||
0x82, 0x39, 0x13, 0x67, 0xda, 0x89, 0xca, 0x0a, 0x47, 0xe0, 0xab, 0x44, 0xa5, 0x9c, 0xb5, 0x88,
|
||||
0x32, 0x00, 0x11, 0x3c, 0x99, 0x16, 0x2b, 0xe6, 0x12, 0x49, 0x35, 0x32, 0x68, 0x3f, 0x8b, 0x8d,
|
||||
0xe2, 0x1b, 0xc5, 0x3c, 0xa2, 0x2b, 0x48, 0x9d, 0x9c, 0xc7, 0x8a, 0x2f, 0x98, 0x5f, 0x76, 0xdc,
|
||||
0xa8, 0x82, 0xba, 0x53, 0x64, 0x0b, 0xea, 0x04, 0xa6, 0x63, 0x21, 0x1e, 0x41, 0x10, 0x17, 0xea,
|
||||
0x45, 0xe4, 0xac, 0x4d, 0xcb, 0x2c, 0xd2, 0x97, 0x55, 0xbc, 0x92, 0xec, 0xff, 0x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, 0xfd, 0xf5, 0x57, 0xdf,
|
||||
0x01, 0x00, 0x00, 0xff, 0xff, 0x05, 0x88, 0xa9, 0x15, 0x10, 0x03, 0x00, 0x00,
|
||||
}
|
||||
|
||||
@@ -20,10 +20,14 @@ message Post {
|
||||
repeated string tags = 8;
|
||||
}
|
||||
|
||||
// Query posts. Acts as a listing when no id or slug provided.
|
||||
// Gets a single post by id or slug if any of them provided.
|
||||
message QueryRequest {
|
||||
string slug = 1;
|
||||
int64 offset = 2;
|
||||
int64 limit = 3;
|
||||
string id = 1;
|
||||
string slug = 2;
|
||||
string tag = 3;
|
||||
int64 offset = 4;
|
||||
int64 limit = 5;
|
||||
}
|
||||
|
||||
message QueryResponse {
|
||||
@@ -36,6 +40,8 @@ message SaveRequest {
|
||||
string slug = 3;
|
||||
string content = 4;
|
||||
int64 timestamp = 5;
|
||||
// When updating a post and wanting to delete all tags,
|
||||
// send a list of tags with only one member being an empty string [""]
|
||||
repeated string tags = 6;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user