This commit is contained in:
vinchent 2024-07-30 21:59:03 +02:00
parent 30c552cf6c
commit f20f256313
6 changed files with 983 additions and 0 deletions

View File

@ -0,0 +1,32 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.26.0
package db
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}
}

View File

@ -0,0 +1,63 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.26.0
package db
import (
"github.com/jackc/pgx/v5/pgtype"
)
type Reservation struct {
ID int32
FirstName string
LastName string
Email string
Phone string
StartDate pgtype.Date
EndDate pgtype.Date
RoomID int32
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
Processed int32
}
type Restriction struct {
ID int32
RestrictionName string
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
}
type Room struct {
ID int32
RoomName string
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
}
type RoomRestriction struct {
ID int32
StartDate pgtype.Date
EndDate pgtype.Date
RoomID int32
ReservationID pgtype.Int4
RestrictionID int32
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
}
type SchemaMigration struct {
Version string
}
type User struct {
ID int32
FirstName string
LastName string
Email string
Password string
AccessLevel int32
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
}

View File

@ -0,0 +1,581 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.26.0
// source: query.sql
package db
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const allNewReservations = `-- name: AllNewReservations :many
SELECT
r.id,
r.first_name,
r.last_name,
r.email,
r.phone,
r.start_date,
r.end_date,
r.room_id,
r.created_at,
r.updated_at,
r.processed,
rm.id,
rm.room_name
FROM
reservations r
LEFT JOIN rooms rm ON (r.room_id = rm.id)
WHERE
r.processed = 0
ORDER BY
r.start_date ASC
`
type AllNewReservationsRow struct {
ID int32
FirstName string
LastName string
Email string
Phone string
StartDate pgtype.Date
EndDate pgtype.Date
RoomID int32
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
Processed int32
ID_2 pgtype.Int4
RoomName pgtype.Text
}
func (q *Queries) AllNewReservations(ctx context.Context) ([]AllNewReservationsRow, error) {
rows, err := q.db.Query(ctx, allNewReservations)
if err != nil {
return nil, err
}
defer rows.Close()
var items []AllNewReservationsRow
for rows.Next() {
var i AllNewReservationsRow
if err := rows.Scan(
&i.ID,
&i.FirstName,
&i.LastName,
&i.Email,
&i.Phone,
&i.StartDate,
&i.EndDate,
&i.RoomID,
&i.CreatedAt,
&i.UpdatedAt,
&i.Processed,
&i.ID_2,
&i.RoomName,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const allReservations = `-- name: AllReservations :many
SELECT
r.id,
r.first_name,
r.last_name,
r.email,
r.phone,
r.start_date,
r.end_date,
r.room_id,
r.created_at,
r.updated_at,
r.processed,
rm.id,
rm.room_name
FROM
reservations r
LEFT JOIN rooms rm ON (r.room_id = rm.id)
ORDER BY
r.start_date ASC
`
type AllReservationsRow struct {
ID int32
FirstName string
LastName string
Email string
Phone string
StartDate pgtype.Date
EndDate pgtype.Date
RoomID int32
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
Processed int32
ID_2 pgtype.Int4
RoomName pgtype.Text
}
func (q *Queries) AllReservations(ctx context.Context) ([]AllReservationsRow, error) {
rows, err := q.db.Query(ctx, allReservations)
if err != nil {
return nil, err
}
defer rows.Close()
var items []AllReservationsRow
for rows.Next() {
var i AllReservationsRow
if err := rows.Scan(
&i.ID,
&i.FirstName,
&i.LastName,
&i.Email,
&i.Phone,
&i.StartDate,
&i.EndDate,
&i.RoomID,
&i.CreatedAt,
&i.UpdatedAt,
&i.Processed,
&i.ID_2,
&i.RoomName,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const allRooms = `-- name: AllRooms :many
SELECT
id,
room_name,
created_at,
updated_at
FROM
rooms
ORDER BY
room_name
`
func (q *Queries) AllRooms(ctx context.Context) ([]Room, error) {
rows, err := q.db.Query(ctx, allRooms)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Room
for rows.Next() {
var i Room
if err := rows.Scan(
&i.ID,
&i.RoomName,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const deleteReservation = `-- name: DeleteReservation :exec
DELETE FROM reservations
WHERE
id = $1
`
func (q *Queries) DeleteReservation(ctx context.Context, id int32) error {
_, err := q.db.Exec(ctx, deleteReservation, id)
return err
}
const getReservationByID = `-- name: GetReservationByID :one
SELECT
r.id,
r.first_name,
r.last_name,
r.email,
r.phone,
r.start_date,
r.end_date,
r.room_id,
r.created_at,
r.updated_at,
r.processed,
rm.id,
rm.room_name
FROM
reservations r
LEFT JOIN rooms rm ON (r.room_id = rm.id)
WHERE
r.id = $1
`
type GetReservationByIDRow struct {
ID int32
FirstName string
LastName string
Email string
Phone string
StartDate pgtype.Date
EndDate pgtype.Date
RoomID int32
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
Processed int32
ID_2 pgtype.Int4
RoomName pgtype.Text
}
func (q *Queries) GetReservationByID(ctx context.Context, id int32) (GetReservationByIDRow, error) {
row := q.db.QueryRow(ctx, getReservationByID, id)
var i GetReservationByIDRow
err := row.Scan(
&i.ID,
&i.FirstName,
&i.LastName,
&i.Email,
&i.Phone,
&i.StartDate,
&i.EndDate,
&i.RoomID,
&i.CreatedAt,
&i.UpdatedAt,
&i.Processed,
&i.ID_2,
&i.RoomName,
)
return i, err
}
const getRoomById = `-- name: GetRoomById :one
SELECT
id,
room_name,
created_at,
updated_at
FROM
rooms
WHERE
id = $1
`
func (q *Queries) GetRoomById(ctx context.Context, id int32) (Room, error) {
row := q.db.QueryRow(ctx, getRoomById, id)
var i Room
err := row.Scan(
&i.ID,
&i.RoomName,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const getUserByID = `-- name: GetUserByID :one
SELECT
id,
first_name,
last_name,
email,
password,
access_level,
created_at,
updated_at
FROM
users
WHERE
id = $1
`
func (q *Queries) GetUserByID(ctx context.Context, id int32) (User, error) {
row := q.db.QueryRow(ctx, getUserByID, id)
var i User
err := row.Scan(
&i.ID,
&i.FirstName,
&i.LastName,
&i.Email,
&i.Password,
&i.AccessLevel,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const getUserCred = `-- name: GetUserCred :one
SELECT
id,
password
FROM
users
WHERE
email = $1
`
type GetUserCredRow struct {
ID int32
Password string
}
func (q *Queries) GetUserCred(ctx context.Context, email string) (GetUserCredRow, error) {
row := q.db.QueryRow(ctx, getUserCred, email)
var i GetUserCredRow
err := row.Scan(&i.ID, &i.Password)
return i, err
}
const insertReservation = `-- name: InsertReservation :one
INSERT INTO
reservations (
first_name,
last_name,
email,
phone,
start_date,
end_date,
room_id,
created_at,
updated_at
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING
id
`
type InsertReservationParams struct {
FirstName string
LastName string
Email string
Phone string
StartDate pgtype.Date
EndDate pgtype.Date
RoomID int32
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
}
func (q *Queries) InsertReservation(ctx context.Context, arg InsertReservationParams) (int32, error) {
row := q.db.QueryRow(ctx, insertReservation,
arg.FirstName,
arg.LastName,
arg.Email,
arg.Phone,
arg.StartDate,
arg.EndDate,
arg.RoomID,
arg.CreatedAt,
arg.UpdatedAt,
)
var id int32
err := row.Scan(&id)
return id, err
}
const insertRoomRestriction = `-- name: InsertRoomRestriction :exec
INSERT INTO
room_restrictions (
start_date,
end_date,
room_id,
reservation_id,
restriction_id,
created_at,
updated_at
)
VALUES
($1, $2, $3, $4, $5, $6, $7)
`
type InsertRoomRestrictionParams struct {
StartDate pgtype.Date
EndDate pgtype.Date
RoomID int32
ReservationID pgtype.Int4
RestrictionID int32
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
}
func (q *Queries) InsertRoomRestriction(ctx context.Context, arg InsertRoomRestrictionParams) error {
_, err := q.db.Exec(ctx, insertRoomRestriction,
arg.StartDate,
arg.EndDate,
arg.RoomID,
arg.ReservationID,
arg.RestrictionID,
arg.CreatedAt,
arg.UpdatedAt,
)
return err
}
const searchAvailabilityByDatesByRoomID = `-- name: SearchAvailabilityByDatesByRoomID :one
SELECT
count(id)
FROM
room_restrictions
WHERE
room_id = $1
AND $2 < end_date
AND $3 > start_date
`
type SearchAvailabilityByDatesByRoomIDParams struct {
RoomID int32
EndDate pgtype.Date
StartDate pgtype.Date
}
func (q *Queries) SearchAvailabilityByDatesByRoomID(ctx context.Context, arg SearchAvailabilityByDatesByRoomIDParams) (int64, error) {
row := q.db.QueryRow(ctx, searchAvailabilityByDatesByRoomID, arg.RoomID, arg.EndDate, arg.StartDate)
var count int64
err := row.Scan(&count)
return count, err
}
const searchAvailabilityForAllRooms = `-- name: SearchAvailabilityForAllRooms :many
SELECT
r.id,
r.room_name
FROM
rooms r
WHERE
r.id NOT IN (
SELECT
room_id
FROM
room_restrictions rr
WHERE
$1 < rr.end_date
AND $2 > rr.start_date
)
`
type SearchAvailabilityForAllRoomsParams struct {
EndDate pgtype.Date
StartDate pgtype.Date
}
type SearchAvailabilityForAllRoomsRow struct {
ID int32
RoomName string
}
func (q *Queries) SearchAvailabilityForAllRooms(ctx context.Context, arg SearchAvailabilityForAllRoomsParams) ([]SearchAvailabilityForAllRoomsRow, error) {
rows, err := q.db.Query(ctx, searchAvailabilityForAllRooms, arg.EndDate, arg.StartDate)
if err != nil {
return nil, err
}
defer rows.Close()
var items []SearchAvailabilityForAllRoomsRow
for rows.Next() {
var i SearchAvailabilityForAllRoomsRow
if err := rows.Scan(&i.ID, &i.RoomName); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const updateProcessedForReservation = `-- name: UpdateProcessedForReservation :exec
UPDATE reservations
SET
processed = $1
WHERE
id = $2
`
type UpdateProcessedForReservationParams struct {
Processed int32
ID int32
}
func (q *Queries) UpdateProcessedForReservation(ctx context.Context, arg UpdateProcessedForReservationParams) error {
_, err := q.db.Exec(ctx, updateProcessedForReservation, arg.Processed, arg.ID)
return err
}
const updateReservation = `-- name: UpdateReservation :exec
UPDATE reservations
SET
first_name = $1,
last_name = $2,
email = $3,
phone = $4,
updated_at = $5
WHERE
id = $6
`
type UpdateReservationParams struct {
FirstName string
LastName string
Email string
Phone string
UpdatedAt pgtype.Timestamp
ID int32
}
func (q *Queries) UpdateReservation(ctx context.Context, arg UpdateReservationParams) error {
_, err := q.db.Exec(ctx, updateReservation,
arg.FirstName,
arg.LastName,
arg.Email,
arg.Phone,
arg.UpdatedAt,
arg.ID,
)
return err
}
const updateUser = `-- name: UpdateUser :exec
UPDATE users
SET
first_name = $1,
last_name = $2,
email = $3,
access_level = $4,
updated_at = $5
`
type UpdateUserParams struct {
FirstName string
LastName string
Email string
AccessLevel int32
UpdatedAt pgtype.Timestamp
}
func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) error {
_, err := q.db.Exec(ctx, updateUser,
arg.FirstName,
arg.LastName,
arg.Email,
arg.AccessLevel,
arg.UpdatedAt,
)
return err
}

201
sql/query.sql Normal file
View File

@ -0,0 +1,201 @@
-- name: InsertReservation :one
INSERT INTO
reservations (
first_name,
last_name,
email,
phone,
start_date,
end_date,
room_id,
created_at,
updated_at
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING
id;
-- name: InsertRoomRestriction :exec
INSERT INTO
room_restrictions (
start_date,
end_date,
room_id,
reservation_id,
restriction_id,
created_at,
updated_at
)
VALUES
($1, $2, $3, $4, $5, $6, $7);
-- name: SearchAvailabilityByDatesByRoomID :one
SELECT
count(id)
FROM
room_restrictions
WHERE
room_id = $1
AND $2 < end_date
AND $3 > start_date;
-- name: SearchAvailabilityForAllRooms :many
SELECT
r.id,
r.room_name
FROM
rooms r
WHERE
r.id NOT IN (
SELECT
room_id
FROM
room_restrictions rr
WHERE
$1 < rr.end_date
AND $2 > rr.start_date
);
-- name: GetRoomById :one
SELECT
id,
room_name,
created_at,
updated_at
FROM
rooms
WHERE
id = $1;
-- name: GetUserByID :one
SELECT
id,
first_name,
last_name,
email,
password,
access_level,
created_at,
updated_at
FROM
users
WHERE
id = $1;
-- name: UpdateUser :exec
UPDATE users
SET
first_name = $1,
last_name = $2,
email = $3,
access_level = $4,
updated_at = $5;
-- name: GetUserCred :one
SELECT
id,
password
FROM
users
WHERE
email = $1;
-- name: AllReservations :many
SELECT
r.id,
r.first_name,
r.last_name,
r.email,
r.phone,
r.start_date,
r.end_date,
r.room_id,
r.created_at,
r.updated_at,
r.processed,
rm.id,
rm.room_name
FROM
reservations r
LEFT JOIN rooms rm ON (r.room_id = rm.id)
ORDER BY
r.start_date ASC;
-- name: AllNewReservations :many
SELECT
r.id,
r.first_name,
r.last_name,
r.email,
r.phone,
r.start_date,
r.end_date,
r.room_id,
r.created_at,
r.updated_at,
r.processed,
rm.id,
rm.room_name
FROM
reservations r
LEFT JOIN rooms rm ON (r.room_id = rm.id)
WHERE
r.processed = 0
ORDER BY
r.start_date ASC;
-- name: GetReservationByID :one
SELECT
r.id,
r.first_name,
r.last_name,
r.email,
r.phone,
r.start_date,
r.end_date,
r.room_id,
r.created_at,
r.updated_at,
r.processed,
rm.id,
rm.room_name
FROM
reservations r
LEFT JOIN rooms rm ON (r.room_id = rm.id)
WHERE
r.id = $1;
-- name: UpdateReservation :exec
UPDATE reservations
SET
first_name = $1,
last_name = $2,
email = $3,
phone = $4,
updated_at = $5
WHERE
id = $6;
-- name: DeleteReservation :exec
DELETE FROM reservations
WHERE
id = $1;
-- name: UpdateProcessedForReservation :exec
UPDATE reservations
SET
processed = $1
WHERE
id = $2;
-- name: AllRooms :many
SELECT
id,
room_name,
created_at,
updated_at
FROM
rooms
ORDER BY
room_name;

96
sql/schema.sql Normal file
View File

@ -0,0 +1,96 @@
-- Adminer 4.8.1 PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) dump
CREATE SEQUENCE reservations_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1;
CREATE TABLE "public"."reservations" (
"id" integer DEFAULT nextval('reservations_id_seq') NOT NULL,
"first_name" character varying(255) DEFAULT '' NOT NULL,
"last_name" character varying(255) DEFAULT '' NOT NULL,
"email" character varying(255) NOT NULL,
"phone" character varying(255) DEFAULT '' NOT NULL,
"start_date" date NOT NULL,
"end_date" date NOT NULL,
"room_id" integer NOT NULL,
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL,
"processed" integer DEFAULT '0' NOT NULL,
CONSTRAINT "reservations_pkey" PRIMARY KEY ("id")
) WITH (oids = false);
CREATE INDEX "reservations_email_idx" ON "public"."reservations" USING btree ("email");
CREATE INDEX "reservations_last_name_idx" ON "public"."reservations" USING btree ("last_name");
CREATE SEQUENCE restrictions_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1;
CREATE TABLE "public"."restrictions" (
"id" integer DEFAULT nextval('restrictions_id_seq') NOT NULL,
"restriction_name" character varying(255) NOT NULL,
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL,
CONSTRAINT "restrictions_pkey" PRIMARY KEY ("id")
) WITH (oids = false);
CREATE SEQUENCE room_restrictions_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1;
CREATE TABLE "public"."room_restrictions" (
"id" integer DEFAULT nextval('room_restrictions_id_seq') NOT NULL,
"start_date" date NOT NULL,
"end_date" date NOT NULL,
"room_id" integer NOT NULL,
"reservation_id" integer,
"restriction_id" integer NOT NULL,
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL,
CONSTRAINT "room_restrictions_pkey" PRIMARY KEY ("id")
) WITH (oids = false);
CREATE INDEX "room_restrictions_reservation_id_idx" ON "public"."room_restrictions" USING btree ("reservation_id");
CREATE INDEX "room_restrictions_room_id_idx" ON "public"."room_restrictions" USING btree ("room_id");
CREATE INDEX "room_restrictions_start_date_end_date_idx" ON "public"."room_restrictions" USING btree ("start_date", "end_date");
CREATE SEQUENCE rooms_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1;
CREATE TABLE "public"."rooms" (
"id" integer DEFAULT nextval('rooms_id_seq') NOT NULL,
"room_name" character varying(255) NOT NULL,
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL,
CONSTRAINT "rooms_pkey" PRIMARY KEY ("id")
) WITH (oids = false);
CREATE TABLE "public"."schema_migration" (
"version" character varying(14) NOT NULL,
CONSTRAINT "schema_migration_pkey" PRIMARY KEY ("version"),
CONSTRAINT "schema_migration_version_idx" UNIQUE ("version")
) WITH (oids = false);
CREATE SEQUENCE users_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1;
CREATE TABLE "public"."users" (
"id" integer DEFAULT nextval('users_id_seq') NOT NULL,
"first_name" character varying(255) DEFAULT '' NOT NULL,
"last_name" character varying(255) DEFAULT '' NOT NULL,
"email" character varying(255) NOT NULL,
"password" character varying(60) NOT NULL,
"access_level" integer DEFAULT '1' NOT NULL,
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL,
CONSTRAINT "users_email_idx" UNIQUE ("email"),
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
) WITH (oids = false);
ALTER TABLE ONLY "public"."reservations" ADD CONSTRAINT "reservations_rooms_id_fk" FOREIGN KEY (room_id) REFERENCES rooms(id) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."room_restrictions" ADD CONSTRAINT "room_restrictions_reservations_id_fk" FOREIGN KEY (reservation_id) REFERENCES reservations(id) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."room_restrictions" ADD CONSTRAINT "room_restrictions_restrictions_id_fk" FOREIGN KEY (restriction_id) REFERENCES restrictions(id) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;
ALTER TABLE ONLY "public"."room_restrictions" ADD CONSTRAINT "room_restrictions_rooms_id_fk" FOREIGN KEY (room_id) REFERENCES rooms(id) ON UPDATE CASCADE ON DELETE CASCADE NOT DEFERRABLE;

10
sqlc.yml Normal file
View File

@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
queries: "sql/query.sql"
schema: "sql/schema.sql"
gen:
go:
package: "db"
out: "internal/repository/db"
sql_package: "pgx/v5"