2022-09-03 09:17:48 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2022-09-06 19:15:07 +00:00
|
|
|
"encoding/csv"
|
2022-09-03 09:17:48 +00:00
|
|
|
"net/http"
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/repo"
|
2022-09-24 19:33:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/services"
|
|
|
|
"github.com/hay-kot/homebox/backend/pkgs/server"
|
2022-09-03 18:38:35 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-09-03 09:17:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HandleItemsGetAll godoc
|
|
|
|
// @Summary Get All Items
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
2022-09-27 23:52:13 +00:00
|
|
|
// @Success 200 {object} server.Results{items=[]repo.ItemSummary}
|
2022-09-03 09:17:48 +00:00
|
|
|
// @Router /v1/items [GET]
|
|
|
|
// @Security Bearer
|
|
|
|
func (ctrl *V1Controller) HandleItemsGetAll() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user := services.UseUserCtx(r.Context())
|
|
|
|
items, err := ctrl.svc.Items.GetAll(r.Context(), user.GroupID)
|
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to get items")
|
2022-09-03 09:17:48 +00:00
|
|
|
server.RespondServerError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
server.Respond(w, http.StatusOK, server.Results{Items: items})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemsCreate godoc
|
|
|
|
// @Summary Create a new item
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
2022-09-27 23:52:13 +00:00
|
|
|
// @Param payload body repo.ItemCreate true "Item Data"
|
|
|
|
// @Success 200 {object} repo.ItemSummary
|
2022-09-03 09:17:48 +00:00
|
|
|
// @Router /v1/items [POST]
|
|
|
|
// @Security Bearer
|
|
|
|
func (ctrl *V1Controller) HandleItemsCreate() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-09-27 23:52:13 +00:00
|
|
|
createData := repo.ItemCreate{}
|
2022-09-03 09:17:48 +00:00
|
|
|
if err := server.Decode(r, &createData); err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to decode request body")
|
2022-09-03 09:17:48 +00:00
|
|
|
server.RespondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := services.UseUserCtx(r.Context())
|
|
|
|
item, err := ctrl.svc.Items.Create(r.Context(), user.GroupID, createData)
|
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to create item")
|
2022-09-03 09:17:48 +00:00
|
|
|
server.RespondServerError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
server.Respond(w, http.StatusCreated, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemDelete godocs
|
|
|
|
// @Summary deletes a item
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
2022-09-24 19:33:38 +00:00
|
|
|
// @Param id path string true "Item ID"
|
2022-09-03 09:17:48 +00:00
|
|
|
// @Success 204
|
|
|
|
// @Router /v1/items/{id} [DELETE]
|
|
|
|
// @Security Bearer
|
|
|
|
func (ctrl *V1Controller) HandleItemDelete() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
uid, user, err := ctrl.partialParseIdAndUser(w, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ctrl.svc.Items.Delete(r.Context(), user.GroupID, uid)
|
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to delete item")
|
2022-09-03 09:17:48 +00:00
|
|
|
server.RespondServerError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
server.Respond(w, http.StatusNoContent, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemGet godocs
|
|
|
|
// @Summary Gets a item and fields
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Item ID"
|
2022-09-27 23:52:13 +00:00
|
|
|
// @Success 200 {object} repo.ItemOut
|
2022-09-03 09:17:48 +00:00
|
|
|
// @Router /v1/items/{id} [GET]
|
|
|
|
// @Security Bearer
|
|
|
|
func (ctrl *V1Controller) HandleItemGet() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
uid, user, err := ctrl.partialParseIdAndUser(w, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
items, err := ctrl.svc.Items.GetOne(r.Context(), user.GroupID, uid)
|
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to get item")
|
2022-09-03 09:17:48 +00:00
|
|
|
server.RespondServerError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
server.Respond(w, http.StatusOK, items)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemUpdate godocs
|
|
|
|
// @Summary updates a item
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
2022-09-27 23:52:13 +00:00
|
|
|
// @Param id path string true "Item ID"
|
|
|
|
// @Param payload body repo.ItemUpdate true "Item Data"
|
|
|
|
// @Success 200 {object} repo.ItemOut
|
2022-09-03 09:17:48 +00:00
|
|
|
// @Router /v1/items/{id} [PUT]
|
|
|
|
// @Security Bearer
|
|
|
|
func (ctrl *V1Controller) HandleItemUpdate() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-09-27 23:52:13 +00:00
|
|
|
body := repo.ItemUpdate{}
|
2022-09-03 09:17:48 +00:00
|
|
|
if err := server.Decode(r, &body); err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to decode request body")
|
2022-09-03 09:17:48 +00:00
|
|
|
server.RespondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
uid, user, err := ctrl.partialParseIdAndUser(w, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body.ID = uid
|
|
|
|
result, err := ctrl.svc.Items.Update(r.Context(), user.GroupID, body)
|
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to update item")
|
2022-09-03 09:17:48 +00:00
|
|
|
server.RespondServerError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
server.Respond(w, http.StatusOK, result)
|
|
|
|
}
|
|
|
|
}
|
2022-09-06 19:15:07 +00:00
|
|
|
|
|
|
|
// HandleItemsImport godocs
|
|
|
|
// @Summary imports items into the database
|
|
|
|
// @Tags Items
|
|
|
|
// @Produce json
|
|
|
|
// @Success 204
|
|
|
|
// @Param csv formData file true "Image to upload"
|
|
|
|
// @Router /v1/items/import [Post]
|
|
|
|
// @Security Bearer
|
|
|
|
func (ctrl *V1Controller) HandleItemsImport() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2022-10-07 02:54:09 +00:00
|
|
|
err := r.ParseMultipartForm(ctrl.maxUploadSize << 20)
|
2022-09-09 18:22:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to parse multipart form")
|
|
|
|
server.RespondServerError(w)
|
|
|
|
return
|
|
|
|
}
|
2022-09-06 19:15:07 +00:00
|
|
|
|
|
|
|
file, _, err := r.FormFile("csv")
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to get file from form")
|
|
|
|
server.RespondServerError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := csv.NewReader(file)
|
|
|
|
data, err := reader.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to read csv")
|
|
|
|
server.RespondServerError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := services.UseUserCtx(r.Context())
|
|
|
|
|
|
|
|
err = ctrl.svc.Items.CsvImport(r.Context(), user.GroupID, data)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to import items")
|
|
|
|
server.RespondServerError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
server.Respond(w, http.StatusNoContent, nil)
|
|
|
|
}
|
|
|
|
}
|