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.

Doc service & fixes

Smaug#6739edb09e9531f6
diff --git a/backend/app/app.go b/backend/app/app.go
index c1e77b5b..49516624 100644
--- a/backend/app/app.go
+++ b/backend/app/app.go
@@ -30,9 +30,10 @@ type Config struct {
}
type Services struct {
- User services.UserService
- Session services.AuthService
- Log services.LogService
+ User services.UserService
+ Session services.AuthService
+ Log services.LogService
+ Document services.DocumentService
}
type App struct {
@@ -50,9 +51,10 @@ func InitApp(config Config) *App {
app.Snowflake = utils.NewSnowflake(1609459200000)
app.Config = config
app.Services = Services{
- User: services.NewUserService(app.DB),
- Session: services.NewAuthService(app.DB),
- Log: services.NewLogService(app.DB),
+ User: services.NewUserService(app.DB),
+ Session: services.NewAuthService(app.DB),
+ Log: services.NewLogService(app.DB),
+ Document: services.NewDocumentService(app.DB),
}
Migrate(&config)
diff --git a/backend/controllers/documents.controller.go b/backend/controllers/documents.controller.go
new file mode 100644
index 00000000..6f37804a
--- /dev/null
+++ b/backend/controllers/documents.controller.go
@@ -0,0 +1,39 @@
+package controllers
+
+import (
+ "alexandrie/app"
+ "alexandrie/utils"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+)
+
+type DocumentController interface {
+ GetDocuments(c *gin.Context) (int, any)
+}
+
+func NewDocumentController(app *app.App) DocumentController {
+ return &Controller{
+ app: app,
+ }
+}
+
+// Get Users
+// @Summary Get all users
+// @Method GET
+// @Router /users [get]
+// @Security Authenfification: Auth, Admin
+// @Success 200 {object} Success([]models.User)
+// @Failure 400 {object} Error
+// @Failure 401 {object} Error
+func (ctr *Controller) GetDocuments(c *gin.Context) (int, any) {
+ id, err := utils.SelfOrPermission(c, utils.ADMINISTRATOR, "userId")
+ if err != nil {
+ return http.StatusUnauthorized, err
+ }
+ documents, err := ctr.app.Services.Document.GetAllDocuments(id)
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ return http.StatusOK, documents
+}
diff --git a/backend/router/router.go b/backend/router/router.go
index d9c1b1ad..582252f7 100644
--- a/backend/router/router.go
+++ b/backend/router/router.go
@@ -13,5 +13,6 @@ func InitRouter(app *app.App) *gin.Engine {
routes.Users(app, mainGroup)
routes.Auth(app, mainGroup)
routes.Uploads(app, mainGroup)
+ routes.Documents(app, mainGroup)
return router
}
diff --git a/backend/router/routes/documents.go b/backend/router/routes/documents.go
new file mode 100644
index 00000000..b59289d7
--- /dev/null
+++ b/backend/router/routes/documents.go
@@ -0,0 +1,17 @@
+package routes
+
+import (
+ "alexandrie/app"
+ "alexandrie/controllers"
+ "alexandrie/middlewares"
+ "alexandrie/utils"
+
+ "github.com/gin-gonic/gin"
+)
+
+func Documents(app *app.App, router *gin.RouterGroup) {
+ doc := router.Group("/documents")
+ docCtrl := controllers.NewDocumentController(app)
+
+ doc.GET("/:userId", middlewares.Auth(), utils.WP(docCtrl.GetDocuments))
+}
diff --git a/backend/router/routes/users.go b/backend/router/routes/users.go
index c2a8acb2..16545694 100644
--- a/backend/router/routes/users.go
+++ b/backend/router/routes/users.go
@@ -13,8 +13,6 @@ func Users(app *app.App, router *gin.RouterGroup) {
usr := router.Group("/users")
usrCtrl := controllers.NewUserController(app)
- usr.Use()
-
usr.GET("/", middlewares.Auth(), middlewares.Admin(), utils.WP(usrCtrl.GetUsers))
usr.GET("/:id", middlewares.Auth(), utils.WP(usrCtrl.GetUserById))
usr.POST("/", utils.WP(usrCtrl.CreateUser))
diff --git a/backend/services/document.service.go b/backend/services/document.service.go
new file mode 100644
index 00000000..6f140810
--- /dev/null
+++ b/backend/services/document.service.go
@@ -0,0 +1,34 @@
+package services
+
+import (
+ "alexandrie/models"
+ "database/sql"
+)
+
+type DocumentService interface {
+ GetAllDocuments(userId int64) ([]*models.Document, error)
+}
+
+func NewDocumentService(db *sql.DB) DocumentService {
+ return &Service{db: db}
+}
+
+func (s *Service) GetAllDocuments(userId int64) ([]*models.Document, error) {
+ var documents []*models.Document
+ rows, err := s.db.Query("SELECT id, name, description, tags, category, parent_id, accessibility, author_id, created_timestamp, updated_timestamp FROM documents WHERE author_id = ? ORDER BY name", userId)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+
+ for rows.Next() {
+ var document models.Document
+ if err := rows.Scan(&document.Id, &document.Name, &document.Description,
+ &document.Tags, &document.Category, &document.ParentId, &document.Accessibility,
+ &document.AuthorId, &document.CreatedTimestamp, &document.UpdatedTimestamp); err != nil {
+ return nil, err
+ }
+ documents = append(documents, &document)
+ }
+ return documents, nil
+}
diff --git a/backend/utils/permissions.go b/backend/utils/permissions.go
index 2f6077b9..d387fb31 100644
--- a/backend/utils/permissions.go
+++ b/backend/utils/permissions.go
@@ -46,12 +46,16 @@ func GetUserIdCtx(ctx *gin.Context) (int64, error) {
return id, nil
}
-func SelfOrPermission(ctx *gin.Context, allowed int) (int64, error) {
+func SelfOrPermission(ctx *gin.Context, allowed int, key ...string) (int64, error) {
// Check if the c.Param("id") is "@me" or c.Param("id") is the same as the user ID in the context
// If not, check if the user has the required permission
// If one of the above is true, return the user ID from the context
// Otherwise, return an error
- id_param, err := GetUserIdParam(ctx.Param("id"), ctx)
+ search_key := "id"
+ if len(key) > 0 {
+ search_key = key[0]
+ }
+ id_param, err := GetUserIdParam(ctx.Param(search_key), ctx)
if err != nil {
return 0, err
}