72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
package usecase
|
|
|
|
import (
|
|
"context"
|
|
"git.sch9.ru/new_gate/ms-auth/config"
|
|
"git.sch9.ru/new_gate/ms-auth/internal/sessions"
|
|
)
|
|
|
|
type useCase struct {
|
|
sessionRepo sessions.ValkeyRepository
|
|
cfg config.Config
|
|
}
|
|
|
|
func NewUseCase(sessionRepo sessions.ValkeyRepository, cfg config.Config) *useCase {
|
|
return &useCase{
|
|
sessionRepo: sessionRepo,
|
|
cfg: cfg,
|
|
}
|
|
}
|
|
|
|
func (s *useCase) Create(ctx context.Context, userId int32) (*string, error) {
|
|
var (
|
|
err error
|
|
)
|
|
|
|
s.sessionRepo.CreateSession(ctx, userId) // FIXME
|
|
|
|
session, err := s.sessionRepo.ReadSessionByUserId(ctx, userId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
token, err := session.Token(s.cfg.JWTSecret)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &token, nil
|
|
}
|
|
|
|
func (s *useCase) Read(ctx context.Context, token string) (*int32, error) {
|
|
session, err := s.sessionRepo.ReadSessionByToken(ctx, token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return session.UserId, nil
|
|
}
|
|
|
|
func (s *useCase) Update(ctx context.Context, token string) error {
|
|
session, err := s.sessionRepo.ReadSessionByToken(ctx, token)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = s.sessionRepo.UpdateSession(ctx, session)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *useCase) Delete(ctx context.Context, token string) error {
|
|
session, err := s.sessionRepo.ReadSessionByToken(ctx, token)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = s.sessionRepo.DeleteSessionByUserId(ctx, *session.UserId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|