Skip to content

Commit

Permalink
feat(endpoint): support delete
Browse files Browse the repository at this point in the history
Endpoints may now be deleted from the API as well.
  • Loading branch information
crazybolillo committed Jul 25, 2024
1 parent e6924c0 commit 267d7f6
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,24 @@ paths:
"500":
description: Internal Server Error
summary: Create a new endpoint.
tags:
- endpoints
/endpoint/{id}:
delete:
parameters:
- description: ID of the endpoint to be deleted
in: path
name: id
required: true
type: string
responses:
"204":
description: No Content
"400":
description: Bad Request
"500":
description: Internal Server Error
summary: Delete an endpoint and its associated resources.
tags:
- endpoints
swagger: "2.0"
52 changes: 52 additions & 0 deletions internal/handler/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type createEndpointRequest struct {
func (e *Endpoint) Router() chi.Router {
r := chi.NewRouter()
r.Post("/", e.create)
r.Delete("/{id}", e.delete)

return r
}
Expand All @@ -39,6 +40,7 @@ func (e *Endpoint) Router() chi.Router {
// @Success 204
// @Failure 400
// @Failure 500
// @Tags endpoints
// @Router /endpoint [post]
func (e *Endpoint) create(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
Expand Down Expand Up @@ -102,3 +104,53 @@ func (e *Endpoint) create(w http.ResponseWriter, r *http.Request) {

w.WriteHeader(http.StatusNoContent)
}

// @Summary Delete an endpoint and its associated resources.
// @Param id path string true "ID of the endpoint to be deleted"
// @Success 204
// @Failure 400
// @Failure 500
// @Tags endpoints
// @Router /endpoint/{id} [delete]
func (e *Endpoint) delete(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
if id == "" {
w.WriteHeader(http.StatusBadRequest)
return
}

tx, err := e.Begin(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer tx.Rollback(r.Context())

queries := sqlc.New(tx)

err = queries.DeleteEndpoint(r.Context(), id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

err = queries.DeleteAOR(r.Context(), id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

err = queries.DeleteAuth(r.Context(), id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

err = tx.Commit(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusNoContent)
}
27 changes: 27 additions & 0 deletions internal/sqlc/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ INSERT INTO ps_endpoints
(id, transport, aors, auth, context, disallow, allow)
VALUES
($1, $2, $1, $1, $3, 'all', $4);

-- name: DeleteEndpoint :exec
DELETE FROM ps_endpoints WHERE id = $1;

-- name: DeleteAOR :exec
DELETE FROM ps_aors WHERE id = $1;

-- name: DeleteAuth :exec
DELETE FROM ps_auths WHERE id = $1;

0 comments on commit 267d7f6

Please sign in to comment.