Init commit. DB, Connection, Create ProductTable

This commit is contained in:
Kameleoonik 2018-12-19 23:42:09 +01:00
commit e01312c15e
6 changed files with 90 additions and 0 deletions

BIN
ZPRP_Rental Executable file

Binary file not shown.

31
db/main.go Normal file
View File

@ -0,0 +1,31 @@
package db
import (
"log"
"os"
pg "github.com/go-pg/pg"
)
func Connect() {
opts := &pg.Options{
User: "postgres",
Password: "",
Addr: "localhost:5432",
Database: "myDB",
}
var db *pg.DB = pg.Connect(opts)
if db == nil {
log.Printf("Fail onnect")
os.Exit(100)
}
log.Printf("Connected :)")
CreateProductItemsTable(db)
closeErr := db.Close()
if closeErr != nil {
log.Printf("Error with close connection", closeErr)
os.Exit(100)
}
log.Printf("Connection closed")
return
}

39
db/product.go Normal file
View File

@ -0,0 +1,39 @@
package db
import(
"log"
"time"
orm "github.com/go-pg/pg/orm"
pg "github.com/go-pg/pg"
)
type ProductItem struct {
RefPointer int `sql:"-"`
tableName struct{} `sql:"product_items_collection"`
ID int `sql:"id,pk"`
Name string `sql:"name,unique"`
Desc string `sql:"desc"`
Image string `sql:"image"`
Price float64 `sql:"price,type:real"`
Features struct {
Name string
Desc string
Imp int
} `sql:"features,type:jsonb"`
CreateAt time.Time `sql:"created_at"`
UpdateAt time.Time `sql:"update_at"`
IsActive bool `sql:"is_active"`
}
func CreateProductItemsTable(db *pg.DB) error {
opts := &orm.CreateTableOptions{
IfNotExists: true,
}
createErr := db.CreateTable(&ProductItem{}, opts)
if createErr != nil {
log.Printf("Blad z dodaniem productItem, Reason: %v\n", createErr)
return createErr
}
log.Printf("Table ProductItems utworzone. \n")
return nil
}

6
glide.lock generated Normal file
View File

@ -0,0 +1,6 @@
hash: a6505100564a0f9d000c6777b4c353a643016a03db7fce6477b29faf64a8be0f
updated: 2018-12-19T16:30:49.538935+01:00
imports:
- name: github.com/go-pg/pg
version: ae5d5e7df4b2e598390e10b66b849c6af94f092b
testImports: []

4
glide.yaml Normal file
View File

@ -0,0 +1,4 @@
package: .
import:
- package: github.com/go-pg/pg
version: v6.15.1

10
main.go Normal file
View File

@ -0,0 +1,10 @@
package main
import (
"log"
db "./db"
)
func main() {
log.Printf("Yelo!\n")
db.Connect()
}