Ejemplo n.º 1
0
func UsersCreate(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	var user User

	body, err := readBody(r)
	if err != nil {
		error_handler.NewError(422, errors.New("Unable to parse parameters")).Handle(w)
		return
	}

	if err := json.Unmarshal(body, &user); err != nil {
		error_handler.NewError(422, err).Handle(w)
	}

	err = user.generatePassword()
	if err != nil {
		error_handler.NewError(422, err).Handle(w)
	}

	err = db.Create(&user)
	if err != nil {
		error_handler.NewError(404, err).Handle(w)
		return
	}

	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(user); err != nil {
		panic(err)
	}
}
Ejemplo n.º 2
0
func MonitorsCreate(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	var monitor Monitor

	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		error_handler.NewError(422, errors.New("Unable to parse parameters")).Handle(w)
		return
	}
	if err := r.Body.Close(); err != nil {
		error_handler.NewError(422, errors.New("Unable to parse parameters")).Handle(w)
		return
	}

	if err := json.Unmarshal(body, &monitor); err != nil {
		error_handler.NewError(422, err).Handle(w)
		return
	}

	err = db.Create(&monitor)
	if err != nil {
		error_handler.NewError(400, err).Handle(w)
		return
	}

	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(monitor); err != nil {
		panic(err)
	}
}
Ejemplo n.º 3
0
func MonitorsDelete(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	vars := mux.Vars(r)
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		error_handler.NewError(400, errors.New("id is not an integer")).Handle(w)
		return
	}

	err = db.Destroy(id)
	if err != nil {
		error_handler.NewError(404, err).Handle(w)
		return
	}

	w.WriteHeader(http.StatusOK)
}
Ejemplo n.º 4
0
func MonitorsShow(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	vars := mux.Vars(r)
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		error_handler.NewError(400, errors.New("id is not an integer")).Handle(w)
		return
	}

	monitor, err := db.Find(id)
	if err != nil {
		error_handler.NewError(404, err).Handle(w)
		return
	}

	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(monitor); err != nil {
		panic(err)
	}
}
Ejemplo n.º 5
0
func MonitorsIndex(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	monitors, err := db.All()
	if err != nil {
		error_handler.NewError(400, err)
		return
	}

	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(monitors); err != nil {
		panic(err)
	}
}
Ejemplo n.º 6
0
func MonitorsUpdate(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	vars := mux.Vars(r)
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		error_handler.NewError(400, errors.New("id is not an integer")).Handle(w)
		return
	}

	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		error_handler.NewError(422, errors.New("Unable to parse parameters")).Handle(w)
		return
	}
	if err = r.Body.Close(); err != nil {
		error_handler.NewError(422, errors.New("Unable to parse parameters")).Handle(w)
		return
	}

	monitor, err := db.Find(id)
	if err != nil {
		error_handler.NewError(404, err).Handle(w)
		return
	}

	if err = json.Unmarshal(body, &monitor); err != nil {
		error_handler.NewError(422, err).Handle(w)
		return
	}

	err = db.Update(id, &monitor)
	if err != nil {
		error_handler.NewError(404, err).Handle(w)
		return
	}

	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(monitor); err != nil {
		panic(err)
	}
}