address.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 = 0
  26. } else {
  27. entity.Defualt = 1
  28. }
  29. return entity, nil
  30. },
  31. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  32. return &model.Address{}
  33. },
  34. JWT: true,
  35. SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
  36. _userId := apictx.User.ID
  37. userId, err := primitive.ObjectIDFromHex(_userId)
  38. if err != nil {
  39. // 6369f4b028c4bf8b14f47a6b
  40. invalidId, _ := primitive.ObjectIDFromHex("6369f4b028c4bf8b14f47a6b")
  41. return repo.Map{"userId": invalidId}
  42. }
  43. return repo.Map{"userId": userId}
  44. },
  45. SearchProject: []string{"area", "addr", "contact", "phone", "defualt", "createTime"},
  46. DetailProject: []string{"area", "addr", "contact", "phone", "defualt", "createTime"},
  47. })
  48. }