MCPcopy Index your code
hub / github.com/coder/coder / postUserSkill

Method postUserSkill

coderd/userskills.go:49–114  ·  view source on GitHub ↗

@Summary Create a user skill @ID create-a-user-skill @Security CoderSessionToken @Accept json @Produce json @Tags Users @Param user path string true "User ID, username, or me" @Param request body codersdk.CreateUserSkillRequest true "Create user skill request" @Success 201 {object} codersdk.UserSkil

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

47// @Router /api/experimental/users/{user}/skills [post]
48// @x-apidocgen {"skip": true}
49func (api *API) postUserSkill(rw http.ResponseWriter, r *http.Request) {
50 var (
51 ctx = r.Context()
52 user = httpmw.UserParam(r)
53 auditor = api.Auditor.Load()
54 aReq, commitAudit = audit.InitRequest[database.UserSkill](rw, &audit.RequestParams{
55 Audit: *auditor,
56 Log: api.Logger,
57 Request: r,
58 Action: database.AuditActionCreate,
59 })
60 )
61 defer commitAudit()
62
63 r.Body = http.MaxBytesReader(rw, r.Body, maxPersonalSkillRequestBytes)
64
65 var req codersdk.CreateUserSkillRequest
66 if !httpapi.Read(ctx, rw, r, &req) {
67 return
68 }
69
70 parsedSkill, err := skills.ParsePersonalSkillMarkdown([]byte(req.Content))
71 if err != nil {
72 writeInvalidUserSkillContent(ctx, rw, err)
73 return
74 }
75
76 params := database.InsertUserSkillParams{
77 ID: uuid.New(),
78 UserID: user.ID,
79 Name: parsedSkill.Name,
80 Description: parsedSkill.Description,
81 Content: req.Content,
82 }
83 skill, err := api.Database.InsertUserSkill(ctx, params)
84 if err != nil {
85 if httpapi.IsUnauthorizedError(err) {
86 httpapi.Forbidden(rw)
87 return
88 }
89 if database.IsCheckViolation(err, userSkillUserDeletedConstraint) {
90 writeCannotCreateUserSkillForDeletedUser(ctx, rw)
91 return
92 }
93 if httpapi.Is404Error(err) {
94 httpapi.ResourceNotFound(rw)
95 return
96 }
97 if database.IsCheckViolation(err, userSkillsPerUserLimitConstraint) {
98 writeUserSkillLimitReached(ctx, rw)
99 return
100 }
101 if database.IsUniqueViolation(err, database.UniqueUserSkillsUserIDNameIndex) {
102 httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
103 Message: "A skill with that name already exists.",
104 Detail: err.Error(),
105 })
106 return

Callers

nothing calls this directly

Calls 15

UserParamFunction · 0.92
InitRequestFunction · 0.92
ReadFunction · 0.92
IsUnauthorizedErrorFunction · 0.92
ForbiddenFunction · 0.92
IsCheckViolationFunction · 0.92
Is404ErrorFunction · 0.92
ResourceNotFoundFunction · 0.92
IsUniqueViolationFunction · 0.92
WriteFunction · 0.92
InternalServerErrorFunction · 0.92

Tested by

no test coverage detected