HEAVY: migrate accounts and logs to service/repo architecture
This commit is contained in:
parent
5c255fb34b
commit
49e14b5bc5
37 changed files with 1019 additions and 687 deletions
140
service/account/account.go
Normal file
140
service/account/account.go
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
package account
|
||||
|
||||
import (
|
||||
"arimelody-web/model"
|
||||
repository "arimelody-web/repository/account"
|
||||
"errors"
|
||||
"log"
|
||||
)
|
||||
|
||||
type AccountService struct {
|
||||
repo repository.AccountRepository
|
||||
log *log.Logger
|
||||
}
|
||||
|
||||
func NewAccountService(repo repository.AccountRepository, logger *log.Logger) (*AccountService) {
|
||||
return &AccountService{
|
||||
repo: repo,
|
||||
log: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AccountService) GetAll() ([]model.Account, error) {
|
||||
return s.repo.GetAll()
|
||||
}
|
||||
|
||||
func (s *AccountService) GetCount() (int, error) {
|
||||
return s.repo.GetCount()
|
||||
}
|
||||
|
||||
func (s *AccountService) GetByID(id string) (*model.Account, error) {
|
||||
return s.repo.GetByID(id)
|
||||
}
|
||||
|
||||
func (s *AccountService) GetByUsername(username string) (*model.Account, error) {
|
||||
return s.repo.GetByUsername(username)
|
||||
}
|
||||
|
||||
func (s *AccountService) GetByEmail(email string) (*model.Account, error) {
|
||||
return s.repo.GetByEmail(email)
|
||||
}
|
||||
|
||||
func (s *AccountService) GetBySession(sessionToken string) (*model.Account, error) {
|
||||
if sessionToken == "" { return nil, nil }
|
||||
|
||||
return s.repo.GetBySession(sessionToken)
|
||||
}
|
||||
|
||||
func (s *AccountService) Create(
|
||||
username string,
|
||||
password string,
|
||||
email *string,
|
||||
avatarURL *string,
|
||||
) (string, error) {
|
||||
var id string
|
||||
var err error
|
||||
|
||||
if len(username) == 0 { return id, errors.New("Username cannot be empty") }
|
||||
if len(password) == 0 { return id, errors.New("Password cannot be empty") }
|
||||
if email != nil && len(*email) == 0 { return id, errors.New("Email cannot be empty") }
|
||||
|
||||
if id, err = s.repo.Create(username, password, email, avatarURL); err != nil {
|
||||
return id, err
|
||||
}
|
||||
|
||||
s.log.Printf("Created account '%s' (%s)", username, id)
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Intended for large profile updates. For smaller adjusments,
|
||||
// more specialised Change* and Remove* functions should be used.
|
||||
func (s *AccountService) Update(
|
||||
id string,
|
||||
username string,
|
||||
password string,
|
||||
email *string,
|
||||
avatarUrl *string,
|
||||
) error {
|
||||
if len(username) == 0 { return errors.New("Username cannot be empty") }
|
||||
if len(password) == 0 { return errors.New("Password cannot be empty") }
|
||||
if email != nil && len(*email) == 0 { return errors.New("Email cannot be empty") }
|
||||
|
||||
if err := s.repo.Update(id, username, password, email, avatarUrl); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.log.Printf("Updated account '%s' (%s)", username, id)
|
||||
return nil
|
||||
}
|
||||
func (s *AccountService) ChangeUsername(id string, username string) error {
|
||||
if len(username) == 0 { return errors.New("Username cannot be empty") }
|
||||
if err := s.repo.ChangeUsername(id, username); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.log.Printf("Changed username for %s to '%s'", id, username)
|
||||
return nil
|
||||
}
|
||||
func (s *AccountService) ChangePassword(id string, password string) error {
|
||||
if len(password) == 0 { return errors.New("Password cannot be empty") }
|
||||
if err := s.repo.ChangePassword(id, password); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.log.Printf("Changed password for %s", id)
|
||||
return nil
|
||||
}
|
||||
func (s *AccountService) ChangeEmail(id string, email string) error {
|
||||
if len(email) == 0 { return s.repo.RemoveEmail(id) }
|
||||
if err := s.repo.ChangeEmail(id, email); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.log.Printf("Changed email for %s to '%s'", id, email)
|
||||
return nil
|
||||
}
|
||||
func (s *AccountService) ChangeAvatarURL(id string, avatarURL string) error {
|
||||
if len(avatarURL) == 0 { return s.repo.RemoveAvatar(id) }
|
||||
if err := s.repo.ChangeAvatarURL(id, avatarURL); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.log.Printf("Changed avatar URL for %s to '%s'", id, avatarURL)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *AccountService) Delete(accountID string) error {
|
||||
return s.repo.Delete(accountID)
|
||||
}
|
||||
|
||||
func (s *AccountService) IncrementFails(accountID string) (int, error) {
|
||||
return s.repo.IncrementFails(accountID)
|
||||
}
|
||||
|
||||
func (s *AccountService) Lock(accountID string) error {
|
||||
return s.repo.Lock(accountID)
|
||||
}
|
||||
|
||||
func (s *AccountService) Unlock(accountID string) error {
|
||||
return s.repo.Unlock(accountID)
|
||||
}
|
||||
57
service/log/log.go
Normal file
57
service/log/log.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package log
|
||||
|
||||
import (
|
||||
"arimelody-web/model"
|
||||
repository "arimelody-web/repository/log"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type LogService struct {
|
||||
repo repository.LogRepository
|
||||
log *log.Logger
|
||||
}
|
||||
|
||||
func NewLogService(repo repository.LogRepository, logger *log.Logger) *LogService {
|
||||
return &LogService{
|
||||
repo: repo,
|
||||
log: logger,
|
||||
}
|
||||
}
|
||||
|
||||
const DEFAULT_LOG_PAGE_LENGTH = 25
|
||||
|
||||
func (s *LogService) Info(logType string, format string, args ...any) {
|
||||
logString := fmt.Sprintf(format, args...)
|
||||
|
||||
s.log.Printf("[%s] [%s] INFO: %s\n", time.Now().Format(time.UnixDate), logType, logString)
|
||||
|
||||
if err := s.repo.Create(model.LEVEL_INFO, logType, logString); err != nil {
|
||||
log.Printf("WARN: Failed to push log to database: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *LogService) Warn(logType string, format string, args ...any) {
|
||||
logString := fmt.Sprintf(format, args...)
|
||||
|
||||
log.Printf("[%s] [%s] WARN: %s\n", time.Now().Format(time.UnixDate), logType, logString)
|
||||
|
||||
if err := s.repo.Create(model.LEVEL_INFO, logType, logString); err != nil {
|
||||
log.Printf("WARN: Failed to push log to database: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *LogService) Fetch(id string) (*model.Log, error) {
|
||||
return s.repo.Get(id)
|
||||
}
|
||||
|
||||
func (s *LogService) Search(
|
||||
levelFilters []model.LogLevel,
|
||||
typeFilters []string,
|
||||
content string,
|
||||
limit int,
|
||||
offset int,
|
||||
) ([]*model.Log, error) {
|
||||
return s.repo.Search(levelFilters, typeFilters, content, limit, offset)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue