You've already forked openaccounting-server
forked from cybercinch/openaccounting-server
- Replace SQL-based queries with GORM repository calls - Update all model interfaces to use repository pattern - Fix compilation errors in core/model/ files - Update mocks to match new repository interfaces - Modify API handlers to use new repository layer - Maintain backward compatibility with existing interfaces 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package model
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/openaccounting/oa-server/core/model/types"
|
|
)
|
|
|
|
type BudgetInterface interface {
|
|
GetBudget(string, string) (*types.Budget, error)
|
|
CreateBudget(*types.Budget, string) error
|
|
DeleteBudget(string, string) error
|
|
}
|
|
|
|
func (model *Model) GetBudget(orgId string, userId string) (*types.Budget, error) {
|
|
belongs, err := model.UserBelongsToOrg(userId, orgId)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !belongs {
|
|
return nil, errors.New("user does not belong to org")
|
|
}
|
|
|
|
return model.db.GetBudget(orgId)
|
|
}
|
|
|
|
func (model *Model) CreateBudget(budget *types.Budget, userId string) error {
|
|
belongs, err := model.UserBelongsToOrg(userId, budget.OrgId)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !belongs {
|
|
return errors.New("user does not belong to org")
|
|
}
|
|
|
|
if budget.OrgId == "" {
|
|
return errors.New("orgId required")
|
|
}
|
|
|
|
return model.db.InsertAndReplaceBudget(budget)
|
|
}
|
|
|
|
func (model *Model) DeleteBudget(orgId string, userId string) error {
|
|
belongs, err := model.UserBelongsToOrg(userId, orgId)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !belongs {
|
|
return errors.New("user does not belong to org")
|
|
}
|
|
|
|
return model.db.DeleteBudget(orgId)
|
|
}
|