39 lines
800 B
Go
39 lines
800 B
Go
package services
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
|
|
"example.com/m/internal/repositories"
|
|
"github.com/sqids/sqids-go"
|
|
)
|
|
|
|
type LinksService struct {
|
|
repo *repositories.LinksRepository
|
|
sqid *sqids.Sqids
|
|
}
|
|
|
|
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, 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
|
|
}
|
|
|
|
func (s *LinksService) GetLink(id string) (string, error) {
|
|
return s.repo.GetLink(id)
|
|
}
|