Skip to content

Commit

Permalink
Merge pull request #5 from Molaryy/feat/create-migrations-postgres
Browse files Browse the repository at this point in the history
feat: added connection to postreSQL
  • Loading branch information
Molaryy authored Oct 25, 2023
2 parents 590b2b7 + ade8a16 commit 6ac1450
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 30 deletions.
1 change: 0 additions & 1 deletion backend/TODO

This file was deleted.

29 changes: 23 additions & 6 deletions backend/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module bective
module github.com/Molaryy/bective

go 1.21.0

Expand Down
2 changes: 1 addition & 1 deletion backend/auth.go → backend/middlewares/auth.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package middlewares

import (
"github.com/gin-gonic/gin"
Expand Down
5 changes: 3 additions & 2 deletions backend/router.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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)
}
11 changes: 11 additions & 0 deletions backend/types/handlerTodos.go
Original file line number Diff line number Diff line change
@@ -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}
}
27 changes: 8 additions & 19 deletions backend/types/todos.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 {
Expand All @@ -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))
Expand All @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions backend/utils/get-env-value.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6ac1450

Please sign in to comment.