this repo has no description
1package server 2 3import ( 4 "github.com/google/uuid" 5 "github.com/haileyok/cocoon/internal/helpers" 6 "github.com/haileyok/cocoon/models" 7 "github.com/labstack/echo/v4" 8) 9 10type ComAtprotoServerCreateInviteCodeRequest struct { 11 UseCount int `json:"useCount" validate:"required"` 12 ForAccount *string `json:"forAccount,omitempty"` 13} 14 15type ComAtprotoServerCreateInviteCodeResponse struct { 16 Code string `json:"code"` 17} 18 19func (s *Server) handleCreateInviteCode(e echo.Context) error { 20 ctx := e.Request().Context() 21 22 var req ComAtprotoServerCreateInviteCodeRequest 23 if err := e.Bind(&req); err != nil { 24 s.logger.Error("error binding", "error", err) 25 return helpers.ServerError(e, nil) 26 } 27 28 if err := e.Validate(req); err != nil { 29 s.logger.Error("error validating", "error", err) 30 return helpers.InputError(e, nil) 31 } 32 33 ic := uuid.NewString() 34 35 var acc string 36 if req.ForAccount == nil { 37 acc = "admin" 38 } else { 39 acc = *req.ForAccount 40 } 41 42 if err := s.db.Create(ctx, &models.InviteCode{ 43 Code: ic, 44 Did: acc, 45 RemainingUseCount: req.UseCount, 46 }, nil).Error; err != nil { 47 s.logger.Error("error creating invite code", "error", err) 48 return helpers.ServerError(e, nil) 49 } 50 51 return e.JSON(200, ComAtprotoServerCreateInviteCodeResponse{ 52 Code: ic, 53 }) 54}