diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 665978d..a69400e 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -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" diff --git a/internal/handler/endpoint.go b/internal/handler/endpoint.go index 254f564..376a089 100644 --- a/internal/handler/endpoint.go +++ b/internal/handler/endpoint.go @@ -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 } @@ -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) @@ -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) +} diff --git a/internal/sqlc/queries.sql.go b/internal/sqlc/queries.sql.go index ac161a8..04c5161 100644 --- a/internal/sqlc/queries.sql.go +++ b/internal/sqlc/queries.sql.go @@ -11,6 +11,33 @@ import ( "github.com/jackc/pgx/v5/pgtype" ) +const deleteAOR = `-- name: DeleteAOR :exec +DELETE FROM ps_aors WHERE id = $1 +` + +func (q *Queries) DeleteAOR(ctx context.Context, id string) error { + _, err := q.db.Exec(ctx, deleteAOR, id) + return err +} + +const deleteAuth = `-- name: DeleteAuth :exec +DELETE FROM ps_auths WHERE id = $1 +` + +func (q *Queries) DeleteAuth(ctx context.Context, id string) error { + _, err := q.db.Exec(ctx, deleteAuth, id) + return err +} + +const deleteEndpoint = `-- name: DeleteEndpoint :exec +DELETE FROM ps_endpoints WHERE id = $1 +` + +func (q *Queries) DeleteEndpoint(ctx context.Context, id string) error { + _, err := q.db.Exec(ctx, deleteEndpoint, id) + return err +} + const newAOR = `-- name: NewAOR :exec INSERT INTO ps_aors (id, max_contacts) diff --git a/queries.sql b/queries.sql index 2c13bff..cb240a7 100644 --- a/queries.sql +++ b/queries.sql @@ -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;