diff --git a/db/migrations/20240819195341_ery_extension_cascade.sql b/db/migrations/20240819195341_ery_extension_cascade.sql new file mode 100644 index 0000000..b5bface --- /dev/null +++ b/db/migrations/20240819195341_ery_extension_cascade.sql @@ -0,0 +1,14 @@ +-- migrate:up +ALTER TABLE ery_extension DROP CONSTRAINT ery_extension_endpoint_id_fkey; + +ALTER TABLE + ery_extension +ADD CONSTRAINT + ry_extension_endpoint_id_fkey +FOREIGN KEY + (endpoint_id) +REFERENCES + ps_endpoints(sid) +ON DELETE CASCADE; + +-- migrate:down diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 11d0e48..b6dc4bc 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -165,14 +165,14 @@ paths: summary: Create a new endpoint. tags: - endpoints - /endpoints/{id}: + /endpoints/{sid}: delete: parameters: - - description: ID of the endpoint to be deleted + - description: Sid of the endpoint to be deleted in: path - name: id + name: sid required: true - type: string + type: integer responses: "204": description: No Content @@ -183,7 +183,6 @@ paths: summary: Delete an endpoint and its associated resources. tags: - endpoints - /endpoints/{sid}: get: parameters: - description: Requested endpoint's sid diff --git a/internal/handler/endpoint.go b/internal/handler/endpoint.go index 9cf0384..3e286f6 100644 --- a/internal/handler/endpoint.go +++ b/internal/handler/endpoint.go @@ -63,7 +63,7 @@ func (e *Endpoint) Router() chi.Router { r.Post("/", e.create) r.Get("/", e.list) r.Get("/{sid}", e.get) - r.Delete("/{id}", e.delete) + r.Delete("/{sid}", e.delete) return r } @@ -348,15 +348,16 @@ func (e *Endpoint) create(w http.ResponseWriter, r *http.Request) { } // @Summary Delete an endpoint and its associated resources. -// @Param id path string true "ID of the endpoint to be deleted" +// @Param sid path int true "Sid of the endpoint to be deleted" // @Success 204 // @Failure 400 // @Failure 500 // @Tags endpoints -// @Router /endpoints/{id} [delete] +// @Router /endpoints/{sid} [delete] func (e *Endpoint) delete(w http.ResponseWriter, r *http.Request) { - id := chi.URLParam(r, "id") - if id == "" { + urlSid := chi.URLParam(r, "sid") + sid, err := strconv.Atoi(urlSid) + if err != nil || sid <= 0 { w.WriteHeader(http.StatusBadRequest) return } @@ -370,7 +371,7 @@ func (e *Endpoint) delete(w http.ResponseWriter, r *http.Request) { queries := sqlc.New(tx) - err = queries.DeleteEndpoint(r.Context(), id) + id, err := queries.DeleteEndpoint(r.Context(), int32(sid)) if err != nil { w.WriteHeader(http.StatusInternalServerError) return diff --git a/internal/sqlc/queries.sql.go b/internal/sqlc/queries.sql.go index 95da9b7..d5ccc69 100644 --- a/internal/sqlc/queries.sql.go +++ b/internal/sqlc/queries.sql.go @@ -40,13 +40,15 @@ func (q *Queries) DeleteAuth(ctx context.Context, id string) error { return err } -const deleteEndpoint = `-- name: DeleteEndpoint :exec -DELETE FROM ps_endpoints WHERE id = $1 +const deleteEndpoint = `-- name: DeleteEndpoint :one +DELETE FROM ps_endpoints WHERE sid = $1 RETURNING id ` -func (q *Queries) DeleteEndpoint(ctx context.Context, id string) error { - _, err := q.db.Exec(ctx, deleteEndpoint, id) - return err +func (q *Queries) DeleteEndpoint(ctx context.Context, sid int32) (string, error) { + row := q.db.QueryRow(ctx, deleteEndpoint, sid) + var id string + err := row.Scan(&id) + return id, err } const getEndpointByExtension = `-- name: GetEndpointByExtension :one diff --git a/queries.sql b/queries.sql index 44c9c68..6f48293 100644 --- a/queries.sql +++ b/queries.sql @@ -17,8 +17,8 @@ VALUES ($1, $2, $1, $1, $3, 'all', $4, $5) RETURNING sid; --- name: DeleteEndpoint :exec -DELETE FROM ps_endpoints WHERE id = $1; +-- name: DeleteEndpoint :one +DELETE FROM ps_endpoints WHERE sid = $1 RETURNING id; -- name: DeleteAOR :exec DELETE FROM ps_aors WHERE id = $1;