|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + containermanger "github.com/swiftwave-org/swiftwave/container_manager" |
| 6 | + "gorm.io/gorm" |
| 7 | +) |
| 8 | + |
| 9 | +// This file contains the operations for the Application model. |
| 10 | +// This functions will perform necessary validation before doing the actual database operation. |
| 11 | + |
| 12 | +// Each function's argument format should be (ctx context.Context, db gorm.DB, ...) |
| 13 | +// context used to pass some data to the function e.g. user id, auth info, etc. |
| 14 | + |
| 15 | +func IsExistApplicationName(ctx context.Context, db gorm.DB, dockerManager containermanger.Manager, name string) (bool, error) { |
| 16 | + // verify from database |
| 17 | + var count int64 |
| 18 | + tx := db.Model(&Application{}).Where("name = ?", name).Count(&count) |
| 19 | + if tx.Error != nil { |
| 20 | + return false, tx.Error |
| 21 | + } |
| 22 | + if count > 0 { |
| 23 | + return true, nil |
| 24 | + } |
| 25 | + // verify from docker client |
| 26 | + _, err := dockerManager.GetService(name) |
| 27 | + if err == nil { |
| 28 | + return true, nil |
| 29 | + } |
| 30 | + return false, nil |
| 31 | +} |
| 32 | + |
| 33 | +func FindAllApplications(ctx context.Context, db gorm.DB) ([]*Application, error) { |
| 34 | + var applications []*Application |
| 35 | + tx := db.Find(&applications) |
| 36 | + return applications, tx.Error |
| 37 | +} |
| 38 | + |
| 39 | +func (application *Application) FindById(ctx context.Context, db gorm.DB, id string) error { |
| 40 | + tx := db.First(&application, id) |
| 41 | + return tx.Error |
| 42 | +} |
| 43 | + |
| 44 | +func (application *Application) Create(ctx context.Context, db gorm.DB) error { |
| 45 | + // TODO: add validation, create new deployment |
| 46 | + tx := db.Create(&application) |
| 47 | + return tx.Error |
| 48 | +} |
| 49 | + |
| 50 | +func (application *Application) Update(ctx context.Context, db gorm.DB) error { |
| 51 | + // TODO: add validation, create new deployment if change required |
| 52 | + tx := db.Save(&application) |
| 53 | + return tx.Error |
| 54 | +} |
| 55 | + |
| 56 | +func (application *Application) Delete(ctx context.Context, db gorm.DB) error { |
| 57 | + // TODO: add validation, delete all deployments and application |
| 58 | + tx := db.Delete(&application) |
| 59 | + return tx.Error |
| 60 | +} |
0 commit comments