account_capability_handler.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package handler
  2. import (
  3. "github.com/Wei-Shaw/sub2api/internal/handler/admin"
  4. "github.com/Wei-Shaw/sub2api/internal/pkg/response"
  5. "github.com/Wei-Shaw/sub2api/internal/server/middleware"
  6. "github.com/Wei-Shaw/sub2api/internal/service"
  7. "github.com/gin-gonic/gin"
  8. )
  9. type AccountCapabilityHandler struct {
  10. svc *service.IntelligentTestService
  11. }
  12. func NewAccountCapabilityHandler(svc *service.IntelligentTestService) *AccountCapabilityHandler {
  13. return &AccountCapabilityHandler{svc: svc}
  14. }
  15. func capabilityUser(c *gin.Context) (int64, bool) {
  16. subject, ok := middleware.GetAuthSubjectFromContext(c)
  17. if !ok || subject.UserID < 1 {
  18. response.Unauthorized(c, "authentication required")
  19. return 0, false
  20. }
  21. c.Header("Cache-Control", "private, no-store")
  22. return subject.UserID, true
  23. }
  24. func (h *AccountCapabilityHandler) List(c *gin.Context) {
  25. user, ok := capabilityUser(c)
  26. if !ok {
  27. return
  28. }
  29. f, ok := admin.ParseIntelligentTestFilter(c)
  30. if !ok {
  31. return
  32. }
  33. items, total, err := h.svc.Capabilities(c.Request.Context(), user, f)
  34. if !response.ErrorFrom(c, err) {
  35. response.Success(c, gin.H{"items": items, "total": total, "page": f.Page, "page_size": f.PageSize})
  36. }
  37. }
  38. func (h *AccountCapabilityHandler) History(c *gin.Context) {
  39. user, ok := capabilityUser(c)
  40. if !ok {
  41. return
  42. }
  43. id, ok := admin.IntelligentTestParamID(c, "account_id")
  44. if !ok {
  45. return
  46. }
  47. f, ok := admin.ParseIntelligentTestFilter(c)
  48. if !ok {
  49. return
  50. }
  51. f.AccountID = id
  52. out, err := h.svc.PublicRecords(c.Request.Context(), user, f)
  53. if !response.ErrorFrom(c, err) {
  54. response.Success(c, out)
  55. }
  56. }
  57. func (h *AccountCapabilityHandler) Get(c *gin.Context) {
  58. user, ok := capabilityUser(c)
  59. if !ok {
  60. return
  61. }
  62. id, ok := admin.IntelligentTestParamID(c, "id")
  63. if !ok {
  64. return
  65. }
  66. out, err := h.svc.PublicGet(c.Request.Context(), user, id)
  67. if !response.ErrorFrom(c, err) {
  68. response.Success(c, out)
  69. }
  70. }
  71. func (h *AccountCapabilityHandler) Image(c *gin.Context) {
  72. user, ok := capabilityUser(c)
  73. if !ok {
  74. return
  75. }
  76. id, ok := admin.IntelligentTestParamID(c, "id")
  77. if !ok {
  78. return
  79. }
  80. out, err := h.svc.PublicGet(c.Request.Context(), user, id)
  81. if response.ErrorFrom(c, err) {
  82. return
  83. }
  84. admin.WriteIntelligentTestImage(c, out.ResultImage)
  85. }