67 lines
2.0 KiB
Go
67 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"Zadanie3VaytsuApi/main/models"
|
|
"Zadanie3VaytsuApi/main/repository"
|
|
"github.com/labstack/echo/v4"
|
|
echoSwagger "github.com/swaggo/echo-swagger"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
const connectionURL = "postgres://tgbot:pass@localhost:5432/vyatsu"
|
|
|
|
func main() {
|
|
e := echo.New()
|
|
e.GET("/equipment", getEquipment)
|
|
e.GET("/equipment/ownerid/:id", getEquipmentWithOwnerId)
|
|
e.GET("/equipment/inventoryNumber/:id", getEquipmentWithInventoryNumber)
|
|
e.GET("/swagger/*", echoSwagger.WrapHandler)
|
|
// Start server
|
|
e.Logger.Fatal(e.Start(":8080"))
|
|
}
|
|
func getEquipment(e echo.Context) error {
|
|
db, err := repository.New(connectionURL)
|
|
offset, err := strconv.Atoi(e.QueryParam("offset"))
|
|
limit, err := strconv.Atoi(e.QueryParam("limit"))
|
|
var equipment []models.Equipment
|
|
if limit == 0 {
|
|
limit = 10
|
|
}
|
|
equipment, err = db.GetEquipment(offset, limit)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return e.JSON(http.StatusOK, equipment)
|
|
}
|
|
func getEquipmentWithOwnerId(e echo.Context) error {
|
|
db, err := repository.New(connectionURL)
|
|
offset, err := strconv.Atoi(e.QueryParam("offset"))
|
|
limit, err := strconv.Atoi(e.QueryParam("limit"))
|
|
ownerId, err := strconv.Atoi(e.Param("id"))
|
|
var equipment []models.Equipment
|
|
if limit == 0 {
|
|
limit = 10
|
|
}
|
|
equipment, err = db.GetEquipmentWithOwnerId(offset, limit, ownerId)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return e.JSON(http.StatusOK, equipment)
|
|
}
|
|
func getEquipmentWithInventoryNumber(e echo.Context) error {
|
|
db, err := repository.New(connectionURL)
|
|
offset, err := strconv.Atoi(e.QueryParam("offset"))
|
|
limit, err := strconv.Atoi(e.QueryParam("limit"))
|
|
inventoryNumber, err := strconv.Atoi(e.Param("id"))
|
|
var equipment []models.Equipment
|
|
if limit == 0 {
|
|
limit = 10
|
|
}
|
|
equipment, err = db.GetEquipmentWithOwnerId(offset, limit, inventoryNumber)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return e.JSON(http.StatusOK, equipment)
|
|
}
|