From ad8d145986502d250a2eb08d3bc6a69fdc751923 Mon Sep 17 00:00:00 2001 From: dragonmuffin Date: Fri, 16 Aug 2024 16:01:18 +0500 Subject: [PATCH] update problem service --- internal/app/app.go | 35 --------- internal/services/problem.go | 2 +- internal/services/session.go | 147 ----------------------------------- 3 files changed, 1 insertion(+), 183 deletions(-) delete mode 100644 internal/app/app.go delete mode 100644 internal/services/session.go diff --git a/internal/app/app.go b/internal/app/app.go deleted file mode 100644 index d97d15e..0000000 --- a/internal/app/app.go +++ /dev/null @@ -1,35 +0,0 @@ -package app - -import ( - "log/slog" - "ms-auth/internal/lib" - - _ "github.com/jackc/pgx/v5/stdlib" -) - -type Server interface { - Start() - GracefullyStop() -} - -type App struct { - server Server - cfg *lib.Config -} - -func NewApp(cfg *lib.Config, server Server) *App { - return &App{ - server: server, - cfg: cfg, - } -} - -func (app *App) Start() { - app.server.Start() - slog.Info("app started") -} - -func (app *App) GracefullyStop() { - app.server.GracefullyStop() - slog.Info("app stopped") -} diff --git a/internal/services/problem.go b/internal/services/problem.go index 76e3080..c636914 100644 --- a/internal/services/problem.go +++ b/internal/services/problem.go @@ -6,7 +6,7 @@ import ( "git.sch9.ru/new_gate/ms-auth/internal/models" ) -type UserStorage interface { +type ProblemStorage interface { CreateProblem(ctx context.Context, problem models.Problem) (int32, error) ReadProblemById(ctx context.Context, id int32) (*models.Problem, error) UpdateProblem(ctx context.Context, problem *models.Problem) error diff --git a/internal/services/session.go b/internal/services/session.go deleted file mode 100644 index 7db7bbe..0000000 --- a/internal/services/session.go +++ /dev/null @@ -1,147 +0,0 @@ -package services - -import ( - "context" - "ms-auth/internal/lib" - "ms-auth/internal/storage" -) - -type SessionProvider interface { - CreateSession(ctx context.Context, userId int32) error - ReadSessionByToken(ctx context.Context, token string) (*storage.Session, error) - ReadSessionByUserId(ctx context.Context, userId int32) (*storage.Session, error) - UpdateSession(ctx context.Context, session *storage.Session) error - DeleteSessionByToken(ctx context.Context, token string) error - DeleteSessionByUserId(ctx context.Context, userId int32) error -} - -// SessionService represents a service for managing sessions. -type SessionService struct { - sessionProvider SessionProvider - userProvider UserProvider - cfg *lib.Config -} - -// NewSessionService creates a new SessionService instance. -// -// Parameters: -// - sessionProvider: The SessionProvider implementation used by the SessionService. -// - userProvider: The UserProvider implementation used by the SessionService. -// - cfg: The lib.Config object used by the SessionService. -// -// Returns: -// - *SessionService: A pointer to the SessionService instance. -func NewSessionService(sessionProvider SessionProvider, userProvider UserProvider, cfg *lib.Config) *SessionService { - return &SessionService{ - sessionProvider: sessionProvider, - userProvider: userProvider, - cfg: cfg, - } -} - -// Create creates a new session for a user with the given handle and password. -// -// Parameters: -// - ctx: The context.Context object for the request. -// - handle: The handle (username or email) of the user. -// - password: The password of the user. -// -// Returns: -// - *string: A pointer to the token of the newly created session, or nil if there was an error. -// - error: An error if the creation of the session or the retrieval of the session's token failed. -func (s *SessionService) Create(ctx context.Context, handle, password string) (*string, error) { - var ( - err error - user *storage.User - ) - - if lib.ValidUsername(handle) == nil { - user, err = s.userProvider.ReadUserByUsername(ctx, handle) - } else if lib.ValidEmail(handle) == nil { - user, err = s.userProvider.ReadUserByEmail(ctx, handle) - } else { - return nil, lib.ErrBadHandleOrPassword - } - if err != nil { - return nil, err - } - - err = user.ComparePassword(password) - if err != nil { - return nil, err - } - - err = s.sessionProvider.CreateSession(ctx, user.Id) - if err != nil { - return nil, err - } - - session, err := s.sessionProvider.ReadSessionByUserId(ctx, user.Id) - if err != nil { - return nil, err - } - - token, err := session.Token(s.cfg.JWTSecret) - if err != nil { - return nil, err - } - - return &token, nil -} - -// Read retrieves the user ID associated with the given session token. -// -// Parameters: -// - ctx: The context.Context object for the request. -// - token: The session token. -// -// Returns: -// - *int32: The user ID associated with the session token, or nil if an error occurs. -// - error: An error object if any error occurs during the retrieval process. -func (s *SessionService) Read(ctx context.Context, token string) (*int32, error) { - session, err := s.sessionProvider.ReadSessionByToken(ctx, token) - if err != nil { - return nil, err - } - return session.UserId, nil -} - -// Update updates the session associated with the given token. -// -// Parameters: -// - ctx: The context.Context object for the request. -// - token: The session token. -// -// Returns: -// - error: An error object if any error occurs during the update process. -func (s *SessionService) Update(ctx context.Context, token string) error { - session, err := s.sessionProvider.ReadSessionByToken(ctx, token) - if err != nil { - return err - } - err = s.sessionProvider.UpdateSession(ctx, session) - if err != nil { - return err - } - return nil -} - -// Delete deletes the session associated with the given token. -// -// Parameters: -// - ctx: The context.Context object for the request. -// - token: The session token. -// -// Returns: -// - error: An error object if any error occurs during the deletion process. -func (s *SessionService) Delete(ctx context.Context, token string) error { - session, err := s.sessionProvider.ReadSessionByToken(ctx, token) - if err != nil { - return err - } - err = s.sessionProvider.DeleteSessionByUserId(ctx, *session.UserId) - if err != nil { - return err - } - return nil -}