197 lines
4.7 KiB
Go
197 lines
4.7 KiB
Go
/**
|
|
* file: api/v0/productTag.go
|
|
* author: Theo Technicguy
|
|
* license: Apache 2.0
|
|
*
|
|
* Product tags API endpoint
|
|
*/
|
|
|
|
package apiv0
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"git.licolas.net/hoffman/server/types"
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
func getAllTags(w http.ResponseWriter, r *http.Request) {
|
|
logger.Trace().Msg("getting all tags")
|
|
tags, err := lgc.GetAllProductTags()
|
|
|
|
if err != nil {
|
|
respondWithError(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
respond(w, http.StatusOK, tags)
|
|
}
|
|
|
|
func getTagById(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseUint(mux.Vars(r)[idQry], 10, 64)
|
|
|
|
if err != nil {
|
|
respondWithError(w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
logger.Trace().Uint("tag", uint(id)).Msg("getting tag by id")
|
|
|
|
tag, err := lgc.GetProductTagByID(uint(id))
|
|
if err != nil {
|
|
var status int
|
|
if err == types.ErrIDNotFound {
|
|
status = http.StatusNotFound
|
|
} else {
|
|
status = http.StatusInternalServerError
|
|
}
|
|
|
|
respondWithError(w, status, err)
|
|
return
|
|
}
|
|
|
|
respond(w, http.StatusOK, tag)
|
|
}
|
|
|
|
func getTagByName(w http.ResponseWriter, r *http.Request) {
|
|
name := mux.Vars(r)["name"]
|
|
logger.Trace().Str("name", name).Msg("getting tag by name")
|
|
|
|
tag, err := lgc.GetProductTagByName(name)
|
|
if err != nil {
|
|
var status int
|
|
if err == types.ErrIDNotFound {
|
|
status = http.StatusNotFound
|
|
} else {
|
|
status = http.StatusInternalServerError
|
|
}
|
|
|
|
respondWithError(w, status, err)
|
|
return
|
|
}
|
|
|
|
respond(w, http.StatusOK, tag)
|
|
}
|
|
|
|
func getTagLikeName(w http.ResponseWriter, r *http.Request) {
|
|
name := mux.Vars(r)["name"]
|
|
logger.Trace().Str("name", name).Msg("getting tag like name")
|
|
|
|
tag, err := lgc.GetProductTagsLikeName(name)
|
|
|
|
if err != nil {
|
|
respondWithError(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
respond(w, http.StatusOK, tag)
|
|
}
|
|
|
|
func postTagById(w http.ResponseWriter, r *http.Request) {
|
|
var tag *types.Tag
|
|
if err := json.NewDecoder(r.Body).Decode(&tag); err != nil {
|
|
respondWithError(w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
|
|
logger.Trace().Any("tag", tag).Msg("posting tag")
|
|
if err := lgc.CreateProductTag(tag); err != nil {
|
|
respondWithError(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
respond(w, http.StatusCreated, tag)
|
|
}
|
|
|
|
func putTagById(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseUint(mux.Vars(r)[idQry], 10, 64)
|
|
|
|
if err != nil {
|
|
respondWithError(w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
logger := logger.With().Uint("tag", uint(id)).Logger()
|
|
logger.Trace().Msg("putting tag")
|
|
|
|
var tag *types.Tag
|
|
if err = json.NewDecoder(r.Body).Decode(&tag); err != nil {
|
|
respondWithError(w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
|
|
logger.Trace().Any("new-tag", tag).Msg("parsed tag")
|
|
logger.Trace().Msg("checking id match")
|
|
|
|
if id != uint64(tag.Id) {
|
|
respondWithError(w, http.StatusBadRequest, ErrMismatchingIds)
|
|
return
|
|
}
|
|
|
|
logger.Trace().Msg("updating tag")
|
|
newTag, err := lgc.UpdateProductTag(tag)
|
|
if err != nil {
|
|
var status int
|
|
if err == types.ErrIDNotFound {
|
|
status = http.StatusNotFound
|
|
} else {
|
|
status = http.StatusInternalServerError
|
|
}
|
|
|
|
respondWithError(w, status, err)
|
|
return
|
|
}
|
|
|
|
respond(w, http.StatusCreated, newTag)
|
|
}
|
|
|
|
func deleteTagById(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseUint(mux.Vars(r)[idQry], 10, 64)
|
|
|
|
if err != nil {
|
|
respondWithError(w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
logger.Trace().Uint("tag", uint(id)).Msg("deleting tag")
|
|
|
|
tag, err := lgc.DeleteProductTag(uint(id))
|
|
if err != nil {
|
|
var status int
|
|
if err == types.ErrIDNotFound {
|
|
status = http.StatusNotFound
|
|
} else {
|
|
status = http.StatusInternalServerError
|
|
}
|
|
|
|
respondWithError(w, status, err)
|
|
return
|
|
}
|
|
|
|
respond(w, http.StatusAccepted, tag)
|
|
}
|
|
|
|
func SetProductTagRoutes(router *mux.Router) {
|
|
productTagRouter := router.PathPrefix("/producttag").Subrouter()
|
|
productTagRouter.HandleFunc("", getAllTags).Methods("GET")
|
|
productTagRouter.HandleFunc("/", getAllTags).Methods("GET")
|
|
|
|
productTagRouter.
|
|
HandleFunc("/search", getTagLikeName).
|
|
Methods("GET").
|
|
Queries("name", "{name}")
|
|
|
|
idPath := "/" + idVar
|
|
productTagRouter.HandleFunc(idPath, getTagById).Methods("GET")
|
|
productTagRouter.HandleFunc(idPath, getTagById).Methods("GET").Queries("searchby", "id")
|
|
productTagRouter.HandleFunc("/{name}", getTagByName).Methods("GET")
|
|
productTagRouter.HandleFunc("/{name}", getTagByName).Methods("GET").Queries("searchby", "name")
|
|
|
|
productTagRouter.HandleFunc("", postTagById).Methods("POST")
|
|
productTagRouter.HandleFunc("/", postTagById).Methods("POST")
|
|
productTagRouter.HandleFunc("/new", postTagById).Methods("POST")
|
|
|
|
productTagRouter.HandleFunc(idPath, putTagById).Methods("PUT")
|
|
|
|
productTagRouter.HandleFunc(idPath, deleteTagById).Methods("DELETE")
|
|
}
|