CreateCode godoc @Security ApiKey @Summary Create a new code @Description create a new code @Tags Code @Accept json @Produce json @Param current_user query string false "current user, the owner" @Param body body types.CreateCodeReq true "body" @Success
(ctx *gin.Context)
| 45 | // @Failure 500 {object} types.APIInternalServerError "Internal server error" |
| 46 | // @Router /codes [post] |
| 47 | func (h *CodeHandler) Create(ctx *gin.Context) { |
| 48 | currentUser := httpbase.GetCurrentUser(ctx) |
| 49 | if currentUser == "" { |
| 50 | httpbase.UnauthorizedError(ctx, component.ErrUserNotFound) |
| 51 | return |
| 52 | } |
| 53 | var req *types.CreateCodeReq |
| 54 | if err := ctx.ShouldBindJSON(&req); err != nil { |
| 55 | slog.Error("Bad request format", "error", err) |
| 56 | httpbase.BadRequest(ctx, err.Error()) |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | _, err := h.sc.CheckRequest(ctx, req) |
| 61 | if err != nil { |
| 62 | slog.Error("failed to check sensitive request", slog.Any("error", err)) |
| 63 | httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error()) |
| 64 | return |
| 65 | } |
| 66 | req.Username = currentUser |
| 67 | |
| 68 | code, err := h.c.Create(ctx, req) |
| 69 | if err != nil { |
| 70 | slog.Error("Failed to create code", slog.Any("error", err)) |
| 71 | httpbase.ServerError(ctx, err) |
| 72 | return |
| 73 | } |
| 74 | slog.Info("Create code succeed", slog.String("code", code.Name)) |
| 75 | respData := gin.H{ |
| 76 | "data": code, |
| 77 | } |
| 78 | ctx.JSON(http.StatusOK, respData) |
| 79 | } |
| 80 | |
| 81 | // GetVisiableCodes godoc |
| 82 | // @Security ApiKey |
no test coverage detected