diff --git a/database/bookholder.sql b/database/bookholder.sql new file mode 100644 index 0000000..e9b31f5 --- /dev/null +++ b/database/bookholder.sql @@ -0,0 +1,33 @@ +CREATE TABLE accounts ( + name "char" NOT NULL, + kind "char" NOT NULL, + id integer NOT NULL +); + +CREATE TABLE transaction ( + id uuid NOT NULL, + amount double precision NOT NULL, + debit boolean NOT NULL, + offset_account integer NOT NULL, + account integer NOT NULL, + date timestamp without time zone NOT NULL, + description "char" +); + +CREATE TABLE users ( + name "char" NOT NULL, + password "char" NOT NULL, + id integer +); + +ALTER TABLE ONLY accounts + ADD CONSTRAINT accounts_pkey PRIMARY KEY (id); + +ALTER TABLE ONLY transaction + ADD CONSTRAINT transaction_pkey PRIMARY KEY (id); + +ALTER TABLE ONLY transaction + ADD CONSTRAINT "Account" FOREIGN KEY (account) REFERENCES accounts(id) NOT VALID; + +ALTER TABLE ONLY transaction + ADD CONSTRAINT "Offset" FOREIGN KEY (offset_account) REFERENCES accounts(id) NOT VALID; \ No newline at end of file diff --git a/database/database.go b/database/database.go index 3b9855b..41fb32e 100644 --- a/database/database.go +++ b/database/database.go @@ -3,6 +3,8 @@ package database import ( "database/sql" "errors" + "os" + "strings" _ "github.com/jackc/pgx/v5/stdlib" ) @@ -19,6 +21,10 @@ var ( database DB ) +const ( + SqlFile = "./database/bookholder.sql" +) + func SetEnv(env map[string]string) *DB { var db DB @@ -44,7 +50,7 @@ func New() (*sql.DB, error) { } func connect() string { - return "postgres://" + database.User + ":" + database.Password + "@" + database.Host + ":" + database.Host + "/" + database.Name + "?sslmode=disable" + return "postgres://" + database.User + ":" + database.Password + "@" + database.Host + ":" + database.Port + "/" + database.Name + "?sslmode=disable" //return "user=" + database.User + " password=" + database.Password + " host=" + database.Host + " dbname=" + database.Name + " sslmode=disable" } @@ -76,6 +82,8 @@ func createDatabase() { if err != nil { panic(err) } + + createTables() } func checkTables() { @@ -83,6 +91,27 @@ func checkTables() { } func createTables() { + sqlFile, err := os.ReadFile(SqlFile) + if err != nil { + panic(err) + } + + sqlStatements := strings.Split(string(sqlFile), ";") + + conn, err := sql.Open("pgx", connect()) + + if err != nil { + panic(err) + } + + defer conn.Close() + + for _, statement := range sqlStatements { + _, err := conn.Exec(statement) + if err != nil { + panic(err) + } + } } @@ -147,7 +176,7 @@ func GetAccount(database *sql.DB, id int) (Account, error) { } func NewTransaction(database *sql.DB, transaction Transaction) error { - _, err := database.Exec("INSERT INTO transactions (amount, debit, offset_account, account, time, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction.Amount, transaction.Debit, transaction.OffsetAccount, transaction.Account, transaction.Time, transaction.Description) + _, err := database.Exec("INSERT INTO transactions (amount, debit, offset_account, account, time, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction.Amount, transaction.Debit, transaction.OffsetAccount, transaction.Account, transaction.Date, transaction.Description) if err != nil { return err } @@ -156,7 +185,7 @@ func NewTransaction(database *sql.DB, transaction Transaction) error { } func UpdateTransaction(database *sql.DB, transaction Transaction) error { - _, err := database.Exec("UPDATE transactions SET amount = $1, debit = $2, offset_account = $3, account = $4, time = $5, description = $6 WHERE id = $7", transaction.Amount, transaction.Debit, transaction.OffsetAccount, transaction.Account, transaction.Time, transaction.Description, transaction.ID) + _, err := database.Exec("UPDATE transactions SET amount = $1, debit = $2, offset_account = $3, account = $4, time = $5, description = $6 WHERE id = $7", transaction.Amount, transaction.Debit, transaction.OffsetAccount, transaction.Account, transaction.Date, transaction.Description, transaction.ID) if err != nil { return err } @@ -175,7 +204,7 @@ func DeleteTransaction(database *sql.DB, id int) error { func GetTransaction(database *sql.DB, id int) (Transaction, error) { var transaction Transaction - err := database.QueryRow("SELECT * FROM transactions WHERE id = $1", id).Scan(&transaction.ID, &transaction.Amount, &transaction.Debit, &transaction.OffsetAccount, &transaction.Account, &transaction.Time, &transaction.Description) + err := database.QueryRow("SELECT * FROM transactions WHERE id = $1", id).Scan(&transaction.ID, &transaction.Amount, &transaction.Debit, &transaction.OffsetAccount, &transaction.Account, &transaction.Date, &transaction.Description) if err != nil { return transaction, err } @@ -204,7 +233,7 @@ func GetTransactions(database *sql.DB, account int, year int, month int) ([]Tran for row.Next() { var transaction Transaction - err := row.Scan(&transaction.ID, &transaction.Amount, &transaction.Debit, &transaction.OffsetAccount, &transaction.Account, &transaction.Time, &transaction.Description) + err := row.Scan(&transaction.ID, &transaction.Amount, &transaction.Debit, &transaction.OffsetAccount, &transaction.Account, &transaction.Date, &transaction.Description) if err != nil { return nil, err } diff --git a/database/models.go b/database/models.go index c4e9294..b0a5f70 100644 --- a/database/models.go +++ b/database/models.go @@ -14,7 +14,7 @@ type Transaction struct { Debit bool OffsetAccount uint Account uint - Time time.Time + Date time.Time Description string } diff --git a/server/server.go b/server/server.go index d5dad9f..ce387f0 100644 --- a/server/server.go +++ b/server/server.go @@ -41,8 +41,13 @@ func Run(env map[string]string, db *database.DB) { r := gin.Default() + r.GET("/", welcome) + v1 := r.Group("/v1") { + //Welcome + v1.GET("/", welcome) + //Account v1.GET("/Account/:AccountID", checkAuth, getAccount) v1.POST("/NewAccount", checkAuth, newAccount) @@ -77,3 +82,9 @@ func Run(env map[string]string, db *database.DB) { r.Run(":8080") } } + +func welcome(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "Welcome to Bookholder API", + }) +}