address.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package api
  2. import (
  3. "3dshow/db/model"
  4. "3dshow/db/repo"
  5. "errors"
  6. "time"
  7. "github.com/gin-gonic/gin"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. )
  10. func Address(r *GinRouter) {
  11. CreateCRUD(r, "/address", &CRUDOption{
  12. Collection: repo.CollectionAddress,
  13. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  14. entity := &model.Address{}
  15. c.ShouldBindJSON(entity)
  16. entity.CreateTime = time.Now()
  17. _userId := apictx.User.ID
  18. userId, err := primitive.ObjectIDFromHex(_userId)
  19. if err != nil {
  20. return nil, errors.New("非法用户")
  21. }
  22. entity.UserId = userId
  23. first, _ := repo.RepoCountDoc(apictx.CreateRepoCtx(), repo.CollectionAddress, repo.Map{"userId": userId})
  24. if first > 0 {
  25. entity.Defualt = 2
  26. } else {
  27. entity.Defualt = 1
  28. }
  29. return entity, nil
  30. },
  31. OnUpdate: func(c *gin.Context, apictx *ApiSession, entity interface{}) {
  32. },
  33. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  34. return &model.Address{}
  35. },
  36. JWT: true,
  37. SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
  38. _userId := apictx.User.ID
  39. userId, err := primitive.ObjectIDFromHex(_userId)
  40. if err != nil {
  41. // 6369f4b028c4bf8b14f47a6b
  42. invalidId, _ := primitive.ObjectIDFromHex("6369f4b028c4bf8b14f47a6b")
  43. return repo.Map{"userId": invalidId}
  44. }
  45. return repo.Map{"userId": userId}
  46. },
  47. SearchProject: []string{"area", "addr", "contact", "phone", "defualt", "createTime"},
  48. DetailProject: []string{"area", "addr", "contact", "phone", "defualt", "createTime"},
  49. })
  50. }