| package admin |
|
|
| import ( |
| "strconv" |
|
|
| "github.com/Wei-Shaw/sub2api/internal/handler/dto" |
| "github.com/Wei-Shaw/sub2api/internal/pkg/response" |
| "github.com/Wei-Shaw/sub2api/internal/service" |
|
|
| "github.com/gin-gonic/gin" |
| ) |
|
|
| |
| type AdminAPIKeyHandler struct { |
| adminService service.AdminService |
| } |
|
|
| |
| func NewAdminAPIKeyHandler(adminService service.AdminService) *AdminAPIKeyHandler { |
| return &AdminAPIKeyHandler{ |
| adminService: adminService, |
| } |
| } |
|
|
| |
| type AdminUpdateAPIKeyGroupRequest struct { |
| GroupID *int64 `json:"group_id"` |
| } |
|
|
| |
| |
| func (h *AdminAPIKeyHandler) UpdateGroup(c *gin.Context) { |
| keyID, err := strconv.ParseInt(c.Param("id"), 10, 64) |
| if err != nil { |
| response.BadRequest(c, "Invalid API key ID") |
| return |
| } |
|
|
| var req AdminUpdateAPIKeyGroupRequest |
| if err := c.ShouldBindJSON(&req); err != nil { |
| response.BadRequest(c, "Invalid request: "+err.Error()) |
| return |
| } |
|
|
| result, err := h.adminService.AdminUpdateAPIKeyGroupID(c.Request.Context(), keyID, req.GroupID) |
| if err != nil { |
| response.ErrorFrom(c, err) |
| return |
| } |
|
|
| resp := struct { |
| APIKey *dto.APIKey `json:"api_key"` |
| AutoGrantedGroupAccess bool `json:"auto_granted_group_access"` |
| GrantedGroupID *int64 `json:"granted_group_id,omitempty"` |
| GrantedGroupName string `json:"granted_group_name,omitempty"` |
| }{ |
| APIKey: dto.APIKeyFromService(result.APIKey), |
| AutoGrantedGroupAccess: result.AutoGrantedGroupAccess, |
| GrantedGroupID: result.GrantedGroupID, |
| GrantedGroupName: result.GrantedGroupName, |
| } |
| response.Success(c, resp) |
| } |
|
|