first commit

This commit is contained in:
TikoRosh
2024-07-06 09:13:03 +03:00
commit 668e3f37e2
17 changed files with 375 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
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)
}
+10
View File
@@ -0,0 +1,10 @@
package models
type Room struct {
ID int
Building string
Type string
Capacity int
Square string
Purpose string
}
+20
View File
@@ -0,0 +1,20 @@
package repository
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
"sync"
)
type PGrepo struct {
mu sync.Mutex
pool *pgxpool.Pool
}
func New(connStr string) (*PGrepo, error) {
pool, err := pgxpool.New(context.Background(), connStr)
if err != nil {
panic(err)
}
return &PGrepo{mu: sync.Mutex{}, pool: pool}, nil
}
+47
View File
@@ -0,0 +1,47 @@
package repository
import (
"context"
"fmt"
"log"
"vyatsuAPIInventory/vyatsuAPI/models"
)
func (repo *PGrepo) GetRooms(offset int, limit int) ([]models.Room, error) {
rows, err := repo.pool.Query(context.Background(), "select * from rooms LIMIT $1 OFFSET $2", limit, offset)
if err != nil {
log.Fatal(err)
return nil, err
}
defer rows.Close()
var rooms []models.Room
for rows.Next() {
e := models.Room{}
err := rows.Scan(&e.ID, &e.Building, &e.Type, &e.Capacity, &e.Square, &e.Purpose)
if err != nil {
fmt.Println(err)
continue
}
rooms = append(rooms, e)
}
return rooms, nil
}
func (repo *PGrepo) GetRoomsWithFilterByType(offset int, limit int, Type string) ([]models.Room, error) {
rows, err := repo.pool.Query(context.Background(), "select * from rooms where type = $3 LIMIT $1 OFFSET $2", limit, offset, Type)
if err != nil {
log.Fatal(err)
return nil, err
}
defer rows.Close()
var Rooms []models.Room
for rows.Next() {
e := models.Room{}
err := rows.Scan(&e.ID, &e.Building, &e.Type, &e.Capacity, &e.Square, &e.Purpose)
if err != nil {
fmt.Println(err)
continue
}
Rooms = append(Rooms, e)
}
return Rooms, nil
}