diff --git a/internal/api/mount_dns_routes.go b/internal/api/mount_dns_routes.go index 0ecb56e..1a81a6b 100644 --- a/internal/api/mount_dns_routes.go +++ b/internal/api/mount_dns_routes.go @@ -20,6 +20,7 @@ func mountDNSRoutes(r chi.Router, db *gorm.DB, authOrg func(http.Handler) http.H d.Get("/domains/{domain_id}/records", handlers.ListRecordSets(db)) d.Post("/domains/{domain_id}/records", handlers.CreateRecordSet(db)) + d.Get("/records/{id}", handlers.GetRecordSet(db)) d.Patch("/records/{id}", handlers.UpdateRecordSet(db)) d.Delete("/records/{id}", handlers.DeleteRecordSet(db)) }) diff --git a/internal/handlers/dns.go b/internal/handlers/dns.go index 2528ee6..2c39bfc 100644 --- a/internal/handlers/dns.go +++ b/internal/handlers/dns.go @@ -503,6 +503,51 @@ func ListRecordSets(db *gorm.DB) http.HandlerFunc { } } + +// GetRecordSet godoc +// +// @ID GetRecordSet +// @Summary Get a record set (org scoped) +// @Tags DNS +// @Produce json +// @Param X-Org-ID header string false "Organization UUID" +// @Param id path string true "Record Set ID (UUID)" +// @Success 200 {object} dto.RecordSetResponse +// @Failure 403 {string} string "organization required" +// @Failure 404 {string} string "not found" +// @Router /dns/records/{id} [get] +func GetRecordSet(db *gorm.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + orgID, ok := httpmiddleware.OrgIDFrom(r.Context()) + if !ok { + utils.WriteError(w, http.StatusForbidden, "org_required", "specify X-Org-ID") + return + } + + id, err := uuid.Parse(chi.URLParam(r, "id")) + if err != nil { + utils.WriteError(w, http.StatusBadRequest, "bad_id", "invalid UUID") + return + } + + var row models.RecordSet + if err := db. + Joins("Domain"). + Where(`record_sets.id = ? AND "Domain"."organization_id" = ?`, id, orgID). + First(&row).Error; err != nil { + + if errors.Is(err, gorm.ErrRecordNotFound) { + utils.WriteError(w, http.StatusNotFound, "not_found", "record set not found") + return + } + utils.WriteError(w, http.StatusInternalServerError, "db_error", err.Error()) + return + } + + utils.WriteJSON(w, http.StatusOK, recordOut(&row)) + } +} + // CreateRecordSet godoc // // @ID CreateRecordSet