119 lines
2.1 KiB
Go
119 lines
2.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
database "app/database"
|
|
"app/models"
|
|
"app/repositories"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// Create(s *models.Specs) error
|
|
// GetSpecs(id uint) (*models.Specs, error)
|
|
// UpdateType(newType string, id uint) error
|
|
// UpdateValue(newValue string, id uint) error
|
|
// Delete(id uint) error
|
|
|
|
func initSpecs() *repositories.Specs_repository {
|
|
db := database.Db()
|
|
specsRepository := new(repositories.Specs_repository)
|
|
specsRepository.DB = &db
|
|
|
|
return specsRepository
|
|
}
|
|
|
|
func CreateSpecs(c echo.Context) error {
|
|
spec := new(models.Specs)
|
|
|
|
specsRepository := initSpecs()
|
|
|
|
if err := c.Bind(spec); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
err := specsRepository.Create(spec)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return c.JSON(http.StatusCreated, spec)
|
|
|
|
}
|
|
|
|
func GetSpecs(c echo.Context) error {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
specsRepository := initSpecs()
|
|
|
|
specs, err := specsRepository.GetSpecs(uint(id))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return c.JSON(http.StatusAccepted, specs)
|
|
|
|
}
|
|
|
|
func UpdateType(c echo.Context) error {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
var update struct {
|
|
NewType string `json:"type"`
|
|
}
|
|
specsRepository := initSpecs()
|
|
|
|
if update.NewType != "" {
|
|
err = specsRepository.UpdateType(update.NewType, uint(id))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
return c.String(http.StatusAccepted, "updated")
|
|
}
|
|
|
|
func UpdateValue(c echo.Context) error {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
var update struct {
|
|
NewValue string `json:"value"`
|
|
}
|
|
specsRepository := initSpecs()
|
|
|
|
if update.NewValue != "" {
|
|
err = specsRepository.UpdateValue(update.NewValue, uint(id))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
return c.String(http.StatusAccepted, "updated")
|
|
}
|
|
|
|
func DeleteSpecs(c echo.Context) error {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
specsRepository := initSpecs()
|
|
|
|
err = specsRepository.Delete(uint(id))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return c.String(http.StatusAccepted, "removed")
|
|
}
|