Add User route and Refactor code

This commit is contained in:
Kenta420 2023-12-06 20:21:25 +07:00
parent 519749fd3a
commit b311a41dc7
24 changed files with 902 additions and 489 deletions

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", zap.Error(err))
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe
}
@ -144,7 +141,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", 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
@ -216,7 +213,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", 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
}
@ -260,7 +257,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", zap.Error(err))
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe.MaterialCode
}
@ -326,13 +323,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
}

Binary file not shown.

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")