MCPcopy Index your code
hub / github.com/NdoleStudio/httpsms / Upsert

Method Upsert

api/pkg/handlers/phone_handler.go:112–139  ·  view source on GitHub ↗

Upsert a phone @Summary Upsert Phone @Description Updates properties of a user's phone. If the phone with this number does not exist, a new one will be created. Think of this method like an 'upsert' @Security ApiKeyAuth @Tags Phones @Accept json @Produce json @Param

(c fiber.Ctx)

Source from the content-addressed store, hash-verified

110// @Failure 500 {object} responses.InternalServerError
111// @Router /phones [put]
112func (h *PhoneHandler) Upsert(c fiber.Ctx) error {
113 ctx, span := h.tracer.StartFromFiberCtx(c)
114 defer span.End()
115
116 ctxLogger := h.tracer.CtxLogger(h.logger, span)
117
118 var request requests.PhoneUpsert
119 if err := c.Bind().Body(&request); err != nil {
120 msg := fmt.Sprintf("cannot marshall params [%s] into %T", c.OriginalURL(), request)
121 ctxLogger.Warn(stacktrace.Propagate(err, msg))
122 return h.responseBadRequest(c, err)
123 }
124
125 if errors := h.validator.ValidateUpsert(ctx, h.userIDFomContext(c), request.Sanitize()); len(errors) != 0 {
126 msg := fmt.Sprintf("validation errors [%s], while updating phones [%+#v]", spew.Sdump(errors), request)
127 ctxLogger.Warn(stacktrace.NewError(msg))
128 return h.responseUnprocessableEntity(c, errors, "validation errors while updating phones")
129 }
130
131 phone, err := h.service.Upsert(ctx, request.ToUpsertParams(h.userFromContext(c), c.OriginalURL(), c.Body()))
132 if err != nil {
133 msg := fmt.Sprintf("cannot update phones with params [%+#v]", request)
134 ctxLogger.Error(stacktrace.Propagate(err, msg))
135 return h.responseInternalServerError(c)
136 }
137
138 return h.responseOK(c, "phone updated successfully", phone)
139}
140
141// Delete a phone
142// @Summary Delete Phone

Callers

nothing calls this directly

Calls 13

SanitizeMethod · 0.95
ToUpsertParamsMethod · 0.95
responseBadRequestMethod · 0.80
ValidateUpsertMethod · 0.80
userIDFomContextMethod · 0.80
userFromContextMethod · 0.80
responseOKMethod · 0.80
StartFromFiberCtxMethod · 0.65
CtxLoggerMethod · 0.65
WarnMethod · 0.65

Tested by

no test coverage detected