parent
4cf140e529
commit
35b2afba37
@ -1,9 +0,0 @@
|
||||
package location
|
||||
|
||||
import "github.com/gorilla/mux"
|
||||
|
||||
func GetRouter() *mux.Router {
|
||||
router := mux.NewRouter().StrictSlash(true)
|
||||
router.HandleFunc("/", location).Methods("GET")
|
||||
return router
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
package location
|
||||
|
||||
import "net/http"
|
||||
|
||||
func location(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
addLocation(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func addLocation(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package locations
|
||||
|
||||
import (
|
||||
"github.com/dustinpianalto/quartermaster/internal/utils"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func GetRouter() *mux.Router {
|
||||
router := mux.NewRouter().StrictSlash(true)
|
||||
router.Handle("/", utils.RootHandler(locations))
|
||||
return router
|
||||
}
|
||||
@ -0,0 +1,86 @@
|
||||
package locations
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/dustinpianalto/errors"
|
||||
"github.com/dustinpianalto/quartermaster"
|
||||
"github.com/dustinpianalto/quartermaster/internal/utils"
|
||||
"github.com/dustinpianalto/quartermaster/pkg/services"
|
||||
)
|
||||
|
||||
func locations(w http.ResponseWriter, r *http.Request) error {
|
||||
const method = "locations"
|
||||
token, err := utils.IsAuthenticated(r)
|
||||
if err != nil {
|
||||
return errors.E(method, errors.Permission, err)
|
||||
}
|
||||
if !token.Valid {
|
||||
return errors.E(method, errors.Permission, "user not authenticated")
|
||||
}
|
||||
user, err := services.UserService.User(token.Claims.(*quartermaster.Claims).Username)
|
||||
if err != nil {
|
||||
return errors.E(method, errors.Permission, err)
|
||||
}
|
||||
if r.Method == "POST" {
|
||||
err = addLocation(w, r, user)
|
||||
if err != nil {
|
||||
return errors.E(method, "there was a problem adding the location", err)
|
||||
}
|
||||
} else if r.Method == "GET" {
|
||||
err = getTopLocations(w, r, user)
|
||||
if err != nil {
|
||||
return errors.E(method, "there was a problem getting locations", err)
|
||||
}
|
||||
} else {
|
||||
return errors.E(method, errors.Malformed, "http method not allowed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func addLocation(w http.ResponseWriter, r *http.Request, u *quartermaster.User) error {
|
||||
const method errors.Method = "locations/addLocation"
|
||||
var l *quartermaster.Location
|
||||
err := json.NewDecoder(r.Body).Decode(&l)
|
||||
if err != nil {
|
||||
return errors.E(method, errors.Malformed, "failed to decode location request", err)
|
||||
}
|
||||
if l.Name == "" || l.Description == "" {
|
||||
return errors.E(method, errors.Malformed, "name and description are required")
|
||||
}
|
||||
if l.Parent != nil {
|
||||
_, err := services.LocationService.Location(l.Parent.ID, u)
|
||||
if err != nil {
|
||||
return errors.E(method, errors.Malformed, "parent does not exist", err)
|
||||
}
|
||||
}
|
||||
l, err = services.LocationService.AddLocation(l, u)
|
||||
if err != nil {
|
||||
return errors.E(method, errors.Internal, err)
|
||||
}
|
||||
lJson, err := json.Marshal(l)
|
||||
if err != nil {
|
||||
return errors.E(method, errors.Internal, "error marshalling location", err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
w.Write(lJson)
|
||||
return nil
|
||||
}
|
||||
|
||||
func getTopLocations(w http.ResponseWriter, r *http.Request, u *quartermaster.User) error {
|
||||
const method = "locations/getTopLocations"
|
||||
locations, err := services.LocationService.GetTopLocations(u)
|
||||
if err != nil {
|
||||
return errors.E(method, errors.Internal, "error getting locations", err)
|
||||
}
|
||||
locationsJson, err := json.Marshal(locations)
|
||||
if err != nil {
|
||||
return errors.E(method, errors.Internal, "error marshalling locations", err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(locationsJson)
|
||||
return nil
|
||||
}
|
||||
Loading…
Reference in new issue