init comment and post service
This commit is contained in:
parent
53f9231a6a
commit
76f116bf3c
2
go.mod
2
go.mod
|
@ -1,4 +1,4 @@
|
|||
module app-cloudep-feed-service
|
||||
module app-cloudep-tweeting-service
|
||||
|
||||
go 1.22.3
|
||||
|
||||
|
|
|
@ -3,8 +3,8 @@ package commentservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package commentservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package commentservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package commentservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package commentservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package commentservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package commentservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package commentservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package postservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package postservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package postservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package postservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package postservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package postservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package postservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -3,8 +3,8 @@ package postservicelogic
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/mon"
|
||||
|
||||
var _ Comment_likesModel = (*customComment_likesModel)(nil)
|
||||
|
||||
type (
|
||||
// Comment_likesModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customComment_likesModel.
|
||||
Comment_likesModel interface {
|
||||
comment_likesModel
|
||||
}
|
||||
|
||||
customComment_likesModel struct {
|
||||
*defaultComment_likesModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewComment_likesModel returns a model for the mongo.
|
||||
func NewComment_likesModel(url, db, collection string) Comment_likesModel {
|
||||
conn := mon.MustNewModel(url, db, collection)
|
||||
return &customComment_likesModel{
|
||||
defaultComment_likesModel: newDefaultComment_likesModel(conn),
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
// Code generated by goctl. DO NOT EDIT.
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/mon"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type comment_likesModel interface {
|
||||
Insert(ctx context.Context, data *Comment_likes) error
|
||||
FindOne(ctx context.Context, id string) (*Comment_likes, error)
|
||||
Update(ctx context.Context, data *Comment_likes) (*mongo.UpdateResult, error)
|
||||
Delete(ctx context.Context, id string) (int64, error)
|
||||
}
|
||||
|
||||
type defaultComment_likesModel struct {
|
||||
conn *mon.Model
|
||||
}
|
||||
|
||||
func newDefaultComment_likesModel(conn *mon.Model) *defaultComment_likesModel {
|
||||
return &defaultComment_likesModel{conn: conn}
|
||||
}
|
||||
|
||||
func (m *defaultComment_likesModel) Insert(ctx context.Context, data *Comment_likes) error {
|
||||
if data.ID.IsZero() {
|
||||
data.ID = primitive.NewObjectID()
|
||||
data.CreateAt = time.Now()
|
||||
data.UpdateAt = time.Now()
|
||||
}
|
||||
|
||||
_, err := m.conn.InsertOne(ctx, data)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultComment_likesModel) FindOne(ctx context.Context, id string) (*Comment_likes, error) {
|
||||
oid, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidObjectId
|
||||
}
|
||||
|
||||
var data Comment_likes
|
||||
|
||||
err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid})
|
||||
switch err {
|
||||
case nil:
|
||||
return &data, nil
|
||||
case mon.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultComment_likesModel) Update(ctx context.Context, data *Comment_likes) (*mongo.UpdateResult, error) {
|
||||
data.UpdateAt = time.Now()
|
||||
|
||||
res, err := m.conn.UpdateOne(ctx, bson.M{"_id": data.ID}, bson.M{"$set": data})
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (m *defaultComment_likesModel) Delete(ctx context.Context, id string) (int64, error) {
|
||||
oid, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return 0, ErrInvalidObjectId
|
||||
}
|
||||
|
||||
res, err := m.conn.DeleteOne(ctx, bson.M{"_id": oid})
|
||||
return res, err
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type Comment_likes struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
|
||||
// TODO: Fill your own fields
|
||||
UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
|
||||
CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/monc"
|
||||
)
|
||||
|
||||
var _ CommentModel = (*customCommentModel)(nil)
|
||||
|
||||
type (
|
||||
// CommentModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customCommentModel.
|
||||
CommentModel interface {
|
||||
commentModel
|
||||
}
|
||||
|
||||
customCommentModel struct {
|
||||
*defaultCommentModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewCommentModel returns a model for the mongo.
|
||||
func NewCommentModel(url, db, collection string, c cache.CacheConf) CommentModel {
|
||||
conn := monc.MustNewModel(url, db, collection, c)
|
||||
return &customCommentModel{
|
||||
defaultCommentModel: newDefaultCommentModel(conn),
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
// Code generated by goctl. DO NOT EDIT.
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/monc"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
var prefixCommentCacheKey = "cache:comment:"
|
||||
|
||||
type commentModel interface {
|
||||
Insert(ctx context.Context, data *Comment) error
|
||||
FindOne(ctx context.Context, id string) (*Comment, error)
|
||||
Update(ctx context.Context, data *Comment) (*mongo.UpdateResult, error)
|
||||
Delete(ctx context.Context, id string) (int64, error)
|
||||
}
|
||||
|
||||
type defaultCommentModel struct {
|
||||
conn *monc.Model
|
||||
}
|
||||
|
||||
func newDefaultCommentModel(conn *monc.Model) *defaultCommentModel {
|
||||
return &defaultCommentModel{conn: conn}
|
||||
}
|
||||
|
||||
func (m *defaultCommentModel) Insert(ctx context.Context, data *Comment) error {
|
||||
if data.ID.IsZero() {
|
||||
data.ID = primitive.NewObjectID()
|
||||
data.CreateAt = time.Now()
|
||||
data.UpdateAt = time.Now()
|
||||
}
|
||||
|
||||
key := prefixCommentCacheKey + data.ID.Hex()
|
||||
_, err := m.conn.InsertOne(ctx, key, data)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultCommentModel) FindOne(ctx context.Context, id string) (*Comment, error) {
|
||||
oid, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidObjectId
|
||||
}
|
||||
|
||||
var data Comment
|
||||
key := prefixCommentCacheKey + id
|
||||
err = m.conn.FindOne(ctx, key, &data, bson.M{"_id": oid})
|
||||
switch err {
|
||||
case nil:
|
||||
return &data, nil
|
||||
case monc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultCommentModel) Update(ctx context.Context, data *Comment) (*mongo.UpdateResult, error) {
|
||||
data.UpdateAt = time.Now()
|
||||
key := prefixCommentCacheKey + data.ID.Hex()
|
||||
res, err := m.conn.UpdateOne(ctx, key, bson.M{"_id": data.ID}, bson.M{"$set": data})
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (m *defaultCommentModel) Delete(ctx context.Context, id string) (int64, error) {
|
||||
oid, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return 0, ErrInvalidObjectId
|
||||
}
|
||||
key := prefixCommentCacheKey + id
|
||||
res, err := m.conn.DeleteOne(ctx, key, bson.M{"_id": oid})
|
||||
return res, err
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type Comment struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
|
||||
// TODO: Fill your own fields
|
||||
UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
|
||||
CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/mon"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = mon.ErrNotFound
|
||||
ErrInvalidObjectId = errors.New("invalid objectId")
|
||||
)
|
|
@ -0,0 +1,25 @@
|
|||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/mon"
|
||||
|
||||
var _ Post_likesModel = (*customPost_likesModel)(nil)
|
||||
|
||||
type (
|
||||
// Post_likesModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customPost_likesModel.
|
||||
Post_likesModel interface {
|
||||
post_likesModel
|
||||
}
|
||||
|
||||
customPost_likesModel struct {
|
||||
*defaultPost_likesModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewPost_likesModel returns a model for the mongo.
|
||||
func NewPost_likesModel(url, db, collection string) Post_likesModel {
|
||||
conn := mon.MustNewModel(url, db, collection)
|
||||
return &customPost_likesModel{
|
||||
defaultPost_likesModel: newDefaultPost_likesModel(conn),
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
// Code generated by goctl. DO NOT EDIT.
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/mon"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type post_likesModel interface {
|
||||
Insert(ctx context.Context, data *Post_likes) error
|
||||
FindOne(ctx context.Context, id string) (*Post_likes, error)
|
||||
Update(ctx context.Context, data *Post_likes) (*mongo.UpdateResult, error)
|
||||
Delete(ctx context.Context, id string) (int64, error)
|
||||
}
|
||||
|
||||
type defaultPost_likesModel struct {
|
||||
conn *mon.Model
|
||||
}
|
||||
|
||||
func newDefaultPost_likesModel(conn *mon.Model) *defaultPost_likesModel {
|
||||
return &defaultPost_likesModel{conn: conn}
|
||||
}
|
||||
|
||||
func (m *defaultPost_likesModel) Insert(ctx context.Context, data *Post_likes) error {
|
||||
if data.ID.IsZero() {
|
||||
data.ID = primitive.NewObjectID()
|
||||
data.CreateAt = time.Now()
|
||||
data.UpdateAt = time.Now()
|
||||
}
|
||||
|
||||
_, err := m.conn.InsertOne(ctx, data)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultPost_likesModel) FindOne(ctx context.Context, id string) (*Post_likes, error) {
|
||||
oid, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidObjectId
|
||||
}
|
||||
|
||||
var data Post_likes
|
||||
|
||||
err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid})
|
||||
switch err {
|
||||
case nil:
|
||||
return &data, nil
|
||||
case mon.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultPost_likesModel) Update(ctx context.Context, data *Post_likes) (*mongo.UpdateResult, error) {
|
||||
data.UpdateAt = time.Now()
|
||||
|
||||
res, err := m.conn.UpdateOne(ctx, bson.M{"_id": data.ID}, bson.M{"$set": data})
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (m *defaultPost_likesModel) Delete(ctx context.Context, id string) (int64, error) {
|
||||
oid, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return 0, ErrInvalidObjectId
|
||||
}
|
||||
|
||||
res, err := m.conn.DeleteOne(ctx, bson.M{"_id": oid})
|
||||
return res, err
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type Post_likes struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
|
||||
// TODO: Fill your own fields
|
||||
UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
|
||||
CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/monc"
|
||||
)
|
||||
|
||||
var _ PostModel = (*customPostModel)(nil)
|
||||
|
||||
type (
|
||||
// PostModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customPostModel.
|
||||
PostModel interface {
|
||||
postModel
|
||||
}
|
||||
|
||||
customPostModel struct {
|
||||
*defaultPostModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewPostModel returns a model for the mongo.
|
||||
func NewPostModel(url, db, collection string, c cache.CacheConf) PostModel {
|
||||
conn := monc.MustNewModel(url, db, collection, c)
|
||||
return &customPostModel{
|
||||
defaultPostModel: newDefaultPostModel(conn),
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
// Code generated by goctl. DO NOT EDIT.
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/monc"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
var prefixPostCacheKey = "cache:post:"
|
||||
|
||||
type postModel interface {
|
||||
Insert(ctx context.Context, data *Post) error
|
||||
FindOne(ctx context.Context, id string) (*Post, error)
|
||||
Update(ctx context.Context, data *Post) (*mongo.UpdateResult, error)
|
||||
Delete(ctx context.Context, id string) (int64, error)
|
||||
}
|
||||
|
||||
type defaultPostModel struct {
|
||||
conn *monc.Model
|
||||
}
|
||||
|
||||
func newDefaultPostModel(conn *monc.Model) *defaultPostModel {
|
||||
return &defaultPostModel{conn: conn}
|
||||
}
|
||||
|
||||
func (m *defaultPostModel) Insert(ctx context.Context, data *Post) error {
|
||||
if data.ID.IsZero() {
|
||||
data.ID = primitive.NewObjectID()
|
||||
data.CreateAt = time.Now()
|
||||
data.UpdateAt = time.Now()
|
||||
}
|
||||
|
||||
key := prefixPostCacheKey + data.ID.Hex()
|
||||
_, err := m.conn.InsertOne(ctx, key, data)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultPostModel) FindOne(ctx context.Context, id string) (*Post, error) {
|
||||
oid, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidObjectId
|
||||
}
|
||||
|
||||
var data Post
|
||||
key := prefixPostCacheKey + id
|
||||
err = m.conn.FindOne(ctx, key, &data, bson.M{"_id": oid})
|
||||
switch err {
|
||||
case nil:
|
||||
return &data, nil
|
||||
case monc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultPostModel) Update(ctx context.Context, data *Post) (*mongo.UpdateResult, error) {
|
||||
data.UpdateAt = time.Now()
|
||||
key := prefixPostCacheKey + data.ID.Hex()
|
||||
res, err := m.conn.UpdateOne(ctx, key, bson.M{"_id": data.ID}, bson.M{"$set": data})
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (m *defaultPostModel) Delete(ctx context.Context, id string) (int64, error) {
|
||||
oid, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return 0, ErrInvalidObjectId
|
||||
}
|
||||
key := prefixPostCacheKey + id
|
||||
res, err := m.conn.DeleteOne(ctx, key, bson.M{"_id": oid})
|
||||
return res, err
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type Post struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
|
||||
// TODO: Fill your own fields
|
||||
UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
|
||||
CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/mon"
|
||||
|
||||
var _ TagsModel = (*customTagsModel)(nil)
|
||||
|
||||
type (
|
||||
// TagsModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customTagsModel.
|
||||
TagsModel interface {
|
||||
tagsModel
|
||||
}
|
||||
|
||||
customTagsModel struct {
|
||||
*defaultTagsModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewTagsModel returns a model for the mongo.
|
||||
func NewTagsModel(url, db, collection string) TagsModel {
|
||||
conn := mon.MustNewModel(url, db, collection)
|
||||
return &customTagsModel{
|
||||
defaultTagsModel: newDefaultTagsModel(conn),
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
// Code generated by goctl. DO NOT EDIT.
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/mon"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type tagsModel interface {
|
||||
Insert(ctx context.Context, data *Tags) error
|
||||
FindOne(ctx context.Context, id string) (*Tags, error)
|
||||
Update(ctx context.Context, data *Tags) (*mongo.UpdateResult, error)
|
||||
Delete(ctx context.Context, id string) (int64, error)
|
||||
}
|
||||
|
||||
type defaultTagsModel struct {
|
||||
conn *mon.Model
|
||||
}
|
||||
|
||||
func newDefaultTagsModel(conn *mon.Model) *defaultTagsModel {
|
||||
return &defaultTagsModel{conn: conn}
|
||||
}
|
||||
|
||||
func (m *defaultTagsModel) Insert(ctx context.Context, data *Tags) error {
|
||||
if data.ID.IsZero() {
|
||||
data.ID = primitive.NewObjectID()
|
||||
data.CreateAt = time.Now()
|
||||
data.UpdateAt = time.Now()
|
||||
}
|
||||
|
||||
_, err := m.conn.InsertOne(ctx, data)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultTagsModel) FindOne(ctx context.Context, id string) (*Tags, error) {
|
||||
oid, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidObjectId
|
||||
}
|
||||
|
||||
var data Tags
|
||||
|
||||
err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid})
|
||||
switch err {
|
||||
case nil:
|
||||
return &data, nil
|
||||
case mon.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultTagsModel) Update(ctx context.Context, data *Tags) (*mongo.UpdateResult, error) {
|
||||
data.UpdateAt = time.Now()
|
||||
|
||||
res, err := m.conn.UpdateOne(ctx, bson.M{"_id": data.ID}, bson.M{"$set": data})
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (m *defaultTagsModel) Delete(ctx context.Context, id string) (int64, error) {
|
||||
oid, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return 0, ErrInvalidObjectId
|
||||
}
|
||||
|
||||
res, err := m.conn.DeleteOne(ctx, bson.M{"_id": oid})
|
||||
return res, err
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type Tags struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
|
||||
// TODO: Fill your own fields
|
||||
UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
|
||||
CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
|
||||
}
|
|
@ -6,9 +6,9 @@ package server
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/logic/commentservice"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/logic/commentservice"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
)
|
||||
|
||||
type CommentServiceServer struct {
|
||||
|
|
|
@ -6,9 +6,9 @@ package server
|
|||
import (
|
||||
"context"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/logic/postservice"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/logic/postservice"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
)
|
||||
|
||||
type PostServiceServer struct {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package svc
|
||||
|
||||
import "app-cloudep-feed-service/internal/config"
|
||||
import "app-cloudep-tweeting-service/internal/config"
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
|
|
10
tweeting.go
10
tweeting.go
|
@ -4,11 +4,11 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
|
||||
"app-cloudep-feed-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-feed-service/internal/config"
|
||||
commentserviceServer "app-cloudep-feed-service/internal/server/commentservice"
|
||||
postserviceServer "app-cloudep-feed-service/internal/server/postservice"
|
||||
"app-cloudep-feed-service/internal/svc"
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/config"
|
||||
commentserviceServer "app-cloudep-tweeting-service/internal/server/commentservice"
|
||||
postserviceServer "app-cloudep-tweeting-service/internal/server/postservice"
|
||||
"app-cloudep-tweeting-service/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
|
|
Loading…
Reference in New Issue