appsmodelgen.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Code generated by goctl. DO NOT EDIT.
  2. // goctl 1.8.1
  3. package model
  4. import (
  5. "context"
  6. "time"
  7. "github.com/zeromicro/go-zero/core/stores/mon"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. "go.mongodb.org/mongo-driver/mongo"
  11. )
  12. type appsModel interface {
  13. Insert(ctx context.Context, data *Apps) error
  14. FindOne(ctx context.Context, id string) (*Apps, error)
  15. Update(ctx context.Context, data *Apps) (*mongo.UpdateResult, error)
  16. Delete(ctx context.Context, id string) (int64, error)
  17. FindAll(ctx context.Context, apptype string, keyword *string) ([]*Apps, error)
  18. }
  19. type defaultAppsModel struct {
  20. conn *mon.Model
  21. }
  22. func newDefaultAppsModel(conn *mon.Model) *defaultAppsModel {
  23. return &defaultAppsModel{conn: conn}
  24. }
  25. func (m *defaultAppsModel) Insert(ctx context.Context, data *Apps) error {
  26. if data.ID.IsZero() {
  27. data.ID = primitive.NewObjectID()
  28. data.UpdateTime = time.Now()
  29. }
  30. _, err := m.conn.InsertOne(ctx, data)
  31. return err
  32. }
  33. func (m *defaultAppsModel) FindOne(ctx context.Context, id string) (*Apps, error) {
  34. oid, err := primitive.ObjectIDFromHex(id)
  35. if err != nil {
  36. return nil, ErrInvalidObjectId
  37. }
  38. var data Apps
  39. err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid})
  40. switch err {
  41. case nil:
  42. return &data, nil
  43. case mon.ErrNotFound:
  44. return nil, ErrNotFound
  45. default:
  46. return nil, err
  47. }
  48. }
  49. func (m *defaultAppsModel) Update(ctx context.Context, data *Apps) (*mongo.UpdateResult, error) {
  50. data.UpdateTime = time.Now()
  51. res, err := m.conn.UpdateOne(ctx, bson.M{"_id": data.ID}, bson.M{"$set": data})
  52. return res, err
  53. }
  54. func (m *defaultAppsModel) Delete(ctx context.Context, id string) (int64, error) {
  55. oid, err := primitive.ObjectIDFromHex(id)
  56. if err != nil {
  57. return 0, ErrInvalidObjectId
  58. }
  59. res, err := m.conn.DeleteOne(ctx, bson.M{"_id": oid})
  60. return res, err
  61. }
  62. func (m *defaultAppsModel) FindAll(ctx context.Context, apptype string, keyword *string) ([]*Apps, error) {
  63. var data []*Apps
  64. err := m.conn.Find(ctx, &data, bson.M{
  65. "type": apptype,
  66. "name": bson.M{
  67. "$regex": keyword, // 用你想要匹配的模式替换 searchPattern
  68. "$options": "i", // 可选:i 表示不区分大小写
  69. },
  70. })
  71. switch err {
  72. case nil:
  73. return data, nil
  74. case mon.ErrNotFound:
  75. return nil, ErrNotFound
  76. default:
  77. return nil, err
  78. }
  79. }