75 lines
1.8 KiB
Go
75 lines
1.8 KiB
Go
// 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 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 *mon.Model
|
|
}
|
|
|
|
func newDefaultCommentModel(conn *mon.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().UTC().UnixNano()
|
|
data.UpdateAt = time.Now().UTC().UnixNano()
|
|
}
|
|
|
|
_, err := m.conn.InsertOne(ctx, 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
|
|
|
|
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 *defaultCommentModel) Update(ctx context.Context, data *Comment) (*mongo.UpdateResult, error) {
|
|
data.UpdateAt = time.Now().UTC().UnixNano()
|
|
|
|
res, err := m.conn.UpdateOne(ctx, 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
|
|
}
|
|
|
|
res, err := m.conn.DeleteOne(ctx, bson.M{"_id": oid})
|
|
return res, err
|
|
}
|