-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,126 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
cacao | ||
sqlite.db | ||
cookie |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/lanthora/cacao/candy" | ||
"github.com/lanthora/cacao/model" | ||
"github.com/lanthora/cacao/status" | ||
"github.com/lanthora/cacao/storage" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func NetShow(c *gin.Context) { | ||
user := c.MustGet("user").(*model.User) | ||
nets := model.GetNetsByUserID(user.ID) | ||
|
||
type netinfo struct { | ||
ID uint `json:"netid"` | ||
Name string `json:"netname"` | ||
Password string `json:"password"` | ||
DHCP string `json:"dhcp"` | ||
Broadcast bool `json:"broadcast"` | ||
} | ||
|
||
response := make([]netinfo, 0) | ||
for _, n := range nets { | ||
response = append(response, netinfo{ | ||
ID: n.ID, | ||
Name: n.Name, | ||
Password: n.Password, | ||
DHCP: n.DHCP, | ||
Broadcast: n.Broadcast, | ||
}) | ||
} | ||
|
||
status.UpdateSuccess(c, gin.H{ | ||
"nets": response, | ||
}) | ||
} | ||
|
||
func NetInsert(c *gin.Context) { | ||
var request struct { | ||
Name string `json:"netname"` | ||
Password string `json:"password"` | ||
DHCP string `json:"dhcp"` | ||
Broadcast bool `json:"broadcast"` | ||
} | ||
|
||
if err := c.BindJSON(&request); err != nil { | ||
status.UpdateCode(c, status.InvalidRequest) | ||
return | ||
} | ||
|
||
user := c.MustGet("user").(*model.User) | ||
modelNet := &model.Net{ | ||
UserID: user.ID, | ||
Name: request.Name, | ||
} | ||
|
||
db := storage.Get() | ||
result := db.Where(modelNet).Take(modelNet) | ||
if result.Error != gorm.ErrRecordNotFound { | ||
status.UpdateCode(c, status.NetworkAlreadyExists) | ||
return | ||
} | ||
|
||
modelNet.Password = request.Password | ||
modelNet.DHCP = request.DHCP | ||
modelNet.Broadcast = request.Broadcast | ||
modelNet.Create() | ||
candy.InsertNet(modelNet) | ||
|
||
status.UpdateSuccess(c, gin.H{ | ||
"netid": modelNet.ID, | ||
"netname": modelNet.Name, | ||
"password": modelNet.Password, | ||
"dhcp": modelNet.DHCP, | ||
"broadcast": modelNet.Broadcast, | ||
}) | ||
} | ||
|
||
func NetEdit(c *gin.Context) { | ||
var request struct { | ||
ID uint `json:"netid"` | ||
Name string `json:"netname"` | ||
Password string `json:"password"` | ||
DHCP string `json:"dhcp"` | ||
Broadcast bool `json:"broadcast"` | ||
} | ||
|
||
if err := c.BindJSON(&request); err != nil { | ||
status.UpdateCode(c, status.InvalidRequest) | ||
return | ||
} | ||
|
||
user := c.MustGet("user").(*model.User) | ||
modelNet := &model.Net{} | ||
modelNet.ID = request.ID | ||
db := storage.Get() | ||
result := db.Where(modelNet).Take(modelNet) | ||
|
||
if result.Error != nil || modelNet.UserID != user.ID { | ||
status.UpdateCode(c, status.NetworkNotExists) | ||
return | ||
} | ||
|
||
modelNet.Name = request.Name | ||
modelNet.Password = request.Password | ||
modelNet.DHCP = request.DHCP | ||
modelNet.Broadcast = request.Broadcast | ||
modelNet.Update() | ||
candy.UpdateNet(modelNet) | ||
|
||
status.UpdateSuccess(c, gin.H{ | ||
"netid": modelNet.ID, | ||
"netname": modelNet.Name, | ||
"password": modelNet.Password, | ||
"dhcp": modelNet.DHCP, | ||
"broadcast": modelNet.Broadcast, | ||
}) | ||
} | ||
|
||
func NetDelete(c *gin.Context) { | ||
var request struct { | ||
ID uint `json:"netid"` | ||
} | ||
|
||
if err := c.BindJSON(&request); err != nil { | ||
status.UpdateCode(c, status.InvalidRequest) | ||
return | ||
} | ||
|
||
user := c.MustGet("user").(*model.User) | ||
modelNet := &model.Net{} | ||
modelNet.ID = request.ID | ||
db := storage.Get() | ||
result := db.Where(modelNet).Take(modelNet) | ||
|
||
if result.Error != nil || modelNet.UserID != user.ID { | ||
status.UpdateCode(c, status.NetworkNotExists) | ||
return | ||
} | ||
|
||
modelNet.Delete() | ||
candy.DeleteNet(modelNet.ID) | ||
|
||
status.UpdateSuccess(c, gin.H{ | ||
"id": modelNet.ID, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package candy | ||
|
||
import ( | ||
"github.com/lanthora/cacao/model" | ||
) | ||
|
||
type Device struct { | ||
model *model.Device | ||
ip uint32 | ||
} |
Oops, something went wrong.