You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
statping/handlers/messages.go

92 lines
2.4 KiB

// Statping
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/statping/statping
//
// 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 (
"fmt"
"github.com/gorilla/mux"
"github.com/statping/statping/types/messages"
"github.com/statping/statping/utils"
"net/http"
)
func getMessageByID(r *http.Request) (*messages.Message, int64, error) {
vars := mux.Vars(r)
num := utils.ToInt(vars["id"])
message, err := messages.Find(num)
if err != nil {
return nil, num, err
}
return message, num, nil
}
func apiAllMessagesHandler(r *http.Request) interface{} {
msgs := messages.All()
return msgs
}
func apiMessageCreateHandler(w http.ResponseWriter, r *http.Request) {
var message *messages.Message
if err := DecodeJSON(r, &message); err != nil {
sendErrorJson(err, w, r)
return
}
if err := message.Create(); err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(message, "create", w, r)
}
func apiMessageGetHandler(r *http.Request) interface{} {
message, id, err := getMessageByID(r)
if err != nil {
return fmt.Errorf("message #%d was not found", id)
}
return message
}
func apiMessageDeleteHandler(w http.ResponseWriter, r *http.Request) {
message, id, err := getMessageByID(r)
if err != nil {
sendErrorJson(fmt.Errorf("message #%d was not found", id), w, r)
return
}
err = message.Delete()
if err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(message, "delete", w, r)
}
func apiMessageUpdateHandler(w http.ResponseWriter, r *http.Request) {
message, id, err := getMessageByID(r)
if err != nil {
sendErrorJson(fmt.Errorf("message #%d was not found", id), w, r)
return
}
if err := DecodeJSON(r, &message); err != nil {
sendErrorJson(err, w, r)
return
}
if err := message.Update(); err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(message, "update", w, r)
}