mirror of
https://github.com/LeRoid-hub/Bookholder-API.git
synced 2025-01-31 02:34:57 +00:00
Creates the DB Tables when a new DB is created
This commit is contained in:
parent
7302b77915
commit
d360d52f01
33
database/bookholder.sql
Normal file
33
database/bookholder.sql
Normal file
@ -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;
|
@ -3,6 +3,8 @@ package database
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
_ "github.com/jackc/pgx/v5/stdlib"
|
_ "github.com/jackc/pgx/v5/stdlib"
|
||||||
)
|
)
|
||||||
@ -19,6 +21,10 @@ var (
|
|||||||
database DB
|
database DB
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
SqlFile = "./database/bookholder.sql"
|
||||||
|
)
|
||||||
|
|
||||||
func SetEnv(env map[string]string) *DB {
|
func SetEnv(env map[string]string) *DB {
|
||||||
var db DB
|
var db DB
|
||||||
|
|
||||||
@ -44,7 +50,7 @@ func New() (*sql.DB, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func connect() string {
|
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"
|
//return "user=" + database.User + " password=" + database.Password + " host=" + database.Host + " dbname=" + database.Name + " sslmode=disable"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,6 +82,8 @@ func createDatabase() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createTables()
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkTables() {
|
func checkTables() {
|
||||||
@ -83,6 +91,27 @@ func checkTables() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createTables() {
|
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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -156,7 +185,7 @@ func NewTransaction(database *sql.DB, transaction Transaction) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UpdateTransaction(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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -175,7 +204,7 @@ func DeleteTransaction(database *sql.DB, id int) error {
|
|||||||
|
|
||||||
func GetTransaction(database *sql.DB, id int) (Transaction, error) {
|
func GetTransaction(database *sql.DB, id int) (Transaction, error) {
|
||||||
var transaction Transaction
|
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 {
|
if err != nil {
|
||||||
return transaction, err
|
return transaction, err
|
||||||
}
|
}
|
||||||
@ -204,7 +233,7 @@ func GetTransactions(database *sql.DB, account int, year int, month int) ([]Tran
|
|||||||
|
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
var transaction Transaction
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ type Transaction struct {
|
|||||||
Debit bool
|
Debit bool
|
||||||
OffsetAccount uint
|
OffsetAccount uint
|
||||||
Account uint
|
Account uint
|
||||||
Time time.Time
|
Date time.Time
|
||||||
Description string
|
Description string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,8 +41,13 @@ func Run(env map[string]string, db *database.DB) {
|
|||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
|
r.GET("/", welcome)
|
||||||
|
|
||||||
v1 := r.Group("/v1")
|
v1 := r.Group("/v1")
|
||||||
{
|
{
|
||||||
|
//Welcome
|
||||||
|
v1.GET("/", welcome)
|
||||||
|
|
||||||
//Account
|
//Account
|
||||||
v1.GET("/Account/:AccountID", checkAuth, getAccount)
|
v1.GET("/Account/:AccountID", checkAuth, getAccount)
|
||||||
v1.POST("/NewAccount", checkAuth, newAccount)
|
v1.POST("/NewAccount", checkAuth, newAccount)
|
||||||
@ -77,3 +82,9 @@ func Run(env map[string]string, db *database.DB) {
|
|||||||
r.Run(":8080")
|
r.Run(":8080")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func welcome(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"message": "Welcome to Bookholder API",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user