2022-12-10 05:57:57 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/google/uuid"
|
2022-12-10 05:57:57 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/web/adapters"
|
|
|
|
"github.com/hay-kot/safeserve/errchain"
|
2022-12-10 05:57:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HandleMaintenanceGetLog godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get Maintenance Log
|
|
|
|
// @Tags Maintenance
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} repo.MaintenanceLog
|
|
|
|
// @Router /v1/items/{id}/maintenance [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleMaintenanceLogGet() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, ID uuid.UUID, q repo.MaintenanceLogQuery) (repo.MaintenanceLog, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.MaintEntry.GetLog(auth, auth.GID, ID, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapters.QueryID("id", fn, http.StatusOK)
|
2022-12-10 05:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleMaintenanceEntryCreate godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Create Maintenance Entry
|
|
|
|
// @Tags Maintenance
|
|
|
|
// @Produce json
|
|
|
|
// @Param payload body repo.MaintenanceEntryCreate true "Entry Data"
|
2023-03-21 04:32:10 +00:00
|
|
|
// @Success 201 {object} repo.MaintenanceEntry
|
2023-03-07 06:18:58 +00:00
|
|
|
// @Router /v1/items/{id}/maintenance [POST]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleMaintenanceEntryCreate() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, itemID uuid.UUID, body repo.MaintenanceEntryCreate) (repo.MaintenanceEntry, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.MaintEntry.Create(auth, itemID, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapters.ActionID("id", fn, http.StatusCreated)
|
2022-12-10 05:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleMaintenanceEntryDelete godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Delete Maintenance Entry
|
|
|
|
// @Tags Maintenance
|
|
|
|
// @Produce json
|
|
|
|
// @Success 204
|
|
|
|
// @Router /v1/items/{id}/maintenance/{entry_id} [DELETE]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleMaintenanceEntryDelete() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, entryID uuid.UUID) (any, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
err := ctrl.repo.MaintEntry.Delete(auth, entryID)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapters.CommandID("entry_id", fn, http.StatusNoContent)
|
2022-12-10 05:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleMaintenanceEntryUpdate godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Update Maintenance Entry
|
|
|
|
// @Tags Maintenance
|
|
|
|
// @Produce json
|
|
|
|
// @Param payload body repo.MaintenanceEntryUpdate true "Entry Data"
|
|
|
|
// @Success 200 {object} repo.MaintenanceEntry
|
|
|
|
// @Router /v1/items/{id}/maintenance/{entry_id} [PUT]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleMaintenanceEntryUpdate() errchain.HandlerFunc {
|
|
|
|
fn := func(r *http.Request, entryID uuid.UUID, body repo.MaintenanceEntryUpdate) (repo.MaintenanceEntry, error) {
|
|
|
|
auth := services.NewContext(r.Context())
|
|
|
|
return ctrl.repo.MaintEntry.Update(auth, entryID, body)
|
2022-12-10 05:57:57 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.ActionID("entry_id", fn, http.StatusOK)
|
2022-12-10 05:57:57 +00:00
|
|
|
}
|