Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

New services structure

Smaug#67398839170ac7f0
diff --git a/backend/controllers/authentication.controller.go b/backend/controllers/authentication.controller.go
index f7b1ce37..757eb2d6 100644
--- a/backend/controllers/authentication.controller.go
+++ b/backend/controllers/authentication.controller.go
@@ -21,10 +21,6 @@ type AuthController interface {
RefreshSession(c *gin.Context) (int, any)
}
-type AuthControllerImpl struct {
- app *app.App
-}
-
func NewAuthController(app *app.App) AuthController {
go func() {
for {
@@ -32,7 +28,7 @@ func NewAuthController(app *app.App) AuthController {
time.Sleep(1 * time.Hour) // Sleep for 1 hour
}
}()
- return &AuthControllerImpl{app: app}
+ return &Controller{app: app}
}
// Login
@@ -48,7 +44,7 @@ type AuthClaims struct {
Password string `form:"password" binding:"required"`
}
-func (dc *AuthControllerImpl) Login(c *gin.Context) (int, any) {
+func (dc *Controller) Login(c *gin.Context) (int, any) {
var authClaims AuthClaims
if err := c.ShouldBind(&authClaims); err != nil {
return http.StatusBadRequest, err
@@ -108,7 +104,7 @@ func (dc *AuthControllerImpl) Login(c *gin.Context) (int, any) {
// @Success 200 {object} Success([]models.User)
// @Failure 400 {object} Error
// @Failure 401 {object} Error
-func (dc *AuthControllerImpl) RefreshSession(c *gin.Context) (int, any) {
+func (dc *Controller) RefreshSession(c *gin.Context) (int, any) {
// Get the refresh token from the cookie
refreshToken, err := c.Cookie("RefreshToken")
if err != nil {
@@ -144,7 +140,7 @@ func (dc *AuthControllerImpl) RefreshSession(c *gin.Context) (int, any) {
return http.StatusOK, "Session refreshed successfully."
}
-func (dc *AuthControllerImpl) signAccessToken(user *models.User) (string, error) {
+func (dc *Controller) signAccessToken(user *models.User) (string, error) {
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"sub": strconv.FormatInt(user.Id, 10), // Subject (user identifier)
"iss": "alexandrie", // Issuer
diff --git a/backend/controllers/uploads.controller.go b/backend/controllers/uploads.controller.go
index 4cf941b5..f05c0f70 100644
--- a/backend/controllers/uploads.controller.go
+++ b/backend/controllers/uploads.controller.go
@@ -16,15 +16,10 @@ type UploadController interface {
UploadAvatar(c *gin.Context) (int, any)
UploadFile(c *gin.Context) (int, any)
}
-type UploadControllerImpl struct {
- Controller
-}
func NewUploadController(app *app.App) UploadController {
- return &UploadControllerImpl{
- Controller: Controller{
- app: app,
- },
+ return &Controller{
+ app: app,
}
}
diff --git a/backend/controllers/users.controller.go b/backend/controllers/users.controller.go
index 1e848595..232f7ead 100644
--- a/backend/controllers/users.controller.go
+++ b/backend/controllers/users.controller.go
@@ -3,7 +3,6 @@ package controllers
import (
"Smaug6739/Alexandrie/app"
"Smaug6739/Alexandrie/models"
- "Smaug6739/Alexandrie/services"
"Smaug6739/Alexandrie/utils"
"errors"
"net/http"
@@ -21,17 +20,10 @@ type UserController interface {
UpdatePassword(c *gin.Context) (int, any)
DeleteUser(c *gin.Context) (int, any)
}
-type UserControllerImpl struct {
- Controller
- user_service services.UserService
-}
func NewUserController(app *app.App) UserController {
- return &UserControllerImpl{
- user_service: services.NewUserService(app.DB),
- Controller: Controller{
- app: app,
- },
+ return &Controller{
+ app: app,
}
}
@@ -43,8 +35,8 @@ func NewUserController(app *app.App) UserController {
// @Success 200 {object} Success([]models.User)
// @Failure 400 {object} Error
// @Failure 401 {object} Error
-func (ctr *UserControllerImpl) GetUsers(c *gin.Context) (int, any) {
- users, err := ctr.user_service.GetAllUsers()
+func (ctr *Controller) GetUsers(c *gin.Context) (int, any) {
+ users, err := ctr.app.Services.UserService.GetAllUsers()
if err != nil {
return http.StatusInternalServerError, err
}
@@ -60,12 +52,12 @@ func (ctr *UserControllerImpl) GetUsers(c *gin.Context) (int, any) {
// @Success 200 {object} Success(models.User)
// @Failure 400 {object} Error
// @Failure 401 {object} Error
-func (ctr *UserControllerImpl) GetUserById(c *gin.Context) (int, any) {
+func (ctr *Controller) GetUserById(c *gin.Context) (int, any) {
id, err := utils.SelfOrPermission(c, utils.ADMINISTRATOR)
if err != nil {
return http.StatusBadRequest, err
}
- user, err := ctr.user_service.GetUserById(id)
+ user, err := ctr.app.Services.UserService.GetUserById(id)
if err != nil {
return http.StatusInternalServerError, err
}
@@ -83,7 +75,7 @@ func (ctr *UserControllerImpl) GetUserById(c *gin.Context) (int, any) {
// @Body Username*, Firstname, Lastname, Avatar, Role, Email*, Password*
// @Success 201 {object} Success(models.User)
// @Failure 400 {object} Error
-func (ctr *UserControllerImpl) CreateUser(c *gin.Context) (int, any) {
+func (ctr *Controller) CreateUser(c *gin.Context) (int, any) {
var user models.User
if err := c.ShouldBind(&user); err != nil {
return http.StatusBadRequest, err
@@ -109,12 +101,12 @@ func (ctr *UserControllerImpl) CreateUser(c *gin.Context) (int, any) {
}
// Specific validation for username and password
- exists := ctr.user_service.CheckUsernameExists(user.Username)
+ exists := ctr.app.Services.UserService.CheckUsernameExists(user.Username)
if exists {
return http.StatusBadRequest, errors.New("username already exists")
}
- createdUser, err := ctr.user_service.CreateUser(&user)
+ createdUser, err := ctr.app.Services.UserService.CreateUser(&user)
if err != nil {
return http.StatusInternalServerError, err
}
@@ -132,7 +124,7 @@ func (ctr *UserControllerImpl) CreateUser(c *gin.Context) (int, any) {
// @Success 200 {object} Success(models.User)
// @Failure 400 {object} Error
// @Failure 401 {object} Error
-func (ctr *UserControllerImpl) UpdateUser(c *gin.Context) (int, any) {
+func (ctr *Controller) UpdateUser(c *gin.Context) (int, any) {
id, err := utils.SelfOrPermission(c, utils.ADMINISTRATOR)
if err != nil {
return http.StatusUnauthorized, err
@@ -142,7 +134,7 @@ func (ctr *UserControllerImpl) UpdateUser(c *gin.Context) (int, any) {
return http.StatusBadRequest, err
}
- dbUser, err := ctr.user_service.GetUserById(id)
+ dbUser, err := ctr.app.Services.UserService.GetUserById(id)
if err != nil || dbUser == nil {
return http.StatusInternalServerError, err
}
@@ -159,7 +151,7 @@ func (ctr *UserControllerImpl) UpdateUser(c *gin.Context) (int, any) {
UpdatedTimestamp: time.Now().UnixMilli(),
}
- updatedUser, err := ctr.user_service.UpdateUser(id, &user)
+ updatedUser, err := ctr.app.Services.UserService.UpdateUser(id, &user)
if err != nil {
return http.StatusInternalServerError, err
}
@@ -176,7 +168,7 @@ func (ctr *UserControllerImpl) UpdateUser(c *gin.Context) (int, any) {
// @Success 200 {object} Success(string)
// @Failure 400 {object} Error
// @Failure 401 {object} Error
-func (ctr *UserControllerImpl) UpdatePassword(c *gin.Context) (int, any) {
+func (ctr *Controller) UpdatePassword(c *gin.Context) (int, any) {
id, err := utils.SelfOrPermission(c, utils.ADMINISTRATOR)
if err != nil {
return http.StatusUnauthorized, err
@@ -195,7 +187,7 @@ func (ctr *UserControllerImpl) UpdatePassword(c *gin.Context) (int, any) {
if err != nil {
return http.StatusInternalServerError, errors.New("failed to hash password")
}
- err = ctr.user_service.UpdatePassword(id, string(hash))
+ err = ctr.app.Services.UserService.UpdatePassword(id, string(hash))
if err != nil {
return http.StatusInternalServerError, err
}
@@ -211,13 +203,13 @@ func (ctr *UserControllerImpl) UpdatePassword(c *gin.Context) (int, any) {
// @Success 200 {object} Success(string)
// @Failure 400 {object} Error
// @Failure 401 {object} Error
-func (ctr *UserControllerImpl) DeleteUser(c *gin.Context) (int, any) {
+func (ctr *Controller) DeleteUser(c *gin.Context) (int, any) {
id, err := utils.SelfOrPermission(c, utils.ADMINISTRATOR)
if err != nil {
return http.StatusUnauthorized, err
}
- err = ctr.user_service.DeleteUser(id)
+ err = ctr.app.Services.UserService.DeleteUser(id)
if err != nil {
return http.StatusInternalServerError, err
}