Skip to content

Commit

Permalink
UPDATE - Adição das tabelas necessárias no setup do postgress
Browse files Browse the repository at this point in the history
  • Loading branch information
JvLetsBora committed Mar 28, 2024
1 parent 44a78a1 commit 33eb521
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 42 deletions.
24 changes: 0 additions & 24 deletions src/frontend/src/views/DefaultView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,3 @@ function apiData(){
</div>
</template>




<!-- {
"elements": [
{
"Name": "Grafico A",
"Index": 1,
"Value": "50vw"
},
{
"Name": "Grafico C",
"Index": 3,
"Value": "25vw"
},
{
"Name": "Grafico B",
"Index": 2,
"Value": "70vw"
}
],
"id": 1,
"status": "ok"
} -->
17 changes: 8 additions & 9 deletions src/server/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
)

type RegisterInput struct {
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required"`
Name string `json:"name" validate:"required"`
Directorate int `json:"directorate" validate:"required"`
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required"`
Name string `json:"name" validate:"required"`
}

type LoginInput struct {
Expand Down Expand Up @@ -74,13 +73,13 @@ func Register(c *gin.Context, pg *gorm.DB) {
hashedPassword := HashPassword(input.Password)

u := db.User{
Email: input.Email,
Password: hashedPassword,
Name: input.Name,
DirectorateRefer: input.Directorate,
Email: input.Email,
Password: hashedPassword,
Name: input.Name,
}

u.SaveUser(pg, &u)
user, _ := u.SaveUser(pg, &u)
println(user)

token, _ := GenerateToken(uint(u.ID), u.Role)

Expand Down
66 changes: 57 additions & 9 deletions src/server/db/setupPostgres.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package db

import (
"fmt"

"gorm.io/driver/postgres"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -54,21 +56,67 @@ type Sensor struct {
CoordY float32
}

func SetupPostgres() *gorm.DB {
func SetupPostgres() (*gorm.DB, error) {

dsn := "host=postgres user=username password=password dbname=postgres port=5432 sslmode=disable TimeZone=America/Sao_Paulo"

db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
return nil, err
}

err = db.AutoMigrate(&Sensor{})
if err != nil {
return nil, err
}
err = db.AutoMigrate(&Props{})
if err != nil {
return nil, err
}
err = db.AutoMigrate(&Elements{})
if err != nil {
return nil, err
}
err = db.AutoMigrate(&Layout{})
if err != nil {
return nil, err
}
err = db.AutoMigrate(&Directorate{})
if err != nil {
return nil, err
}
err = db.AutoMigrate(&User{})
if err != nil {
return nil, err
}

directorate := Directorate{
ID: 1,
Directorate: "Default",
}
db.Create(&directorate)

props := Props{
ID: 1,
Value: "w-full",
}
db.Create(&props)

elements := [2]Elements{
{
ID: 1,
Name: "MainChart",
PropsRefer: 1,
},
{
ID: 2,
Name: "HeatMap",
PropsRefer: 1,
},
}
db.Create(&elements)

db.AutoMigrate(&Sensor{})
db.AutoMigrate(&Props{})
db.AutoMigrate(&Elements{})
db.AutoMigrate(&Layout{})
db.AutoMigrate(&Directorate{})
db.AutoMigrate(&User{})
fmt.Println("Inicialização do banco de dados concluída")

return db
return db, nil
}

0 comments on commit 33eb521

Please sign in to comment.