Added devices and companies models
This commit is contained in:
13
.gitignore
vendored
Normal file
13
.gitignore
vendored
Normal 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
|
||||||
19
controllers/CompaniesController.go
Normal file
19
controllers/CompaniesController.go
Normal 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})
|
||||||
|
// }
|
||||||
11
main.go
11
main.go
@@ -26,13 +26,22 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Db Connect and Close
|
// Db Connect and Close
|
||||||
shared.Init()
|
if shared.Init() != nil {
|
||||||
|
panic("Failed to connect to database")
|
||||||
|
}
|
||||||
defer shared.CloseDb()
|
defer shared.CloseDb()
|
||||||
|
|
||||||
// Initialize Admin interface
|
// Initialize Admin interface
|
||||||
Admin := admin.New(&admin.AdminConfig{DB: shared.GetDb()})
|
Admin := admin.New(&admin.AdminConfig{DB: shared.GetDb()})
|
||||||
|
fmt.Printf("Admin instance: %+v\n", Admin)
|
||||||
// Allow Admin to manage User resource
|
// 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.User{})
|
||||||
|
Admin.AddResource(&models.Device{})
|
||||||
// Initialize HTTP request multiplexer
|
// Initialize HTTP request multiplexer
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
// Mount admin interface to mux
|
// Mount admin interface to mux
|
||||||
|
|||||||
33
models/company.go
Normal file
33
models/company.go
Normal 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
|
||||||
|
}
|
||||||
@@ -1,7 +1,3 @@
|
|||||||
/**
|
|
||||||
* Created by VoidArtanis on 10/24/2017
|
|
||||||
*/
|
|
||||||
|
|
||||||
package models
|
package models
|
||||||
|
|
||||||
type SimpleCRUD interface {
|
type SimpleCRUD interface {
|
||||||
|
|||||||
20
models/device.go
Normal file
20
models/device.go
Normal 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
|
||||||
|
}
|
||||||
@@ -1,7 +1,3 @@
|
|||||||
/**
|
|
||||||
* Created by desha on 10/24/2017
|
|
||||||
*/
|
|
||||||
|
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import "github.com/jinzhu/gorm"
|
import "github.com/jinzhu/gorm"
|
||||||
@@ -12,14 +8,15 @@ type User struct {
|
|||||||
Password string
|
Password string
|
||||||
Email string
|
Email string
|
||||||
Avatar string
|
Avatar string
|
||||||
|
CompanyID uint
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this User)Update() (bool, error) {
|
func (User)Update() (bool, error) {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
func (this User)Create() (bool, error) {
|
func (User)Create() (bool, error) {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
func (this User)Delete() (bool, error) {
|
func (User)Delete() (bool, error) {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,13 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/VoidArtanis/go-rest-boilerplate/controllers"
|
"github.com/VoidArtanis/go-rest-boilerplate/controllers"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterPublicRoutes(r *gin.Engine){
|
func RegisterPublicRoutes(r *gin.Engine){
|
||||||
|
|
||||||
r.GET("/publicmessage", controllers.GetPublicText)
|
r.GET("/publicmessage", controllers.GetPublicText)
|
||||||
|
// r.GET("/companies", controllers.Ge)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package shared
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"novatech/config"
|
"novatech/config"
|
||||||
|
"novatech/models"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
_ "github.com/jinzhu/gorm/dialects/postgres"
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||||
@@ -13,23 +15,28 @@ var db *gorm.DB
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
|
||||||
func Init() {
|
func Init() error{
|
||||||
host := config.AppConfig.Database.HostName
|
host := config.AppConfig.Database.HostName
|
||||||
user := config.AppConfig.Database.UserName
|
user := config.AppConfig.Database.UserName
|
||||||
port := config.AppConfig.Database.Port
|
// port := config.AppConfig.Database.Port
|
||||||
dbName := config.AppConfig.Database.DatabaseName
|
dbName := config.AppConfig.Database.DatabaseName
|
||||||
password := config.AppConfig.Database.Password
|
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)
|
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")
|
||||||
//PostgreSQL
|
var err error
|
||||||
db, err = gorm.Open(dbString)
|
// //PostgreSQL
|
||||||
|
db, err = gorm.Open("postgres",dbString)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println("Error initializing the database: ", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
//TODO AUTOMIGRATE models once we have them
|
//TODO AUTOMIGRATE models once we have them
|
||||||
//db.AutoMigrate(&models.Person{})
|
db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user