Initial commit
This commit is contained in:
Executable
+17
@@ -0,0 +1,17 @@
|
||||
clear
|
||||
|
||||
# Get dependencies
|
||||
# go mod tidy
|
||||
|
||||
# Build server
|
||||
go build .
|
||||
|
||||
# Delete previous database
|
||||
sudo rm ./SQLiteDatabase.db
|
||||
|
||||
# Create new database
|
||||
./cmd --new-schema
|
||||
|
||||
# Run server
|
||||
./cmd --host=127.0.0.1 --port=8080 --limit=10 --offset=0 --write-timeout=10 --read-timeout=10
|
||||
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
clear
|
||||
|
||||
# A 1 tables in Room 1
|
||||
curl -X POST -H 'Content-Type: application/json' -d '{"name":11,"max_people":1,"room":"Room 1"}' localhost:8080/table --silent
|
||||
# A 2 tables in Room 2
|
||||
curl -X POST -H 'Content-Type: application/json' -d '{"name":21,"max_people":2,"room":"Room 2"}' localhost:8080/table --silent
|
||||
curl -X POST -H 'Content-Type: application/json' -d '{"name":22,"max_people":4,"room":"Room 2"}' localhost:8080/table --silent
|
||||
# A 3 tables in Room 3
|
||||
curl -X POST -H 'Content-Type: application/json' -d '{"name":31,"max_people":3,"room":"Room 3"}' localhost:8080/table --silent
|
||||
curl -X POST -H 'Content-Type: application/json' -d '{"name":32,"max_people":5,"room":"Room 3"}' localhost:8080/table --silent
|
||||
curl -X POST -H 'Content-Type: application/json' -d '{"name":33,"max_people":6,"room":"Room 3"}' localhost:8080/table --silent
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
clear
|
||||
|
||||
# We move the table with id 6 from room 3 to room 1, rename it to 12
|
||||
curl -X PUT -H 'Content-Type: application/json' -d '{"id":6,"name":12,"max_people":6,"room":"Room 1"}' localhost:8080/table --silent
|
||||
|
||||
# Now there are an equal number of tables in each room
|
||||
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
clear
|
||||
|
||||
# Get all record of Tables
|
||||
# curl -X GET -H 'Content-Type: application/json' localhost:8080/tables
|
||||
|
||||
# Get only one record of Table by id 6
|
||||
curl -X GET -H 'Content-Type: application/json' localhost:8080/table/6
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
clear
|
||||
|
||||
# They decided to remove one table from the establishment. It was a table with id 6
|
||||
curl -X DELETE localhost:8080/table/6 --silent
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
clear
|
||||
|
||||
# We will create the first booking for the very first table with a reservation for 1 hour
|
||||
curl -X POST -H 'Content-Type: application/json' -d '{"id_tables":1,"reservation":"2000-01-01T12:00:00Z","time_reserve":"01:00","who_booked":"Ivan 1"}' localhost:8080/booking --silent
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
clear
|
||||
|
||||
# Ivan warned that he did not have time and asked to postpone the reservation of the table an hour later, he also informed that the duration of the reservation should be increased in time
|
||||
curl -X PUT -H 'Content-Type: application/json' -d '{"id_tables":1,"reservation":"2000-01-01T13:00:00Z","time_reserve":"02:00","who_booked":"Ivan 1"}' localhost:8080/booking --silent
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
clear
|
||||
|
||||
# Get all record of Booking
|
||||
# curl -X GET -H 'Content-Type: application/json' localhost:8080/booking
|
||||
|
||||
# Get only one record of Booking by id 1
|
||||
curl -X GET -H 'Content-Type: application/json' localhost:8080/booking/1
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
clear
|
||||
|
||||
# Delete one record of Booking by id 1
|
||||
curl -X DELETE localhost:8080/booking/1 --silent
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
+125
@@ -0,0 +1,125 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gorilla/handlers"
|
||||
"go_project/internal/configuration"
|
||||
"go_project/internal/connection"
|
||||
"go_project/internal/controllers"
|
||||
"go_project/internal/routes"
|
||||
"go_project/internal/schema"
|
||||
"go_project/pkg/behaivor"
|
||||
"go_project/pkg/file"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
configuration.Init()
|
||||
|
||||
// Create database
|
||||
if configuration.GetForceCreateSchema() {
|
||||
sqlDBFileName := configuration.GetDatabaseLocation()
|
||||
file.MustCreate(sqlDBFileName)
|
||||
sqlHandler, err := connection.Open(sqlDBFileName)
|
||||
|
||||
behaivor.Anxiety("Database connection error", err)
|
||||
|
||||
schema.MustCreate(sqlHandler)
|
||||
|
||||
log.Println("Database created")
|
||||
|
||||
return
|
||||
} else if configuration.GetCreateSchema() {
|
||||
sqlDBFileName := configuration.GetDatabaseLocation()
|
||||
|
||||
if file.MustIsNotExist(sqlDBFileName) {
|
||||
file.MustCreate(sqlDBFileName)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
|
||||
sqlHandler, err := connection.Open(sqlDBFileName)
|
||||
|
||||
behaivor.Anxiety("Database connection error", err)
|
||||
|
||||
schema.MustCreate(sqlHandler)
|
||||
|
||||
log.Println("Database created")
|
||||
|
||||
return
|
||||
}
|
||||
// Create database end
|
||||
|
||||
// NOTE: Temp data for testing
|
||||
MustFillDatabase()
|
||||
|
||||
serverRouter := routes.DefaultRouter()
|
||||
|
||||
// Adding CORS policy
|
||||
corsHandler := handlers.CORS(
|
||||
handlers.AllowedOrigins([]string{"*"}), // Allow all origins
|
||||
handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}), // Allow common HTTP methods
|
||||
handlers.AllowedHeaders([]string{"Content-Type"}), // Allow specific headers
|
||||
)
|
||||
|
||||
serverHost := configuration.GetServerHost()
|
||||
serverPort := strconv.FormatInt(int64(configuration.GetServerPort()), 10)
|
||||
serverAddr := serverHost + ":" + serverPort
|
||||
serverWriteTimeout := configuration.GetWriteTimeout()
|
||||
serverReadTimeout := configuration.GetReadTimeout()
|
||||
|
||||
server := &http.Server{
|
||||
Handler: corsHandler(serverRouter),
|
||||
Addr: serverAddr,
|
||||
WriteTimeout: serverWriteTimeout,
|
||||
ReadTimeout: serverReadTimeout,
|
||||
}
|
||||
|
||||
log.Println("Preparations for listen and serve are complete. Starting server at " + serverAddr)
|
||||
|
||||
log.Fatal(server.ListenAndServe())
|
||||
}
|
||||
|
||||
// NOTE: Temp data for testing
|
||||
func MustFillDatabase() {
|
||||
sqlHandler, err := connection.OpenDefault()
|
||||
|
||||
behaivor.Anxiety("Database connection error", err)
|
||||
|
||||
log.Println("Database filling operations are started")
|
||||
defer connection.MustClose(sqlHandler)
|
||||
|
||||
// Fill database with Tables data
|
||||
for i := 1; i <= 5; i++ {
|
||||
createTableParams := controllers.CreateTableParams{
|
||||
Name: i,
|
||||
MaxPeople: 2,
|
||||
Room: "Room 1",
|
||||
}
|
||||
log.Printf("Current CreateTableParams: %v\n", createTableParams)
|
||||
_, err := controllers.CreateTable(sqlHandler, createTableParams)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
//Fill database with Bookings data
|
||||
for i := 0; i < 10; i++ {
|
||||
reservationTime := fmt.Sprintf("2025-01-01T%02d:00:00", 10+i)
|
||||
createBookingParams := controllers.CreateBookingParams{
|
||||
IdTables: i%5 + 1,
|
||||
Reservation: reservationTime,
|
||||
TimeReserve: "01:00",
|
||||
WhoBooked: "John Doe",
|
||||
}
|
||||
log.Printf("Current CreateBookingParams: %v\n", createBookingParams)
|
||||
_, err := controllers.CreateBooking(sqlHandler, createBookingParams)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("Database filling operations are completed")
|
||||
}
|
||||
Reference in New Issue
Block a user