This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
|
||||
"example.com/m/internal/repositories"
|
||||
@@ -12,17 +13,23 @@ type LinksService struct {
|
||||
sqid *sqids.Sqids
|
||||
}
|
||||
|
||||
func NewLinksService(repo *repositories.LinksRepository) *LinksService {
|
||||
s, _ := sqids.New()
|
||||
func NewLinksService(repo *repositories.LinksRepository) (*LinksService, error) {
|
||||
s, err := sqids.New()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize sqids: %w", err)
|
||||
}
|
||||
return &LinksService{
|
||||
repo: repo,
|
||||
sqid: s,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *LinksService) CreateLink(original string) (string, error) {
|
||||
id, _ := s.sqid.Encode([]uint64{rand.Uint64()})
|
||||
err := s.repo.CreateLink(id, original)
|
||||
id, err := s.sqid.Encode([]uint64{rand.Uint64()})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to encode link id: %w", err)
|
||||
}
|
||||
err = s.repo.CreateLink(id, original)
|
||||
return id, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user