feat: dbcontext abstraction via dependency
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"easywish/internal/models"
|
||||
"easywish/internal/services"
|
||||
"net/http"
|
||||
|
||||
@@ -72,9 +73,25 @@ func (a *authControllerImpl) Refresh(c *gin.Context) {
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.RegistrationBeginRequest true "desc"
|
||||
// @Router /auth/registrationBegin [post]
|
||||
func (a *authControllerImpl) RegistrationBegin(c *gin.Context) {
|
||||
c.Status(http.StatusNotImplemented)
|
||||
|
||||
var request models.RegistrationBeginRequest
|
||||
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err := a.authService.RegistrationBegin(request)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusAccepted)
|
||||
return
|
||||
}
|
||||
|
||||
// RegistrationBegin implements AuthController.
|
||||
|
||||
@@ -9,4 +9,5 @@ var (
|
||||
ErrUsernameTaken = errors.New("Provided username is already in use")
|
||||
ErrInvalidCredentials = errors.New("Invalid username, password or TOTP code")
|
||||
ErrInvalidToken = errors.New("Token is invalid or expired")
|
||||
ErrServerError = errors.New("Internal server error")
|
||||
)
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"easywish/internal/database"
|
||||
errs "easywish/internal/errors"
|
||||
"easywish/internal/logger"
|
||||
"easywish/internal/models"
|
||||
"easywish/internal/utils"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type AuthService interface {
|
||||
@@ -15,14 +19,29 @@ type AuthService interface {
|
||||
}
|
||||
|
||||
type authServiceImpl struct {
|
||||
log logger.Logger
|
||||
dbctx database.DbContext
|
||||
}
|
||||
|
||||
func NewAuthService() AuthService {
|
||||
return &authServiceImpl{}
|
||||
func NewAuthService(_log logger.Logger, _dbctx database.DbContext) AuthService {
|
||||
return &authServiceImpl{log: _log, dbctx: _dbctx}
|
||||
}
|
||||
|
||||
func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequest) (bool, error) {
|
||||
return false, errs.ErrNotImplemented
|
||||
|
||||
ctx := context.Background()
|
||||
queries := database.New(a.dbctx)
|
||||
user, err := queries.CreateUser(ctx, request.Username) // TODO: validation
|
||||
|
||||
if err != nil {
|
||||
a.log.Get().Error("Failed to add user to database", zap.Error(err))
|
||||
return false, errs.ErrServerError
|
||||
}
|
||||
a.log.Get().Info("Registered a new user", zap.String("username", user.Username))
|
||||
|
||||
// TODO: Send verification email
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (a *authServiceImpl) RegistrationComplete(request models.RegistrationBeginRequest) (*models.RegistrationCompleteResponse, error) {
|
||||
|
||||
Reference in New Issue
Block a user