57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/labstack/echo/v4"
|
|
echoSwagger "github.com/swaggo/echo-swagger"
|
|
"net/http"
|
|
"strconv"
|
|
"zadanie7API/main/models"
|
|
"zadanie7API/main/repository"
|
|
)
|
|
|
|
const connectionURL = "postgres://tgbot:pass@localhost:5432/vyatsu"
|
|
|
|
//INPUT DATA IN FORM-DATA FORMAT
|
|
|
|
func main() {
|
|
e := echo.New()
|
|
e.GET("/equipment/:id", getEquipment)
|
|
e.GET("/swagger/*", echoSwagger.WrapHandler)
|
|
e.PUT("/equipment", createEquipment)
|
|
// Start server
|
|
e.Logger.Fatal(e.Start(":8080"))
|
|
}
|
|
func getEquipment(e echo.Context) error {
|
|
db, err := repository.New(connectionURL)
|
|
id, err := strconv.Atoi(e.Param("id"))
|
|
equipment, err := db.GetEquipment(id)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return e.JSON(http.StatusOK, equipment)
|
|
}
|
|
func createEquipment(e echo.Context) error {
|
|
db, err := repository.New(connectionURL)
|
|
|
|
var Equipment models.Equipment
|
|
|
|
name := e.FormValue("Name")
|
|
ownerId, err := strconv.Atoi(e.FormValue("OwnerId"))
|
|
receiptDate := e.FormValue("ReceiptDate")
|
|
liteTime := e.FormValue("LifeTime")
|
|
inventoryNumber, err := strconv.Atoi(e.FormValue("InventoryNumber"))
|
|
|
|
Equipment = models.Equipment{
|
|
Name: name,
|
|
OwnerId: ownerId,
|
|
ReceiptDate: receiptDate,
|
|
LifeTime: liteTime,
|
|
InventoryNumber: inventoryNumber,
|
|
}
|
|
db.CreateEquipment(Equipment)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return e.JSON(http.StatusOK, Equipment)
|
|
}
|