Add Application and ParameterSchema models with CRUD store. Includes 10 store tests and ParamType constants. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
115 lines
2.6 KiB
Go
115 lines
2.6 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"gcy_hpc_server/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ApplicationStore struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewApplicationStore(db *gorm.DB) *ApplicationStore {
|
|
return &ApplicationStore{db: db}
|
|
}
|
|
|
|
func (s *ApplicationStore) List(ctx context.Context, page, pageSize int) ([]model.Application, int, error) {
|
|
var apps []model.Application
|
|
var total int64
|
|
|
|
if err := s.db.WithContext(ctx).Model(&model.Application{}).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (page - 1) * pageSize
|
|
if err := s.db.WithContext(ctx).Order("id DESC").Limit(pageSize).Offset(offset).Find(&apps).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return apps, int(total), nil
|
|
}
|
|
|
|
func (s *ApplicationStore) GetByID(ctx context.Context, id int64) (*model.Application, error) {
|
|
var app model.Application
|
|
err := s.db.WithContext(ctx).First(&app, id).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &app, nil
|
|
}
|
|
|
|
func (s *ApplicationStore) Create(ctx context.Context, req *model.CreateApplicationRequest) (int64, error) {
|
|
params := req.Parameters
|
|
if len(params) == 0 {
|
|
params = json.RawMessage(`[]`)
|
|
}
|
|
|
|
app := &model.Application{
|
|
Name: req.Name,
|
|
Description: req.Description,
|
|
Icon: req.Icon,
|
|
Category: req.Category,
|
|
ScriptTemplate: req.ScriptTemplate,
|
|
Parameters: params,
|
|
Scope: req.Scope,
|
|
}
|
|
if err := s.db.WithContext(ctx).Create(app).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
return app.ID, nil
|
|
}
|
|
|
|
func (s *ApplicationStore) Update(ctx context.Context, id int64, req *model.UpdateApplicationRequest) error {
|
|
updates := map[string]interface{}{}
|
|
if req.Name != nil {
|
|
updates["name"] = *req.Name
|
|
}
|
|
if req.Description != nil {
|
|
updates["description"] = *req.Description
|
|
}
|
|
if req.Icon != nil {
|
|
updates["icon"] = *req.Icon
|
|
}
|
|
if req.Category != nil {
|
|
updates["category"] = *req.Category
|
|
}
|
|
if req.ScriptTemplate != nil {
|
|
updates["script_template"] = *req.ScriptTemplate
|
|
}
|
|
if req.Parameters != nil {
|
|
updates["parameters"] = *req.Parameters
|
|
}
|
|
if req.Scope != nil {
|
|
updates["scope"] = *req.Scope
|
|
}
|
|
|
|
if len(updates) == 0 {
|
|
return nil
|
|
}
|
|
|
|
result := s.db.WithContext(ctx).Model(&model.Application{}).Where("id = ?", id).Updates(updates)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *ApplicationStore) Delete(ctx context.Context, id int64) error {
|
|
result := s.db.WithContext(ctx).Delete(&model.Application{}, id)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
return nil
|
|
}
|