65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package user
|
|
|
|
import "fmt"
|
|
|
|
type Service struct {
|
|
initialized bool
|
|
|
|
store UserCRUD
|
|
}
|
|
|
|
func NewService(store UserCRUD) (*Service, error) {
|
|
if store == nil {
|
|
return nil, fmt.Errorf("store is nil")
|
|
}
|
|
return &Service{
|
|
store: store,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Service) isInitialized() bool {
|
|
return s.initialized
|
|
}
|
|
|
|
func (s *Service) Init() error {
|
|
if s.isInitialized() {
|
|
return nil
|
|
}
|
|
|
|
err := s.store.init()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to initialize user store: %w", err)
|
|
}
|
|
|
|
s.initialized = true
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) Create(user *User) error {
|
|
if !s.isInitialized() {
|
|
return fmt.Errorf("user service is not initialized")
|
|
}
|
|
return s.store.Create(user)
|
|
}
|
|
|
|
func (s *Service) GetBy(by, value string) (*User, error) {
|
|
if !s.isInitialized() {
|
|
return nil, fmt.Errorf("user service is not initialized")
|
|
}
|
|
return s.store.GetBy(by, value)
|
|
}
|
|
|
|
func (s *Service) Update(user *User) error {
|
|
if !s.isInitialized() {
|
|
return fmt.Errorf("user service is not initialized")
|
|
}
|
|
return s.store.Update(user)
|
|
}
|
|
|
|
func (s *Service) Delete(id int64) error {
|
|
if !s.isInitialized() {
|
|
return fmt.Errorf("user service is not initialized")
|
|
}
|
|
return s.store.Delete(id)
|
|
}
|