Fisrt commit
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package models
|
||||
|
||||
type Equipment struct {
|
||||
GUID int
|
||||
InventoryNumber int
|
||||
Name string
|
||||
OwnerId int
|
||||
ReceiptDate string
|
||||
LifeTime string
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"Zadanie3VaytsuApi/main/models"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
func (repo *PGrepo) GetEquipment(offset int, limit int) ([]models.Equipment, error) {
|
||||
rows, err := repo.pool.Query(context.Background(), "select * from Equipment LIMIT $1 OFFSET $2", limit, offset)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var equipments []models.Equipment
|
||||
for rows.Next() {
|
||||
equipment := models.Equipment{}
|
||||
err := rows.Scan(&equipment.GUID, &equipment.InventoryNumber, &equipment.Name, &equipment.OwnerId, &equipment.ReceiptDate,
|
||||
&equipment.LifeTime)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
equipments = append(equipments, equipment)
|
||||
}
|
||||
return equipments, nil
|
||||
}
|
||||
func (repo *PGrepo) GetEquipmentWithInventoryNumber(offset int, limit int, number int) ([]models.Equipment, error) {
|
||||
rows, err := repo.pool.Query(context.Background(), "select * from Equipment WHERE inventoryNumber = $3 LIMIT $1 OFFSET $2", limit, offset, number)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var equipments []models.Equipment
|
||||
for rows.Next() {
|
||||
equipment := models.Equipment{}
|
||||
err := rows.Scan(&equipment.GUID, &equipment.InventoryNumber, &equipment.Name, &equipment.OwnerId, &equipment.ReceiptDate,
|
||||
&equipment.LifeTime)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
equipments = append(equipments, equipment)
|
||||
}
|
||||
return equipments, nil
|
||||
}
|
||||
func (repo *PGrepo) GetEquipmentWithOwnerId(offset int, limit int, ownerid int) ([]models.Equipment, error) {
|
||||
rows, err := repo.pool.Query(context.Background(), "select * from Equipment WHERE OwnerId = $3 LIMIT $1 OFFSET $2", limit, offset, ownerid)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var equipments []models.Equipment
|
||||
for rows.Next() {
|
||||
equipment := models.Equipment{}
|
||||
err := rows.Scan(&equipment.GUID, &equipment.InventoryNumber, &equipment.Name, &equipment.OwnerId, &equipment.ReceiptDate,
|
||||
&equipment.LifeTime)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
equipments = append(equipments, equipment)
|
||||
}
|
||||
return equipments, nil
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user