appsmodelgen.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. }
  18. type defaultAppsModel struct {
  19. conn *mon.Model
  20. }
  21. func newDefaultAppsModel(conn *mon.Model) *defaultAppsModel {
  22. return &defaultAppsModel{conn: conn}
  23. }
  24. func (m *defaultAppsModel) Insert(ctx context.Context, data *Apps) error {
  25. if data.ID.IsZero() {
  26. data.ID = primitive.NewObjectID()
  27. data.UpdateTime = time.Now()
  28. }
  29. _, err := m.conn.InsertOne(ctx, data)
  30. return err
  31. }
  32. func (m *defaultAppsModel) FindOne(ctx context.Context, id string) (*Apps, error) {
  33. oid, err := primitive.ObjectIDFromHex(id)
  34. if err != nil {
  35. return nil, ErrInvalidObjectId
  36. }
  37. var data Apps
  38. err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid})
  39. switch err {
  40. case nil:
  41. return &data, nil
  42. case mon.ErrNotFound:
  43. return nil, ErrNotFound
  44. default:
  45. return nil, err
  46. }
  47. }
  48. func (m *defaultAppsModel) Update(ctx context.Context, data *Apps) (*mongo.UpdateResult, error) {
  49. data.UpdateTime = time.Now()
  50. res, err := m.conn.UpdateOne(ctx, bson.M{"_id": data.ID}, bson.M{"$set": data})
  51. return res, err
  52. }
  53. func (m *defaultAppsModel) Delete(ctx context.Context, id string) (int64, error) {
  54. oid, err := primitive.ObjectIDFromHex(id)
  55. if err != nil {
  56. return 0, ErrInvalidObjectId
  57. }
  58. res, err := m.conn.DeleteOne(ctx, bson.M{"_id": oid})
  59. return res, err
  60. }