statping/handlers/groups.go

134 lines
3.2 KiB
Go
Raw Normal View History

2018-12-31 21:36:58 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package handlers
import (
"github.com/gorilla/mux"
2020-03-04 10:29:00 +00:00
"github.com/pkg/errors"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/groups"
"github.com/statping/statping/utils"
2018-12-31 21:36:58 +00:00
"net/http"
)
2020-03-04 10:29:00 +00:00
func selectGroup(r *http.Request) (*groups.Group, error) {
vars := mux.Vars(r)
id := utils.ToInt(vars["id"])
2020-03-08 18:13:27 +00:00
g, err := groups.Find(id)
if err != nil {
return nil, err
}
return g, nil
2020-03-04 10:29:00 +00:00
}
2019-01-03 21:09:11 +00:00
// apiAllGroupHandler will show all the groups
2020-01-20 05:02:15 +00:00
func apiAllGroupHandler(r *http.Request) interface{} {
auth, admin := IsUser(r), IsAdmin(r)
2020-03-04 10:29:00 +00:00
return groups.SelectGroups(admin, auth)
2020-02-26 05:38:03 +00:00
}
2019-01-03 21:09:11 +00:00
// apiGroupHandler will show a single group
2018-12-31 21:36:58 +00:00
func apiGroupHandler(w http.ResponseWriter, r *http.Request) {
2020-03-04 10:29:00 +00:00
group, err := selectGroup(r)
if err != nil {
2020-03-10 05:24:35 +00:00
sendErrorJson(errors.Wrap(err, "group not found"), w, r, http.StatusNotFound)
2018-12-31 21:36:58 +00:00
return
}
returnJson(group, w, r)
2018-12-31 21:36:58 +00:00
}
2019-03-22 05:34:52 +00:00
// apiGroupUpdateHandler will update a group
func apiGroupUpdateHandler(w http.ResponseWriter, r *http.Request) {
2020-03-04 10:29:00 +00:00
group, err := selectGroup(r)
if err != nil {
2020-03-08 21:32:26 +00:00
w.WriteHeader(http.StatusNotFound)
2020-03-04 10:29:00 +00:00
sendErrorJson(errors.Wrap(err, "group not found"), w, r)
2019-03-22 05:34:52 +00:00
return
}
2020-03-04 10:29:00 +00:00
if err := DecodeJSON(r, &group); err != nil {
sendErrorJson(err, w, r)
return
}
if err := group.Update(); err != nil {
2019-03-22 05:34:52 +00:00
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
2019-03-22 05:34:52 +00:00
sendJsonAction(group, "update", w, r)
}
2019-01-03 21:09:11 +00:00
// apiCreateGroupHandler accepts a POST method to create new groups
2018-12-31 21:36:58 +00:00
func apiCreateGroupHandler(w http.ResponseWriter, r *http.Request) {
2020-03-04 10:29:00 +00:00
var group *groups.Group
if err := DecodeJSON(r, &group); err != nil {
2018-12-31 21:36:58 +00:00
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
if err := group.Create(); err != nil {
2018-12-31 21:36:58 +00:00
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
2018-12-31 21:36:58 +00:00
sendJsonAction(group, "create", w, r)
}
2019-01-03 21:09:11 +00:00
// apiGroupDeleteHandler accepts a DELETE method to delete groups
2018-12-31 21:36:58 +00:00
func apiGroupDeleteHandler(w http.ResponseWriter, r *http.Request) {
2020-03-04 10:29:00 +00:00
group, err := selectGroup(r)
if err != nil {
sendErrorJson(errors.Wrap(err, "group not found"), w, r)
2018-12-31 21:36:58 +00:00
return
}
2020-03-04 10:29:00 +00:00
if err := group.Delete(); err != nil {
2018-12-31 21:36:58 +00:00
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
2018-12-31 21:36:58 +00:00
sendJsonAction(group, "delete", w, r)
}
2019-02-20 02:11:40 +00:00
type groupOrder struct {
Id int64 `json:"group"`
Order int `json:"order"`
}
func apiGroupReorderHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var newOrder []*groupOrder
2020-03-04 10:29:00 +00:00
if err := DecodeJSON(r, &newOrder); err != nil {
sendErrorJson(err, w, r)
return
}
2019-02-20 02:11:40 +00:00
for _, g := range newOrder {
2020-03-04 10:29:00 +00:00
group, err := groups.Find(g.Id)
if err != nil {
sendErrorJson(err, w, r)
return
}
2019-02-20 02:11:40 +00:00
group.Order = g.Order
2020-03-04 10:29:00 +00:00
if err := group.Update(); err != nil {
sendErrorJson(err, w, r)
return
}
2019-02-20 02:11:40 +00:00
}
returnJson(newOrder, w, r)
2019-02-20 02:11:40 +00:00
}