Hub-Api/commands/store_handler.go

100 lines
2.1 KiB
Go

package commands
import (
m "netina/models"
c "netina/models/commands"
s "netina/repositories/store"
"netina/validation"
"time"
)
type CreateStoreHandler struct {
Repository s.StoreCommandRepository
}
func (r *CreateStoreHandler) Handle (command c.CreateStoreCommand , modified_by string)error {
if err := validation.ValidateStruct(command); err != nil {
return err
}
store := &m.Store{
Owner_id: command.Owner_id,
Name: command.Name,
Address: command.Address,
PhoneNumber: command.PhoneNumber,
WebAddress: command.WebAddress,
ApiAddress: command.ApiAddress,
AdminPanelAddress: command.AdminPanelAddress,
StorageAddress: command.StorageAddress,
License_id: command.License_id,
Modified_by: modified_by,
Created_at: time.Now(),
StorageApiKey: command.StorageApiKey,
StorageSecret: command.StorageSecret,
StorageName: command.StorageName,
}
// baseDir := "../uploads"
// dirName := command.Name
// targetDir := filepath.Join(baseDir , dirName)
// err := os.MkdirAll(targetDir, 0755)
// if err != nil {
// return err
// }
return r.Repository.CreateStore(store)
}
type UpdateStoreHandler struct {
Repository s.StoreCommandRepository
}
func (r *UpdateStoreHandler) Handle (id uint ,command c.UpdateStoreCommand , modified_by string)(*m.Store , error) {
if err := validation.ValidateStruct(command); err != nil {
return nil,err
}
store := &m.Store{
Owner_id: command.Owner_id,
Name: command.Name,
Address: command.Address,
PhoneNumber: command.PhoneNumber,
WebAddress: command.WebAddress,
ApiAddress: command.ApiAddress,
AdminPanelAddress: command.AdminPanelAddress,
StorageAddress: command.StorageAddress,
Modified_by: modified_by,
Modified_at: time.Now(),
StorageName: command.StorageName,
StorageApiKey: command.StorageApiKey,
StorageSecret: command.StorageSecret,
}
return r.Repository.UpdateStore(id ,store )
}
type RemoveStoreHandler struct {
Repository s.StoreCommandRepository
}
func (r *RemoveStoreHandler) Handle (id uint) error {
return r.Repository.RemoveStore(id)
}