From 729a4e3fa0448bd452df38460e13953f85670236 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Tue, 23 Jun 2020 22:12:14 +0200 Subject: [PATCH] admin: add clear routes --- admin/db-domain.go | 21 ++++++++++++++++++--- admin/db-source.go | 6 ++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/admin/db-domain.go b/admin/db-domain.go index 2534caa..d977bb0 100644 --- a/admin/db-domain.go +++ b/admin/db-domain.go @@ -52,7 +52,9 @@ func init() { router.GET("/api/users/:userid/domains/:domain", api.ApiHandler(userHandler(domainHandler(getUserDomain)))) router.PUT("/api/users/:userid/domains/:domain", api.ApiHandler(userHandler(domainHandler(updateUserDomain)))) - router.DELETE("/api/users/:userid/domains/:domain", api.ApiHandler(userHandler(domainHandler(deleteUserDomain)))) + router.DELETE("/api/users/:userid/domains/:domain", api.ApiHandler(userHandler(deleteUserDomain))) + + router.DELETE("/api/domains", api.ApiHandler(clearDomains)) } func getUserDomains(_ *config.Options, user *happydns.User, _ httprouter.Params, _ io.Reader) api.Response { @@ -113,6 +115,19 @@ func updateUserDomain(_ *config.Options, domain *happydns.Domain, _ httprouter.P return api.NewAPIResponse(ud, storage.MainStore.UpdateDomain(ud)) } -func deleteUserDomain(_ *config.Options, domain *happydns.Domain, _ httprouter.Params, _ io.Reader) api.Response { - return api.NewAPIResponse(true, storage.MainStore.DeleteDomain(domain)) +func deleteUserDomain(_ *config.Options, user *happydns.User, ps httprouter.Params, _ io.Reader) api.Response { + domainid, err := strconv.ParseInt(ps.ByName("domain"), 10, 64) + if err != nil { + domain, err := storage.MainStore.GetDomainByDN(user, ps.ByName("domain")) + if err != nil { + return api.NewAPIErrorResponse(http.StatusNotFound, err) + } else { + domainid = domain.Id + } + } + return api.NewAPIResponse(true, storage.MainStore.DeleteDomain(&happydns.Domain{Id: domainid})) +} + +func clearDomains(_ *config.Options, _ httprouter.Params, _ io.Reader) api.Response { + return api.NewAPIResponse(true, storage.MainStore.ClearDomains()) } diff --git a/admin/db-source.go b/admin/db-source.go index 969c35e..a8808b2 100644 --- a/admin/db-source.go +++ b/admin/db-source.go @@ -52,6 +52,8 @@ func init() { router.GET("/api/users/:userid/sources/:source", api.ApiHandler(userHandler(sourceHandler(getUserSource)))) router.PUT("/api/users/:userid/sources/:source", api.ApiHandler(userHandler(sourceHandler(updateUserSource)))) router.DELETE("/api/users/:userid/sources/:source", api.ApiHandler(userHandler(sourceHandler(deleteUserSource)))) + + router.DELETE("/api/sources", api.ApiHandler(clearSources)) } func getUserSources(_ *config.Options, user *happydns.User, _ httprouter.Params, _ io.Reader) api.Response { @@ -101,3 +103,7 @@ func updateUserSource(_ *config.Options, source *happydns.SourceCombined, _ http func deleteUserSource(_ *config.Options, source *happydns.SourceCombined, _ httprouter.Params, _ io.Reader) api.Response { return api.NewAPIResponse(true, storage.MainStore.DeleteSource(&source.SourceType)) } + +func clearSources(_ *config.Options, _ httprouter.Params, _ io.Reader) api.Response { + return api.NewAPIResponse(true, storage.MainStore.ClearSources()) +}