1 Star 0 Fork 0

MinKo-U/keyauth

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
dao.go 2.99 KB
一键复制 编辑 原始数据 按行查看 历史
MinKo-U 提交于 2022-11-20 16:16 . 修改包名
package impl
import (
"context"
"fmt"
"gitee.com/MinKo-U/keyauth/apps/book"
"github.com/infraboard/mcube/exception"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func (s *service) save(ctx context.Context, ins *book.Book) error {
if _, err := s.col.InsertOne(ctx, ins); err != nil {
return exception.NewInternalServerError("inserted book(%s) document error, %s",
ins.Data.Name, err)
}
return nil
}
func (s *service) get(ctx context.Context, id string) (*book.Book, error) {
filter := bson.M{"_id": id}
ins := book.NewDefaultBook()
if err := s.col.FindOne(ctx, filter).Decode(ins); err != nil {
if err == mongo.ErrNoDocuments {
return nil, exception.NewNotFound("book %s not found", id)
}
return nil, exception.NewInternalServerError("find book %s error, %s", id, err)
}
return ins, nil
}
func newQueryBookRequest(r *book.QueryBookRequest) *queryBookRequest {
return &queryBookRequest{
r,
}
}
type queryBookRequest struct {
*book.QueryBookRequest
}
func (r *queryBookRequest) FindOptions() *options.FindOptions {
pageSize := int64(r.Page.PageSize)
skip := int64(r.Page.PageSize) * int64(r.Page.PageNumber-1)
opt := &options.FindOptions{
Sort: bson.D{
{Key: "create_at", Value: -1},
},
Limit: &pageSize,
Skip: &skip,
}
return opt
}
func (r *queryBookRequest) FindFilter() bson.M {
filter := bson.M{}
if r.Keywords != "" {
filter["$or"] = bson.A{
bson.M{"data.name": bson.M{"$regex": r.Keywords, "$options": "im"}},
bson.M{"data.author": bson.M{"$regex": r.Keywords, "$options": "im"}},
}
}
return filter
}
func (s *service) query(ctx context.Context, req *queryBookRequest) (*book.BookSet, error) {
resp, err := s.col.Find(ctx, req.FindFilter(), req.FindOptions())
if err != nil {
return nil, exception.NewInternalServerError("find book error, error is %s", err)
}
set := book.NewBookSet()
// 循环
for resp.Next(ctx) {
ins := book.NewDefaultBook()
if err := resp.Decode(ins); err != nil {
return nil, exception.NewInternalServerError("decode book error, error is %s", err)
}
set.Add(ins)
}
// count
count, err := s.col.CountDocuments(ctx, req.FindFilter())
if err != nil {
return nil, exception.NewInternalServerError("get book count error, error is %s", err)
}
set.Total = count
return set, nil
}
func (s *service) update(ctx context.Context, ins *book.Book) error {
if _, err := s.col.UpdateByID(ctx, ins.Id, ins); err != nil {
return exception.NewInternalServerError("inserted book(%s) document error, %s",
ins.Data.Name, err)
}
return nil
}
func (s *service) deleteBook(ctx context.Context, ins *book.Book) error {
if ins == nil || ins.Id == "" {
return fmt.Errorf("book is nil")
}
result, err := s.col.DeleteOne(ctx, bson.M{"_id": ins.Id})
if err != nil {
return exception.NewInternalServerError("delete book(%s) error, %s", ins.Id, err)
}
if result.DeletedCount == 0 {
return exception.NewNotFound("book %s not found", ins.Id)
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/MinKo-U/keyauth.git
git@gitee.com:MinKo-U/keyauth.git
MinKo-U
keyauth
keyauth
v1.0.9

搜索帮助