node-api/apps/main.go

102 lines
1.8 KiB
Go
Raw Normal View History

2020-07-08 22:09:19 +00:00
package apps
import "github.com/rosti-cz/apps-api/common"
func init() {
db := common.GetDBConnection()
db.AutoMigrate(Label{})
db.AutoMigrate(App{})
}
// Get returns one app
func Get(name string) (*App, error) {
var app App
db := common.GetDBConnection()
err := db.First(&app).Where("name = ?", name).Error
if err != nil {
return nil, err
}
return &app, nil
}
// List returns all apps located on this node
func List() (*[]App, error) {
var apps []App
db := common.GetDBConnection()
err := db.Find(&apps).Error
if err != nil {
return nil, err
}
return &apps, nil
}
// New creates new record about application in the database
2020-07-09 22:27:23 +00:00
func New(name string, SSHPort int, HTTPPort int, image string, CPU int, memory int) error {
2020-07-08 22:09:19 +00:00
app := App{
Name: name,
SSHPort: SSHPort,
HTTPPort: HTTPPort,
Image: image,
CPU: CPU,
Memory: memory,
}
db := common.GetDBConnection()
2020-07-09 22:27:23 +00:00
validationErrors := app.Validate()
if len(validationErrors) != 0 {
return ValidationError{
Errors: validationErrors,
}
}
2020-07-08 22:09:19 +00:00
if err := db.Create(app).Error; err != nil {
return err
}
return nil
}
// Update changes value about app in the database
2020-07-09 22:27:23 +00:00
func Update(name string, SSHPort int, HTTPPort int, image string, CPU int, memory int) error {
2020-07-08 22:09:19 +00:00
var app App
db := common.GetDBConnection()
err := db.First(&app).Where("name = ?", name).Error
if err != nil {
return err
}
2020-07-09 22:27:23 +00:00
app.SSHPort = SSHPort
app.HTTPPort = HTTPPort
app.Image = image
app.CPU = CPU
app.Memory = memory
validationErrors := app.Validate()
if len(validationErrors) != 0 {
return ValidationError{
Errors: validationErrors,
}
}
err = db.Update(&app).Error
2020-07-08 22:09:19 +00:00
return err
}
// Delete removes records about one app from the database
func Delete(name string) error {
db := common.GetDBConnection()
err := db.Delete(App{}).Where("name = ?", name).Error
return err
}