From 3130bbfb40d1938152cceaff33404e516228d88e Mon Sep 17 00:00:00 2001 From: agile6v Date: Sun, 14 Apr 2019 22:12:53 +0800 Subject: [PATCH] Support for search api in web server. --- pkg/server/web/api/api.go | 19 +++++++++++++++++++ pkg/server/web/controllers/app.go | 13 ++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/pkg/server/web/api/api.go b/pkg/server/web/api/api.go index 44af39c..59eb844 100644 --- a/pkg/server/web/api/api.go +++ b/pkg/server/web/api/api.go @@ -51,6 +51,11 @@ func (api *AppAPI) create(w http.ResponseWriter, r *http.Request) { return } + if task.Protocol == "" { + util.RespondWithError(w, http.StatusBadRequest, "Protocol field cannot be empty.") + return + } + err = task.Handle(body) if err != nil { util.RespondWithError(w, http.StatusInternalServerError, err.Error()) @@ -79,7 +84,21 @@ func (api *AppAPI) delete(w http.ResponseWriter, r *http.Request) { } func (api *AppAPI) search(w http.ResponseWriter, r *http.Request) { + // Read body + task := &controllers.GenericTask{} + _, err := util.ReadBody(r, task) + if err != nil { + util.RespondWithError(w, http.StatusInternalServerError, err.Error()) + return + } + + ret, err := task.Search() + if err != nil { + util.RespondWithError(w, http.StatusInternalServerError, err.Error()) + return + } + util.RespondWithJSON(w, http.StatusOK, ret) } func (api *AppAPI) list(w http.ResponseWriter, r *http.Request) { diff --git a/pkg/server/web/controllers/app.go b/pkg/server/web/controllers/app.go index d94e590..b3c3963 100644 --- a/pkg/server/web/controllers/app.go +++ b/pkg/server/web/controllers/app.go @@ -31,7 +31,14 @@ type CreateTask struct { } func (c *CreateTask) Handle(data string) error { - err := dao.CreateTask(data) + // check the validity of the data + createTask := &CreateTask{} + err := json.Unmarshal([]byte(data), createTask) + if err != nil { + return err + } + + err = dao.CreateTask(data) if err != nil { return err } @@ -147,8 +154,8 @@ func (g *GenericTask) Stop(masterAddr string) error { return nil } -func (task *GenericTask) Search() error { - return nil +func (g *GenericTask) Search() (*dao.Task, error) { + return dao.SearchTask(g.ID) } func ListTask() ([]dao.Task, error) {