From 0e1fe9eb0ef4a9775eaa402b10cf1a8e2cc512af Mon Sep 17 00:00:00 2001 From: William Woodson Date: Sat, 20 Feb 2016 17:16:46 -0600 Subject: [PATCH] Created API endpoint smtp for storing SMTP server config --- controllers/api.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/controllers/api.go b/controllers/api.go index 8c009f11..2d3784d1 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -350,6 +350,88 @@ func API_Pages_Id(w http.ResponseWriter, r *http.Request) { } } +// API_SMTP handles requests for the /api/smtp/ endpoint +func API_SMTP(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == "GET": + ps, err := models.GetPages(ctx.Get(r, "user_id").(int64)) + if err != nil { + fmt.Println(err) + } + JSONResponse(w, ps, http.StatusOK) + //POST: Create a new page and return it as JSON + case r.Method == "POST": + p := models.Page{} + // Put the request into a page + err := json.NewDecoder(r.Body).Decode(&p) + if err != nil { + JSONResponse(w, models.Response{Success: false, Message: "Invalid request"}, http.StatusBadRequest) + return + } + // Check to make sure the name is unique + _, err = models.GetPageByName(p.Name, ctx.Get(r, "user_id").(int64)) + if err != gorm.RecordNotFound { + JSONResponse(w, models.Response{Success: false, Message: "Page name already in use"}, http.StatusConflict) + Logger.Println(err) + return + } + p.ModifiedDate = time.Now() + p.UserId = ctx.Get(r, "user_id").(int64) + err = models.PostPage(&p) + if err != nil { + JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError) + return + } + JSONResponse(w, p, http.StatusCreated) + } +} + +// API_SMTP_Id contains functions to handle the GET'ing, DELETE'ing, and PUT'ing +// of a SMTP object +func API_SMTP_Id(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + id, _ := strconv.ParseInt(vars["id"], 0, 64) + p, err := models.GetPage(id, ctx.Get(r, "user_id").(int64)) + if err != nil { + JSONResponse(w, models.Response{Success: false, Message: "Page not found"}, http.StatusNotFound) + return + } + switch { + case r.Method == "GET": + JSONResponse(w, p, http.StatusOK) + case r.Method == "DELETE": + err = models.DeletePage(id, ctx.Get(r, "user_id").(int64)) + if err != nil { + JSONResponse(w, models.Response{Success: false, Message: "Error deleting page"}, http.StatusInternalServerError) + return + } + JSONResponse(w, models.Response{Success: true, Message: "Page Deleted Successfully"}, http.StatusOK) + case r.Method == "PUT": + p = models.Page{} + err = json.NewDecoder(r.Body).Decode(&p) + if err != nil { + Logger.Println(err) + } + if p.Id != id { + JSONResponse(w, models.Response{Success: false, Message: "/:id and /:page_id mismatch"}, http.StatusBadRequest) + return + } + err = p.Validate() + if err != nil { + JSONResponse(w, models.Response{Success: false, Message: "Invalid attributes given"}, http.StatusBadRequest) + return + } + p.ModifiedDate = time.Now() + p.UserId = ctx.Get(r, "user_id").(int64) + err = models.PutPage(&p) + if err != nil { + JSONResponse(w, models.Response{Success: false, Message: "Error updating page"}, http.StatusInternalServerError) + return + } + JSONResponse(w, p, http.StatusOK) + } +} + // API_Import_Group imports a CSV of group members func API_Import_Group(w http.ResponseWriter, r *http.Request) { ts, err := util.ParseCSV(r)