Added devices and companies models #3

Merged
duplingnedim merged 2 commits from setting-up-user into main 2023-09-11 07:55:04 +02:00
10 changed files with 116 additions and 21 deletions
Showing only changes of commit 40c6366608 - Show all commits

13
.gitignore vendored Normal file
View File

@@ -0,0 +1,13 @@
#configuration
.env
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
debug
.vscode
.vs/
.DS_Store

View File

@@ -0,0 +1,19 @@
package controllers
// import (
// "net/http"
// models "novatech/models"
// "github.com/VoidArtanis/go-rest-boilerplate/models"
// "github.com/gin-gonic/gin"
// )
// func Companies(c *gin.Context) {
// var companies []models.Company
// // Fetch companies from DB here
// if err := models.FetchCompanies(&companies); err != nil {
// c.JSON(http.StatusInternalServerError, gin.H{"error": "Error fetching companies"})
// return
// }
// c.JSON(http.StatusOK, gin.H{"companies": companies})
// }

BIN
demo.db

Binary file not shown.

11
main.go
View File

@@ -26,13 +26,22 @@ func main() {
}
// Db Connect and Close
shared.Init()
if shared.Init() != nil {
panic("Failed to connect to database")
}
defer shared.CloseDb()
// Initialize Admin interface
Admin := admin.New(&admin.AdminConfig{DB: shared.GetDb()})
fmt.Printf("Admin instance: %+v\n", Admin)
// Allow Admin to manage User resource
company := Admin.AddResource(&models.Company{})
company.Meta(&admin.Meta{Name: "Users", Config: &admin.SelectManyConfig{SelectMode: "bottom_sheet"}})
company.Meta(&admin.Meta{Name: "Devices", Config: &admin.SelectManyConfig{SelectMode: "bottom_sheet"}})
// Add User and Device resources
Admin.AddResource(&models.User{})
Admin.AddResource(&models.Device{})
// Initialize HTTP request multiplexer
mux := http.NewServeMux()
// Mount admin interface to mux

33
models/company.go Normal file
View File

@@ -0,0 +1,33 @@
package models
import "github.com/jinzhu/gorm"
type Company struct {
gorm.Model
Name string
Password string
Email string
Avatar string
Users []User
Devices []Device
}
// func FetchCompanies(companies *[]Company) (err error) {
// db := gorm.GetDb()
// if err = db.Find(companies).Error; err != nil {
// return err
// }
// return nil
// }
func (Company)Update() (bool, error) {
return false, nil
}
func (Company)Create() (bool, error) {
return false, nil
}
func (Company)Delete() (bool, error) {
return false, nil
}

View File

@@ -1,7 +1,3 @@
/**
* Created by VoidArtanis on 10/24/2017
*/
package models
type SimpleCRUD interface {

20
models/device.go Normal file
View File

@@ -0,0 +1,20 @@
package models
import "github.com/jinzhu/gorm"
type Device struct {
gorm.Model
DeviceName string
DeviceConfiguration string `gorm:"type:json"`
CompanyID uint
}
func (Device)Update() (bool, error) {
return false, nil
}
func (Device)Create() (bool, error) {
return false, nil
}
func (Device)Delete() (bool, error) {
return false, nil
}

View File

@@ -1,7 +1,3 @@
/**
* Created by desha on 10/24/2017
*/
package models
import "github.com/jinzhu/gorm"
@@ -12,14 +8,15 @@ type User struct {
Password string
Email string
Avatar string
CompanyID uint
}
func (this User)Update() (bool, error) {
func (User)Update() (bool, error) {
return false, nil
}
func (this User)Create() (bool, error) {
func (User)Create() (bool, error) {
return false, nil
}
func (this User)Delete() (bool, error) {
func (User)Delete() (bool, error) {
return false, nil
}

View File

@@ -5,12 +5,13 @@
package routes
import (
"github.com/gin-gonic/gin"
"github.com/VoidArtanis/go-rest-boilerplate/controllers"
"github.com/gin-gonic/gin"
)
func RegisterPublicRoutes(r *gin.Engine){
r.GET("/publicmessage", controllers.GetPublicText)
// r.GET("/companies", controllers.Ge)
}

View File

@@ -2,7 +2,9 @@ package shared
import (
"fmt"
"log"
"novatech/config"
"novatech/models"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
@@ -13,23 +15,28 @@ var db *gorm.DB
var err error
func Init() {
func Init() error{
host := config.AppConfig.Database.HostName
user := config.AppConfig.Database.UserName
port := config.AppConfig.Database.Port
// port := config.AppConfig.Database.Port
dbName := config.AppConfig.Database.DatabaseName
password := config.AppConfig.Database.Password
dbString:= fmt.Sprintf("postgres, host=%s:%s user=%s dbname=%s sslmode=disable password=%s",host,port,user,dbName,password)
//PostgreSQL
db, err = gorm.Open(dbString)
dbString:= fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%s",host,user,dbName,password)
// db, err = gorm.Open("postgres", "host=localhost user=postgres dbname=postgres sslmode=disable password=root")
var err error
// //PostgreSQL
db, err = gorm.Open("postgres",dbString)
if err != nil {
fmt.Println(err)
log.Println("Error initializing the database: ", err)
return err
}
//TODO AUTOMIGRATE models once we have them
//db.AutoMigrate(&models.Person{})
db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{})
return nil
}