瀏覽代碼

Pull first SQL query into a struct

Setup structs for storing query information.
master
Immanuel Onyeka 1 年之前
父節點
當前提交
50f6682961
共有 8 個檔案被更改,包括 116 行新增4 行删除
  1. +1
    -0
      .gitignore
  2. +1
    -1
      assets/main.css
  3. +9
    -0
      config.default.go
  4. +5
    -0
      go.mod
  5. +2
    -0
      go.sum
  6. +2
    -2
      migrations/0_29092022_create_main_tables.sql
  7. +1
    -1
      migrations/reset.sql
  8. +95
    -0
      skouter.go

+ 1
- 0
.gitignore 查看文件

@@ -4,3 +4,4 @@ skouter
.env
assets/app.js
package-lock.json
config.go

+ 1
- 1
assets/main.css 查看文件

@@ -336,7 +336,7 @@ div.modal {
div.modal .form {
z-index: 5;
margin: auto auto;
margin-top: 20%;
margin-top: 10%;
width: 100%;
max-width: 300px;
padding: 20px 10px;


+ 9
- 0
config.default.go 查看文件

@@ -0,0 +1,9 @@
/*
package main

var config = map[string]string {
"dbUsername": "",
"dbPassword": "",
}
*/
package main

+ 5
- 0
go.mod 查看文件

@@ -0,0 +1,5 @@
module example.com/m

go 1.19

require github.com/go-sql-driver/mysql v1.6.0

+ 2
- 0
go.sum 查看文件

@@ -0,0 +1,2 @@
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=

+ 2
- 2
migrations/0_29092022_create_main_tables.sql 查看文件

@@ -97,7 +97,7 @@ CREATE TABLE estimate (
);

/* template = true fees are saved for users or branches. If template or default
* are true, estimate_id should be null. */
* are true, estimate_id should be null.*/
CREATE TABLE fee (
id INT AUTO_INCREMENT NOT NULL,
estimate_id INT,
@@ -125,7 +125,7 @@ CREATE TABLE fee_template (
name VARCHAR(30),
/* Group heading shown in report */
category VARCHAR(60),
auto BOOLEAN,
auto BOOLEAN NOT NULL,
/* If fee should be automatically applied */
PRIMARY KEY (`id`),
FOREIGN KEY (user_id) REFERENCES user(id),


+ 1
- 1
migrations/reset.sql 查看文件

@@ -1,7 +1,7 @@
DROP TABLE IF EXISTS comparison;
DROP TABLE IF EXISTS fee;
DROP TABLE IF EXISTS fee_template;
DROP TABLE IF EXISTS estimate;
DROP TABLE IF EXISTS comparison;
DROP TABLE IF EXISTS borrower;
DROP TABLE IF EXISTS loan_type;
DROP TABLE IF EXISTS license;


+ 95
- 0
skouter.go 查看文件

@@ -6,6 +6,9 @@ import (
"sync"
"regexp"
"html/template"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"fmt"
)

type Page struct {
@@ -14,6 +17,42 @@ type Page struct {
Name string
}

type LoanType struct {
user int
branch int
name string
}

type FeeTemplate struct {
id sql.NullInt64
user sql.NullInt64
branch sql.NullInt64
amount sql.NullInt64
perc sql.NullInt64
ftype sql.NullString
notes sql.NullString
name sql.NullString
category sql.NullString
auto bool
}

type Estimate struct {
user int
borrower int
comparison int
transaction int
loanType LoanType
loanAmount int
price int
property string
pud bool
term int
interest int
hoi int
miName string
miAmount int
}

var (
regexen = make(map[string]*regexp.Regexp)
relock sync.Mutex
@@ -52,18 +91,60 @@ func match(path, pattern string, args *[]string) bool {
relock.Lock()
defer relock.Unlock()
regex := regexen[pattern]

if regex == nil {
regex = regexp.MustCompile("^" + pattern + "$")
regexen[pattern] = regex
}

matches := regex.FindStringSubmatch(path)
if len(matches) <= 0 {
return false
}

*args = matches[1:]
return true
}

// Fetch fees from the database
func getFees(db *sql.DB, user int) ([]FeeTemplate, error) {
var fees []FeeTemplate

rows, err := db.Query("SELECT * FROM fee_template")
if err != nil {
return nil, fmt.Errorf("Fee error %v", err)
}

defer rows.Close()

for rows.Next() {
var fee FeeTemplate
// var fee = FeeTemplate{ ftype: map[string]uint8{
// "Government": 0, "Title": 1, "Required": 2, "Lender": 3, "Other": 4,
// }}

if err := rows.Scan(
&fee.id,
&fee.user,
&fee.branch,
&fee.amount,
&fee.perc,
&fee.ftype,
&fee.notes,
&fee.name,
&fee.category,
&fee.auto)
err != nil {
return nil, fmt.Errorf("The fees %q: %v", user, err)
}

fees = append(fees, fee)
}

fmt.Print("the fees: %v", fees)
return fees, nil
}

func route(w http.ResponseWriter, r *http.Request) {
var page Page
var args []string
@@ -88,6 +169,20 @@ func route(w http.ResponseWriter, r *http.Request) {

func main() {
files := http.FileServer(http.Dir(""))
db, err := sql.Open("mysql",
fmt.Sprintf( "%s:%s@tcp(127.0.0.1:3306)/skouter",
config["DBUser"],
config["DBPass"]))

err = db.Ping()
if err != nil {
// do something here like abort
print("Bad database configuration: %v", err)
panic(err)
// maybe os.Exit(1) instead
}

fmt.Print(getFees(db, 0))

http.Handle("/assets/", files)
http.HandleFunc("/", route)


Loading…
取消
儲存