diff --git a/backend/TODO b/backend/TODO deleted file mode 100644 index 6b517e3..0000000 --- a/backend/TODO +++ /dev/null @@ -1 +0,0 @@ -[] - Check contenu json \ No newline at end of file diff --git a/backend/db.go b/backend/db.go index 0787803..9e252e6 100644 --- a/backend/db.go +++ b/backend/db.go @@ -2,13 +2,30 @@ package main import ( "fmt" - "os" + "github.com/Molaryy/bective/types" + "github.com/Molaryy/bective/utils" + "gorm.io/driver/postgres" + "gorm.io/gorm" + "log" ) -func InitDb() { - dbUsr := os.Getenv("POSTGRES_USER") - dbURL := "postgres://pg:pass@localhost:5432/crud" +func getDbUrl() string { + dbUsr := utils.GetEnvFileValue("../.env", "POSTGRES_USER") + dbHost := utils.GetEnvFileValue("../.env", "POSTGRES_HOST") + dbPassword := utils.GetEnvFileValue("../.env", "POSTGRES_PASSWORD") + dbName := utils.GetEnvFileValue("../.env", "POSTGRES_DB") - fmt.Println(dbUsr) - fmt.Println(dbURL) + return fmt.Sprintf("postgres://%s:%s@%s:5432/%s", dbUsr, dbPassword, dbHost, dbName) +} + +func InitDb() *gorm.DB { + dbURL := getDbUrl() + db, err := gorm.Open(postgres.Open(dbURL), &gorm.Config{}) + + if err != nil { + log.Fatalln(err) + } + + utils.CheckError(db.AutoMigrate(&types.Todo{})) + return db } diff --git a/backend/go.mod b/backend/go.mod index a10c314..ef446ee 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -1,4 +1,4 @@ -module bective +module github.com/Molaryy/bective go 1.21.0 diff --git a/backend/auth.go b/backend/middlewares/auth.go similarity index 89% rename from backend/auth.go rename to backend/middlewares/auth.go index 9895a96..c85927c 100644 --- a/backend/auth.go +++ b/backend/middlewares/auth.go @@ -1,4 +1,4 @@ -package main +package middlewares import ( "github.com/gin-gonic/gin" diff --git a/backend/router.go b/backend/router.go index 1e3de71..1fbdab4 100644 --- a/backend/router.go +++ b/backend/router.go @@ -1,7 +1,8 @@ package main import ( - "bective/types" + "github.com/Molaryy/bective/middlewares" + "github.com/Molaryy/bective/types" "github.com/gin-gonic/gin" "net/http" ) @@ -18,7 +19,7 @@ func todosHandler(r *gin.Engine) { func router(r *gin.Engine) { - r.POST("/auth", AuthController) + r.POST("/auth", middlewares.AuthController) todosHandler(r) r.NoRoute(RouteNotFound) } diff --git a/backend/types/handlerTodos.go b/backend/types/handlerTodos.go new file mode 100644 index 0000000..7475722 --- /dev/null +++ b/backend/types/handlerTodos.go @@ -0,0 +1,11 @@ +package types + +import "gorm.io/gorm" + +type TodoHandler struct { + DB *gorm.DB +} + +func NewTodo(db *gorm.DB) TodoHandler { + return TodoHandler{db} +} diff --git a/backend/types/todos.go b/backend/types/todos.go index f406c7e..8f47e22 100644 --- a/backend/types/todos.go +++ b/backend/types/todos.go @@ -1,9 +1,8 @@ package types import ( - "bective/utils" "encoding/json" - "fmt" + "github.com/Molaryy/bective/utils" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "net/http" @@ -13,9 +12,11 @@ import ( var todos Todos type Todo struct { - Id int `json:"id"` + Id int `json:"id" gorm:"primaryKey"` Title string `json:"title"` Description string `json:"description"` + StartData string `json:"startData"` + EndData string `json:"endData"` } type Todos struct { @@ -28,19 +29,11 @@ func (t *Todo) GetTodos(c *gin.Context) { c.JSON(http.StatusOK, todos.FileToTodos(fileBytes)) } -func (t *Todo) CreateTodo(c *gin.Context) { +func (h TodoHandler) CreateTodo(c *gin.Context) { fileBytes := utils.JsonToBytes("data/data.json") statusCode := http.StatusOK message := "You just created a todo" var todo Todo - file, err := os.OpenFile("data/data.json", os.O_WRONLY, 4) - var todosBytes []byte - - defer file.Close() - if err != nil { - statusCode = http.StatusServiceUnavailable - message = "Couldn't access data" - } if len(todos.Todos) == 0 { utils.CheckError(json.Unmarshal(fileBytes, &todos)) @@ -52,13 +45,9 @@ func (t *Todo) CreateTodo(c *gin.Context) { } todos.Todos = append(todos.Todos, todo) - todosBytes, errJSON := json.Marshal(todos) - if errJSON != nil { - statusCode = http.StatusServiceUnavailable - message = "Couldn't access data" - } - fmt.Println(todos.Todos) - file.Write(todosBytes) + + file, _ := json.MarshalIndent(todos.Todos, "", "") + utils.CheckError(os.WriteFile("data/data.json", file, 0644)) c.JSON(statusCode, gin.H{ "message": message, diff --git a/backend/utils/get-env-value.go b/backend/utils/get-env-value.go new file mode 100644 index 0000000..7a7fe45 --- /dev/null +++ b/backend/utils/get-env-value.go @@ -0,0 +1,29 @@ +package utils + +import ( + "log" + "os" + "strings" +) + +func GetEnvFileValue(envPath string, key string) string { + var valueToReturn string = "" + data, err := os.ReadFile(envPath) + + if err != nil { + panic(err) + } + arrayFile := strings.Split(string(data), "\n") + + for _, line := range arrayFile { + splitedString := strings.Split(line, "=") + + if len(splitedString) == 2 && splitedString[0] == key { + valueToReturn = strings.TrimSuffix(splitedString[1], "\n") + } + } + if valueToReturn == "" { + log.Fatal("Nothing found for the key ", key) + } + return valueToReturn +}