Files
KoCoder a9e1d1e5d9
All checks were successful
Build and Push Docker Image / build (push) Successful in 5m55s
Implement rudimentary link shortening service and wire everything together
2026-06-02 19:34:58 +02:00

106 lines
3.1 KiB
Go

package service
import (
"context"
"log/slog"
"time"
"git.kocoder.xyz/vt/shortener/internal/config"
"git.kocoder.xyz/vt/shortener/internal/database"
shortenv1 "git.kocoder.xyz/vt/shortener/internal/proto/shorten/v1"
"git.kocoder.xyz/vt/shortener/internal/proto/shorten/v1/shortenv1connect"
"google.golang.org/protobuf/types/known/timestamppb"
)
type ShortenerService struct {
conf *config.Config
db *database.Queries
}
// CreateURLRedirection implements [shortenv1connect.ShortenServiceHandler].
func (s *ShortenerService) CreateURLRedirection(ctx context.Context, req *shortenv1.CreateURLRedirectionRequest) (*shortenv1.CreateURLRedirectionResponse, error) {
_, err := s.db.CreateURL(ctx, database.CreateURLParams{
ShortCode: req.ShortCode,
LongUrl: req.Url,
IsActive: true,
CreatedAt: time.Now(),
})
if err != nil {
slog.Log(ctx, slog.LevelError, err.Error())
return &shortenv1.CreateURLRedirectionResponse{
Ok: false,
}, err
}
return &shortenv1.CreateURLRedirectionResponse{
Ok: true,
}, nil
}
// DeactivateURLRedirection implements [shortenv1connect.ShortenServiceHandler].
func (s *ShortenerService) DeactivateURLRedirection(ctx context.Context, req *shortenv1.DeactivateURLRedirectionRequest) (*shortenv1.DeactivateURLRedirectionResponse, error) {
_, err := s.db.SetActive(ctx, database.SetActiveParams{
IsActive: req.IsActive,
UrlID: req.UrlId,
})
if err != nil {
slog.Log(ctx, slog.LevelError, err.Error())
return &shortenv1.DeactivateURLRedirectionResponse{
Ok: false,
}, err
}
return &shortenv1.DeactivateURLRedirectionResponse{
Ok: true,
}, nil
}
// DeleteURLRedirection implements [shortenv1connect.ShortenServiceHandler].
func (s *ShortenerService) DeleteURLRedirection(ctx context.Context, req *shortenv1.DeleteURLRedirectionRequest) (*shortenv1.DeleteURLRedirectionResponse, error) {
_, err := s.db.DeleteURL(ctx, req.UrlId)
if err != nil {
slog.Log(ctx, slog.LevelError, err.Error())
return &shortenv1.DeleteURLRedirectionResponse{
Ok: false,
}, err
}
return &shortenv1.DeleteURLRedirectionResponse{
Ok: true,
}, nil
}
// ListURLRedirections implements [shortenv1connect.ShortenServiceHandler].
func (s *ShortenerService) ListURLRedirections(ctx context.Context, req *shortenv1.ListURLRedirectionsRequest) (*shortenv1.ListURLRedirectionsResponse, error) {
urls, err := s.db.GetURLs(ctx)
if err != nil {
slog.Log(ctx, slog.LevelError, err.Error())
return nil, err
}
var redirections []*shortenv1.URLRedirection
for _, url := range urls {
redirections = append(redirections, &shortenv1.URLRedirection{
UrlId: int32(url.UrlID),
ShortCode: url.ShortCode,
Url: url.LongUrl,
CreatedAt: timestamppb.New(url.CreatedAt),
ExpiresAt: timestamppb.New(url.ExpiresAt.Time),
IsActive: url.IsActive,
ClickCountByDay: []int32{},
})
}
return &shortenv1.ListURLRedirectionsResponse{
UrlRedirections: redirections,
}, nil
}
func NewShortenerService(conf *config.Config, db *database.Queries) shortenv1connect.ShortenServiceHandler {
return &ShortenerService{
conf: conf,
db: db,
}
}