feat: dbcontext abstraction via dependency

This commit is contained in:
2025-06-21 02:27:23 +03:00
parent 03c072e67b
commit 0c4d618fa4
7 changed files with 112 additions and 4 deletions

View File

@@ -24,6 +24,7 @@ import (
"easywish/config" "easywish/config"
docs "easywish/docs" docs "easywish/docs"
"easywish/internal/controllers" "easywish/internal/controllers"
"easywish/internal/database"
"easywish/internal/logger" "easywish/internal/logger"
"easywish/internal/routes" "easywish/internal/routes"
"easywish/internal/services" "easywish/internal/services"
@@ -43,6 +44,7 @@ func main() {
logger.NewLogger, logger.NewLogger,
gin.Default, gin.Default,
), ),
database.Module,
services.Module, services.Module,
controllers.Module, controllers.Module,
routes.Module, routes.Module,

View File

@@ -128,6 +128,17 @@ const docTemplate = `{
"Auth" "Auth"
], ],
"summary": "Register an account", "summary": "Register an account",
"parameters": [
{
"description": "desc",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/models.RegistrationBeginRequest"
}
}
],
"responses": {} "responses": {}
} }
}, },
@@ -310,6 +321,21 @@ const docTemplate = `{
"type": "string" "type": "string"
} }
} }
},
"models.RegistrationBeginRequest": {
"type": "object",
"properties": {
"email": {
"type": "string"
},
"password": {
"description": "TODO: password checking",
"type": "string"
},
"username": {
"type": "string"
}
}
} }
}, },
"securityDefinitions": { "securityDefinitions": {

View File

@@ -124,6 +124,17 @@
"Auth" "Auth"
], ],
"summary": "Register an account", "summary": "Register an account",
"parameters": [
{
"description": "desc",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/models.RegistrationBeginRequest"
}
}
],
"responses": {} "responses": {}
} }
}, },
@@ -306,6 +317,21 @@
"type": "string" "type": "string"
} }
} }
},
"models.RegistrationBeginRequest": {
"type": "object",
"properties": {
"email": {
"type": "string"
},
"password": {
"description": "TODO: password checking",
"type": "string"
},
"username": {
"type": "string"
}
}
} }
}, },
"securityDefinitions": { "securityDefinitions": {

View File

@@ -21,6 +21,16 @@ definitions:
refresh_token: refresh_token:
type: string type: string
type: object type: object
models.RegistrationBeginRequest:
properties:
email:
type: string
password:
description: 'TODO: password checking'
type: string
username:
type: string
type: object
info: info:
contact: {} contact: {}
description: Easy and feature-rich wishlist. description: Easy and feature-rich wishlist.
@@ -97,6 +107,13 @@ paths:
post: post:
consumes: consumes:
- application/json - application/json
parameters:
- description: desc
in: body
name: request
required: true
schema:
$ref: '#/definitions/models.RegistrationBeginRequest'
produces: produces:
- application/json - application/json
responses: {} responses: {}

View File

@@ -1,6 +1,7 @@
package controllers package controllers
import ( import (
"easywish/internal/models"
"easywish/internal/services" "easywish/internal/services"
"net/http" "net/http"
@@ -72,9 +73,25 @@ func (a *authControllerImpl) Refresh(c *gin.Context) {
// @Tags Auth // @Tags Auth
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Param request body models.RegistrationBeginRequest true "desc"
// @Router /auth/registrationBegin [post] // @Router /auth/registrationBegin [post]
func (a *authControllerImpl) RegistrationBegin(c *gin.Context) { 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. // RegistrationBegin implements AuthController.

View File

@@ -9,4 +9,5 @@ var (
ErrUsernameTaken = errors.New("Provided username is already in use") ErrUsernameTaken = errors.New("Provided username is already in use")
ErrInvalidCredentials = errors.New("Invalid username, password or TOTP code") ErrInvalidCredentials = errors.New("Invalid username, password or TOTP code")
ErrInvalidToken = errors.New("Token is invalid or expired") ErrInvalidToken = errors.New("Token is invalid or expired")
ErrServerError = errors.New("Internal server error")
) )

View File

@@ -1,10 +1,14 @@
package services package services
import ( import (
"context"
"easywish/internal/database" "easywish/internal/database"
errs "easywish/internal/errors" errs "easywish/internal/errors"
"easywish/internal/logger"
"easywish/internal/models" "easywish/internal/models"
"easywish/internal/utils" "easywish/internal/utils"
"go.uber.org/zap"
) )
type AuthService interface { type AuthService interface {
@@ -15,14 +19,29 @@ type AuthService interface {
} }
type authServiceImpl struct { type authServiceImpl struct {
log logger.Logger
dbctx database.DbContext
} }
func NewAuthService() AuthService { func NewAuthService(_log logger.Logger, _dbctx database.DbContext) AuthService {
return &authServiceImpl{} return &authServiceImpl{log: _log, dbctx: _dbctx}
} }
func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequest) (bool, error) { 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) { func (a *authServiceImpl) RegistrationComplete(request models.RegistrationBeginRequest) (*models.RegistrationCompleteResponse, error) {