From 65d254e58623abd10b16b7236e4fc3029db80da8 Mon Sep 17 00:00:00 2001 From: Umputun Date: Fri, 29 Dec 2017 17:15:35 -0600 Subject: [PATCH] add import rest --- README.md | 5 +++-- app/rest/admin.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c54e5a3c..64240ba1 100644 --- a/README.md +++ b/README.md @@ -75,8 +75,8 @@ type Comment struct { } type Locator struct { - SiteID string `json:"site"` - URL string `json:"url"` + SiteID string `json:"site"` // site id + URL string `json:"url"` // post url } ``` @@ -110,4 +110,5 @@ Sort can be `time` or `score`. Supported sort order with prefix -/+, i.e. `-time - `DELETE /api/v1/admin/comment/{id}?site=site-id&url=post-url` - delete comment by `id`. _auth and admin required_ - `PUT /api/v1/admin/user/{userid}?site=site-id&block=1` - block or unblock user. _auth and admin required_ - `GET /api/v1/admin/export?site=side-id&mode=[stream|file]` - export all comments to json stream or gz file. _auth and admin required_ +- `POST /api/v1/admin/import?site=side-id` - import comments from the backup. _auth and admin required_ - `PUT /api/v1/admin/pin/{id}?site=site-id&url=post-url&pin=1` - pin or unpin comment. _auth and admin required_ diff --git a/app/rest/admin.go b/app/rest/admin.go index a6879133..5eaa3e1e 100644 --- a/app/rest/admin.go +++ b/app/rest/admin.go @@ -21,6 +21,7 @@ import ( type admin struct { dataService store.Service exporter migrator.Exporter + importer migrator.Importer respCache *cache.Cache } @@ -30,6 +31,7 @@ func (a *admin) routes() chi.Router { router.Delete("/comment/{id}", a.deleteCommentCtrl) router.Put("/user/{userid}", a.setBlockCtrl) router.Get("/export", a.exportCtrl) + router.Post("/import", a.importCtrl) router.Put("/pin/{id}", a.setPinCtrl) return router } @@ -96,6 +98,15 @@ func (a *admin) exportCtrl(w http.ResponseWriter, r *http.Request) { httpError(w, r, http.StatusInternalServerError, err, "export failed") } } + +// POST /import?site=site-id +func (a *admin) importCtrl(w http.ResponseWriter, r *http.Request) { + siteID := r.URL.Query().Get("site") + if err := a.importer.Import(r.Body, siteID); err != nil { + httpError(w, r, http.StatusBadRequest, err, "import failed") + } +} + func (a *admin) checkBlocked(locator store.Locator, user store.User) bool { return a.dataService.IsBlocked(locator, user.ID) }