new file structure & koneksi ke DB
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/api/models"
|
||||
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/api/repository"
|
||||
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/auth"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -38,14 +39,14 @@ var (
|
||||
|
||||
// ShortLinkService handles operations related to short links
|
||||
type ShortLinkService struct {
|
||||
jwtManager *auth.JWTManager
|
||||
logger *zap.Logger
|
||||
jwtManager *auth.JWTManager
|
||||
logger *zap.Logger
|
||||
shortLinkRepo *repository.ShortLinkRepository
|
||||
patientRepo *repository.PatientRepository
|
||||
// Configuration settings
|
||||
baseURL string
|
||||
defaultExpiryTime time.Duration
|
||||
maxAttempts int
|
||||
// Mock in-memory storage for now, would use a database in production
|
||||
shortLinks map[string]*models.ShortLink
|
||||
}
|
||||
|
||||
// NewShortLinkService creates a new short link service
|
||||
@@ -66,10 +67,11 @@ func NewShortLinkService(jwtManager *auth.JWTManager, logger *zap.Logger, baseUR
|
||||
return &ShortLinkService{
|
||||
jwtManager: jwtManager,
|
||||
logger: logger,
|
||||
shortLinkRepo: repository.NewShortLinkRepository(),
|
||||
patientRepo: repository.NewPatientRepository(),
|
||||
baseURL: baseURL,
|
||||
defaultExpiryTime: time.Duration(defaultExpiryHours) * time.Hour,
|
||||
maxAttempts: maxAttempts,
|
||||
shortLinks: make(map[string]*models.ShortLink),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,8 +125,12 @@ func (s *ShortLinkService) GenerateShortLink(req *models.GenerateShortLinkReques
|
||||
RemainingTries: s.maxAttempts,
|
||||
}
|
||||
|
||||
// Store the short link (in production, this would be in a database)
|
||||
s.shortLinks[token] = shortLink
|
||||
// Store the short link in the database
|
||||
err = s.shortLinkRepo.CreateShortLink(shortLink)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to store shortlink in database", zap.Error(err))
|
||||
return nil, ErrCreationFailed
|
||||
}
|
||||
|
||||
// Generate the full URL using the configured base URL
|
||||
fullURL := fmt.Sprintf("%s/short-auth?short=%s", s.baseURL, token)
|
||||
@@ -141,9 +147,14 @@ func (s *ShortLinkService) ValidateShortLink(req *models.ShortLinkAuthRequest) (
|
||||
// Get the token using the helper method that handles both field names
|
||||
token := req.GetToken()
|
||||
|
||||
// Find the short link
|
||||
shortLink, exists := s.shortLinks[token]
|
||||
if !exists {
|
||||
// Find the short link in the database
|
||||
shortLink, err := s.shortLinkRepo.GetShortLinkByToken(token)
|
||||
if err != nil {
|
||||
s.logger.Error("Error retrieving shortlink", zap.Error(err), zap.String("token", token))
|
||||
return nil, ErrShortLinkNotFound
|
||||
}
|
||||
|
||||
if shortLink == nil {
|
||||
return nil, ErrShortLinkNotFound
|
||||
}
|
||||
|
||||
@@ -168,6 +179,13 @@ func (s *ShortLinkService) ValidateShortLink(req *models.ShortLinkAuthRequest) (
|
||||
if !isValidDOBFormat(dob) {
|
||||
// Decrement remaining tries on invalid format
|
||||
shortLink.RemainingTries--
|
||||
|
||||
// Update the shortlink in the database
|
||||
err = s.shortLinkRepo.UpdateShortLink(shortLink)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to update shortlink tries", zap.Error(err))
|
||||
}
|
||||
|
||||
return nil, errors.New("invalid date of birth format, expected YYYY-MM-DD")
|
||||
}
|
||||
|
||||
@@ -177,12 +195,25 @@ func (s *ShortLinkService) ValidateShortLink(req *models.ShortLinkAuthRequest) (
|
||||
if hashedDOB != shortLink.HashedDOB {
|
||||
// Decrement remaining tries on failed verification
|
||||
shortLink.RemainingTries--
|
||||
|
||||
// Update the shortlink in the database
|
||||
err = s.shortLinkRepo.UpdateShortLink(shortLink)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to update shortlink tries", zap.Error(err))
|
||||
}
|
||||
|
||||
return nil, ErrInvalidDOB
|
||||
}
|
||||
|
||||
// DOB verified, reset tries count as successful login
|
||||
shortLink.RemainingTries = s.maxAttempts
|
||||
|
||||
// Update the shortlink in the database
|
||||
err = s.shortLinkRepo.UpdateShortLink(shortLink)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to update shortlink tries after successful validation", zap.Error(err))
|
||||
}
|
||||
|
||||
return shortLink, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user