first commit
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user