Skip to content

Commit 73ac9b8

Browse files
committed
Relations between models
1 parent 24cedce commit 73ac9b8

File tree

8 files changed

+41
-18
lines changed

8 files changed

+41
-18
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
run:
22
CompileDaemon -command="./homelab-backend"
33

4-
run linux:
4+
run-linux:
55
/home/kaizer/go/bin/CompileDaemon -command="./homelab-backend"

controllers/Devices.controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func GetAllDevices(c *gin.Context) {
1212

1313
// Get all devices from database
14-
var devices []models.Devices
14+
var devices []models.Device
1515
initializers.DB.Find(&devices)
1616

1717
c.IndentedJSON(http.StatusOK, devices)
@@ -22,7 +22,7 @@ func GetDeviceById(c *gin.Context) {
2222

2323
id := c.Param("id")
2424

25-
var device models.Devices
25+
var device models.Device
2626
result := initializers.DB.First(&device, id)
2727

2828
if result.Error != nil {

controllers/Rooms.controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func GetAllRooms(c *gin.Context) {
1212

1313
// >> Get all rooms from database
14-
var rooms []models.Rooms
14+
var rooms []models.Room
1515
initializers.DB.Find(rooms)
1616

1717
c.IndentedJSON(http.StatusOK, rooms)
@@ -20,7 +20,7 @@ func GetAllRooms(c *gin.Context) {
2020
func GetAllRoomsByID(c *gin.Context) {
2121

2222
// >> Get all rooms from database
23-
var room models.Rooms
23+
var room models.Room
2424
result := initializers.DB.Find(room)
2525

2626
if result.Error != nil {

migrate/migrate.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func init() {
1212

1313
func main() {
1414
initializers.DB.AutoMigrate(&models.User{})
15-
initializers.DB.AutoMigrate(&models.Rooms{})
16-
initializers.DB.AutoMigrate(&models.Devices{})
15+
initializers.DB.AutoMigrate(&models.House{})
16+
initializers.DB.AutoMigrate(&models.Room{})
17+
initializers.DB.AutoMigrate(&models.Device{})
1718
}

models/Devices.go renamed to models/Device.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package models
22

33
import "gorm.io/gorm"
44

5-
type Devices struct {
5+
type Device struct {
66
gorm.Model
77
ID int `gorm:"primaryKey;autoIncrement"`
88
Name string `gorm:"size:64;index"`
99
Category string `gorm:"size:64;"`
10-
Room int
10+
RoomID int
11+
Room Room `gorm:"foreignKey:RoomID;"`
1112
Status int8
1213
}

models/House.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package models
2+
3+
import "gorm.io/gorm"
4+
5+
type House struct {
6+
gorm.Model
7+
ID int `gorm:"primaryKey;autoIncrement"`
8+
Address string `gorm:"size:128;index"`
9+
City string `gorm:"size:128;index"`
10+
Country string `gorm:"size:128;index"`
11+
ZipCode string `gorm:"size:128;index"`
12+
Floors int8
13+
Garage bool
14+
Parking bool
15+
SquareMeters float32
16+
Latitude float32
17+
Longitude float32
18+
}

models/Room.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package models
2+
3+
import "gorm.io/gorm"
4+
5+
type Room struct {
6+
gorm.Model
7+
ID int `gorm:"primaryKey;autoIncrement"`
8+
Name string `gorm:"size:64;index"`
9+
SquareMeters float64
10+
HouseID int
11+
House House `gorm:"foreignKey:HouseID"`
12+
}

models/Rooms.go

-9
This file was deleted.

0 commit comments

Comments
 (0)