add init functions
This commit is contained in:
@@ -3,6 +3,8 @@ package user
|
||||
import "fmt"
|
||||
|
||||
type Service struct {
|
||||
initialized bool
|
||||
|
||||
store UserCRUD
|
||||
}
|
||||
|
||||
@@ -15,18 +17,48 @@ func NewService(store UserCRUD) (*Service, error) {
|
||||
}, 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user