goploy/model/server_process.go

114 lines
2.4 KiB
Go
Raw Normal View History

2022-08-26 19:31:32 +08:00
// Copyright 2022 The Goploy Authors. All rights reserved.
// Use of this source code is governed by a GPLv3-style
// license that can be found in the LICENSE file.
package model
import (
sq "github.com/Masterminds/squirrel"
)
const serverProcessTable = "`server_process`"
type ServerProcess struct {
2022-09-30 14:28:29 +08:00
ID int64 `json:"id"`
NamespaceID int64 `json:"namespaceId"`
Name string `json:"name"`
Items string `json:"items"`
InsertTime string `json:"insertTime,omitempty"`
UpdateTime string `json:"updateTime,omitempty"`
2022-08-26 19:31:32 +08:00
}
type ServerProcesses []ServerProcess
2022-09-30 14:28:29 +08:00
type ServerProcessItem struct {
Name string
Command string
}
type ServerProcessItems []ServerProcessItem
2022-08-26 19:31:32 +08:00
func (sp ServerProcess) GetData() (ServerProcess, error) {
var serverProcess ServerProcess
err := sq.
2022-09-30 14:28:29 +08:00
Select("id, name, items").
2022-08-26 19:31:32 +08:00
From(serverProcessTable).
Where(sq.Eq{"id": sp.ID}).
OrderBy("id DESC").
RunWith(DB).
QueryRow().
Scan(&serverProcess.ID,
&serverProcess.Name,
2022-09-30 14:28:29 +08:00
&serverProcess.Items)
2022-08-26 19:31:32 +08:00
if err != nil {
return serverProcess, err
}
return serverProcess, nil
}
2022-09-30 14:28:29 +08:00
func (sp ServerProcess) GetList() (ServerProcesses, error) {
2022-08-26 19:31:32 +08:00
rows, err := sq.
2022-09-30 14:28:29 +08:00
Select("id, name, items, insert_time, update_time").
2022-08-26 19:31:32 +08:00
From(serverProcessTable).
2022-09-30 14:28:29 +08:00
Where(sq.Eq{"namespace_id": sp.NamespaceID}).
2022-08-26 19:31:32 +08:00
OrderBy("id DESC").
RunWith(DB).
Query()
if err != nil {
return nil, err
}
serverProcesses := ServerProcesses{}
for rows.Next() {
var serverProcess ServerProcess
if err := rows.Scan(
&serverProcess.ID,
&serverProcess.Name,
2022-09-30 14:28:29 +08:00
&serverProcess.Items,
2022-08-26 19:31:32 +08:00
&serverProcess.InsertTime,
&serverProcess.UpdateTime,
); err != nil {
return serverProcesses, err
}
serverProcesses = append(serverProcesses, serverProcess)
}
return serverProcesses, nil
}
func (sp ServerProcess) AddRow() (int64, error) {
result, err := sq.
Insert(serverProcessTable).
2022-09-30 14:28:29 +08:00
Columns("namespace_id", "name", "items").
Values(sp.NamespaceID, sp.Name, sp.Items).
2022-08-26 19:31:32 +08:00
RunWith(DB).
Exec()
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
return id, err
}
func (sp ServerProcess) EditRow() error {
_, err := sq.
Update(serverProcessTable).
SetMap(sq.Eq{
2022-09-30 14:28:29 +08:00
"name": sp.Name,
"items": sp.Items,
2022-08-26 19:31:32 +08:00
}).
Where(sq.Eq{"id": sp.ID}).
RunWith(DB).
Exec()
return err
}
func (sp ServerProcess) DeleteRow() error {
_, err := sq.
Delete(serverProcessTable).
Where(sq.Eq{"id": sp.ID}).
RunWith(DB).
Exec()
return err
}