Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delete device #83

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions api/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,31 @@ func DeviceShow(c *gin.Context) {
"devices": response,
})
}

func DeviceDelete(c *gin.Context) {
var request struct {
DevID uint `json:"devid"`
}

if err := c.BindJSON(&request); err != nil {
status.UpdateCode(c, status.InvalidRequest)
return
}

deviceModel := model.GetDeviceByDevID(request.DevID)
if deviceModel.Online {
status.UpdateCode(c, status.CannotDeleteOnlineDevice)
return
}

netModel := model.GetNetByNetID(deviceModel.NetID)

user := c.MustGet("user").(*model.User)
if user.ID != netModel.UserID {
status.UpdateCode(c, status.DeviceNotExists)
return
}

deviceModel.Delete()
status.UpdateSuccess(c, nil)
}
6 changes: 3 additions & 3 deletions candy/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (net *Net) updateHost() string {
return uint32ToStrIp(net.net | net.host)
}

func (net *Net) Close() {
func (net *Net) close() {
net.ipWsMapMutex.Lock()
defer net.ipWsMapMutex.Unlock()
for ip, ws := range net.ipWsMap {
Expand Down Expand Up @@ -179,7 +179,7 @@ func DeleteNet(netid uint) {
defer idNetMapMutex.Unlock()

if net, ok := idNetMap[netid]; ok {
net.Close()
net.close()
}

delete(idNetMap, netid)
Expand All @@ -190,7 +190,7 @@ func ReloadNet(netid uint) {
defer idNetMapMutex.Unlock()

if net, ok := idNetMap[netid]; ok {
net.Close()
net.close()
}
}

Expand Down
30 changes: 29 additions & 1 deletion frontend/src/components/normal/DeviceView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
</a-layout-header>
<a-layout-content :style="{ margin: '24px 16px 0' }">
<div :style="{ padding: '24px', background: '#fff' }">
<a-table :columns="deviceColumns" :dataSource="deviceSource"> </a-table>
<a-table :columns="deviceColumns" :dataSource="deviceSource">
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'action'">
<a-space wrap>
<a-button danger type="primary" size="small" @click="deleteDevice(record)">
Delete
</a-button>
</a-space>
</template>
</template>
</a-table>
</div>
</a-layout-content>
</template>
Expand Down Expand Up @@ -74,6 +84,11 @@ const deviceColumns = [
dataIndex: 'version',
key: 'version',
align: 'center'
},
{
title: 'Action',
key: 'action',
align: 'center'
}
]

Expand Down Expand Up @@ -117,6 +132,19 @@ const updateNetMap = async () => {
)
}
}

const deleteDevice = async (record) => {
console.log(record)
const response = await axios.post('/api/device/delete', {
devid: record.devid
})

const status = response.data.status
if (status == 0) {
updateDeviceSource()
}
}

onBeforeMount(() => {
updateNetMap()
})
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func main() {

device := r.Group("/api/device")
device.POST("/show", api.DeviceShow)
device.POST("/delete", api.DeviceDelete)

route := r.Group("/api/route")
route.POST("/show", api.RouteShow)
Expand Down
43 changes: 33 additions & 10 deletions model/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,55 @@ func (d *Device) Save() {
}
}

func GetDevicesByNetID(netid uint) (devices []Device) {
func (d *Device) Delete() {
db := storage.Get()
db.Where(&Device{NetID: netid}).Find(&devices)
db.Delete(d)
}

func GetDeviceByDevID(devid uint) (device Device) {
if devid != 0 {
db := storage.Get()
db.Where(&Device{Model: gorm.Model{ID: devid}}).Find(&device)
}
return
}

func GetDevicesByNetID(netid uint) (devices []Device) {
if netid != 0 {
db := storage.Get()
db.Where(&Device{NetID: netid}).Find(&devices)
}
return
}

func GetDevicesByUserID(userid uint) (devices []Device) {
db := storage.Get()
db.Model(&Device{}).Joins("left join nets on devices.net_id = nets.id").Where("nets.user_id = ?", userid).Find(&devices)
if userid != 0 {
db := storage.Get()
db.Model(&Device{}).Joins("left join nets on devices.net_id = nets.id").Where("nets.user_id = ?", userid).Find(&devices)
}
return
}

func GetRxSumByUserID(userid uint) (rx uint64) {
db := storage.Get()
db.Model(&Device{}).Select("sum(rx)").Joins("left join nets on devices.net_id = nets.id").Where("nets.user_id = ?", userid).Take(&rx)
if userid != 0 {
db := storage.Get()
db.Model(&Device{}).Select("sum(rx)").Joins("left join nets on devices.net_id = nets.id").Where("nets.user_id = ?", userid).Take(&rx)
}
return
}

func GetTxSumByUserID(userid uint) (tx uint64) {
db := storage.Get()
db.Model(&Device{}).Select("sum(tx)").Joins("left join nets on devices.net_id = nets.id").Where("nets.user_id = ?", userid).Take(&tx)
if userid != 0 {
db := storage.Get()
db.Model(&Device{}).Select("sum(tx)").Joins("left join nets on devices.net_id = nets.id").Where("nets.user_id = ?", userid).Take(&tx)
}
return
}

func DeleteDevicesByNetID(netid uint) (devices []Device) {
db := storage.Get()
db.Where(&Device{NetID: netid}).Delete(&Device{})
if netid != 0 {
db := storage.Get()
db.Where(&Device{NetID: netid}).Delete(&Device{})
}
return
}
2 changes: 1 addition & 1 deletion model/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func GetNets() (nets []Net) {
func GetNetByNetID(netid uint) (net Net) {
if netid != 0 {
db := storage.Get()
db.Where(&Net{Model: gorm.Model{ID: netid}}).Take(&net)
db.Where(&Net{Model: gorm.Model{ID: netid}}).Unscoped().Take(&net)
}
return
}
Expand Down
4 changes: 4 additions & 0 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func init() {
statusMessage[CannotDeleteAdmin] = "cannot delete admin"
statusMessage[InvalidIPAddress] = "invalid ip address"
statusMessage[RouteNotExists] = "route not exists"
statusMessage[DeviceNotExists] = "device not exists"
statusMessage[CannotDeleteOnlineDevice] = "cannot delete online device"
}

const (
Expand All @@ -49,6 +51,8 @@ const (
CannotDeleteAdmin
InvalidIPAddress
RouteNotExists
DeviceNotExists
CannotDeleteOnlineDevice
)

var statusMessage map[int]string
Expand Down