This commit is contained in:
pakintada@gmail.com 2023-12-07 11:09:20 +07:00
commit 96f341aa28
26 changed files with 1068 additions and 664 deletions

View file

@ -5,7 +5,6 @@ import (
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"go.uber.org/zap"
"crypto/rand"
"encoding/hex"
@ -39,7 +38,7 @@ func HashCommit(n int) (string, error) {
byt := make([]byte, n)
_, err := rand.Read(byt)
if err != nil {
Log.Debug("Error when hashing commit", zap.Error(err))
return "", err
}
return hex.EncodeToString(byt), nil
@ -48,14 +47,14 @@ func HashCommit(n int) (string, error) {
func Insert(c *CommitLog) error {
commit_db, err := sqlx.Connect("sqlite3", "./data/database.db")
if err != nil {
Log.Fatal("Error when connecting to database", zap.Error(err))
}
// init table in db
commit_db.MustExec(schema)
_, err = commit_db.NamedExec("INSERT INTO commit_log (id, msg, created_at, editor, change_file) VALUES (:id, :msg, :created_at, :editor, :change_file)", c)
if err != nil {
Log.Error("Error when insert commit log", zap.Error(err))
return err
}
@ -74,11 +73,9 @@ func GetCommitLogOfFilename(countryId string, filename string) ([]CommitLog, err
filename = filenameParts[1] + "_" + filenameParts[2]
}
Log.Debug("CommitDB", zap.Any("lookup", filename))
commitDB, err := sqlx.Connect("sqlite3", "./data/database.db")
if err != nil {
Log.Fatal("Error when connecting to database", zap.Error(err))
}
var commits []CommitLog
@ -96,7 +93,6 @@ func GetCommitLogOfFilename(countryId string, filename string) ([]CommitLog, err
}
if err != nil {
Log.Error("Error when get commit log", zap.Error(err))
return nil, err
}
@ -106,14 +102,14 @@ func GetCommitLogOfFilename(countryId string, filename string) ([]CommitLog, err
func GetCommitLogs() ([]CommitLog, error) {
commit_db, err := sqlx.Connect("sqlite3", "./data/database.db")
if err != nil {
Log.Fatal("Error when connecting to database", zap.Error(err))
}
var commits []CommitLog
err = commit_db.Get(&commits, "SELECT * FROM commit_log", nil)
if err != nil {
Log.Error("Error when get commit log", zap.Error(err))
return nil, err
}

View file

@ -1,7 +1,6 @@
package data
import (
"encoding/json"
"fmt"
"log"
"recipe-manager/helpers"
@ -12,10 +11,6 @@ import (
"go.uber.org/zap"
)
var (
Log = logger.GetInstance()
)
type RecipeWithTimeStamps struct {
Recipe models.Recipe
TimeStamps int64
@ -28,9 +23,10 @@ type Data struct {
currentRecipe *models.Recipe
recipeMap map[string]RecipeWithTimeStamps
Countries []helpers.CountryName
taoLogger *logger.TaoLogger
}
func NewData() *Data {
func NewData(taoLogger *logger.TaoLogger) *Data {
countries := []helpers.CountryName{{
CountryID: "tha",
@ -66,6 +62,7 @@ func NewData() *Data {
},
},
Countries: countries,
taoLogger: taoLogger,
}
}
@ -91,7 +88,7 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
recipe, err := helpers.ReadRecipeFile(countryID, filename)
if err != nil {
logger.GetInstance().Error("Error when read recipe file [GetRecipe]", zap.Error(err))
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe
}
@ -148,7 +145,7 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
recipe, err := helpers.ReadRecipeFile(countryID, filename)
if err != nil {
logger.GetInstance().Error("Error when read recipe file [GetRecipe01ByProductCode]", zap.Error(err))
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
for _, v := range d.currentRecipe.Recipe01 {
if v.ProductCode == productCode {
return v, nil
@ -222,7 +219,7 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
recipe, err := helpers.ReadRecipeFile(countryID, filename)
if err != nil {
logger.GetInstance().Error("Error when read recipe file [GetMaterialSetting]", zap.Error(err))
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
copy(result, d.currentRecipe.MaterialSetting)
return result
}
@ -266,7 +263,7 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
recipe, err := helpers.ReadRecipeFile(countryID, filename)
if err != nil {
logger.GetInstance().Error("Error when read recipe file [GetMaterialCode]", zap.Error(err))
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe.MaterialCode
}
@ -332,13 +329,3 @@ func (d *Data) GetCountryIDByName(countryName string) (string, error) {
}
return "", fmt.Errorf("country name: %s not found", countryName)
}
func (d *Data) ExportToJSON() []byte {
b_recipe, err := json.Marshal(d.currentRecipe)
if err != nil {
Log.Error("Error when marshal recipe", zap.Error(err))
return nil
}
return b_recipe
}

View file

@ -0,0 +1 @@
DROP TABLE IF EXISTS users;

View file

@ -1,10 +1,10 @@
-- slqlite3
-- create users table
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
email TEXT NOT NULL,
password TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
email TEXT UNIQUE NOT NULL,
permissions INT DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

View file

@ -0,0 +1 @@
ALTER TABLE users DROP COLUMN picture;

View file

@ -0,0 +1 @@
ALTER TABLE users ADD COLUMN picture TEXT;

View file

@ -1,6 +1,9 @@
package data
import "github.com/jmoiron/sqlx"
import (
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
func NewSqliteDatabase() *sqlx.DB {
db := sqlx.MustConnect("sqlite3", "./data/database.db")