55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/swaggo/echo-swagger"
|
|
"net/http"
|
|
"strconv"
|
|
_ "vyatsuAPIInventory/docs"
|
|
"vyatsuAPIInventory/vyatsuAPI/models"
|
|
"vyatsuAPIInventory/vyatsuAPI/repository"
|
|
)
|
|
|
|
const connectionURL = "postgres://tgbot:pass@localhost:5432/vyatsu"
|
|
|
|
// @title Vyatsu
|
|
// @host localhost:8080
|
|
// @version 1.0
|
|
// @BasePath /
|
|
func main() {
|
|
e := echo.New()
|
|
e.GET("/rooms", getRooms)
|
|
e.GET("/swagger/*", echoSwagger.WrapHandler)
|
|
// Start server
|
|
e.Logger.Fatal(e.Start(":8080"))
|
|
}
|
|
|
|
// @Summary Get List Of Rooms
|
|
// @Tags get
|
|
// @Description get list
|
|
// @Produce json
|
|
// @Success 200
|
|
func getRooms(e echo.Context) error {
|
|
db, err := repository.New(connectionURL)
|
|
offset, err := strconv.Atoi(e.QueryParam("offset"))
|
|
limit, err := strconv.Atoi(e.QueryParam("limit"))
|
|
Type := e.QueryParam("Type")
|
|
var rooms []models.Room
|
|
if limit == 0 {
|
|
limit = 10
|
|
}
|
|
if Type == "" {
|
|
rooms, err = db.GetRooms(offset, limit)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
} else {
|
|
rooms, err = db.GetRoomsWithFilterByType(offset, limit, Type)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
}
|
|
return e.JSON(http.StatusOK, rooms)
|
|
|
|
}
|