Skip to content

Commit

Permalink
feat(endpoint): return endpoint on creation
Browse files Browse the repository at this point in the history
Besides this being customary, it saves consumers a roundtrip if they
want to verify the state of the created resource.
  • Loading branch information
crazybolillo committed Aug 19, 2024
1 parent af4659b commit fa3058b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
8 changes: 6 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ definitions:
type: string
maxContacts:
type: integer
sid:
type: integer
transport:
type: string
type: object
Expand Down Expand Up @@ -152,8 +154,10 @@ paths:
schema:
$ref: '#/definitions/handler.createEndpointRequest'
responses:
"204":
description: No Content
"201":
description: Created
schema:
$ref: '#/definitions/handler.getEndpointResponse'
"400":
description: Bad Request
"500":
Expand Down
46 changes: 44 additions & 2 deletions internal/handler/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type listEndpointsResponse struct {
}

type getEndpointResponse struct {
Sid int32 `json:"sid"`
ID string `json:"id"`
DisplayName string `json:"displayName"`
Transport string `json:"transport"`
Expand Down Expand Up @@ -128,6 +129,7 @@ func (e *Endpoint) get(w http.ResponseWriter, r *http.Request) {
}

endpoint := getEndpointResponse{
Sid: int32(id),
ID: row.ID,
Transport: row.Transport.String,
Context: row.Context.String,
Expand Down Expand Up @@ -220,7 +222,7 @@ func (e *Endpoint) list(w http.ResponseWriter, r *http.Request) {
// @Summary Create a new endpoint.
// @Accept json
// @Param payload body createEndpointRequest true "Endpoint's information"
// @Success 204
// @Success 201 {object} getEndpointResponse
// @Failure 400
// @Failure 500
// @Tags endpoints
Expand Down Expand Up @@ -297,7 +299,47 @@ func (e *Endpoint) create(w http.ResponseWriter, r *http.Request) {
return
}

w.WriteHeader(http.StatusNoContent)
// TODO: Duplicate code, same as when fetching endpoint. Probably should put this into a service layer.
tx, err = e.Begin(r.Context())
queries = sqlc.New(tx)
if err != nil {
slog.Error("Failed to create new transaction", slog.String("path", r.URL.Path), slog.String("reason", err.Error()))
w.WriteHeader(http.StatusInternalServerError)
return
}
res, err := queries.GetEndpointByID(r.Context(), sid)
if err != nil {
slog.Error(
"Failed to retrieve created endpoint",
slog.String("path", r.URL.Path), slog.String("reason", err.Error()), slog.Int("sid", int(sid)),
)
w.WriteHeader(http.StatusInternalServerError)
return
}

endpoint := getEndpointResponse{
Sid: sid,
ID: res.ID,
Transport: res.Transport.String,
Context: res.Context.String,
Codecs: strings.Split(res.Allow.String, ","),
MaxContacts: res.MaxContacts.Int32,
Extension: res.Extension.String,
DisplayName: displayNameFromClid(res.Callerid.String),
}
content, err := json.Marshal(endpoint)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
slog.Error("Failed to marshall response", slog.String("path", r.URL.Path))
return
}

w.Header().Set("Content-Type", "application/json")
_, err = w.Write(content)
if err != nil {
slog.Error("Failed to write response", slog.String("path", r.URL.Path), slog.String("reason", err.Error()))
}
w.WriteHeader(http.StatusCreated)
}

// @Summary Delete an endpoint and its associated resources.
Expand Down

0 comments on commit fa3058b

Please sign in to comment.