Refactor and improve code consistency across multiple files

- Enhanced SQL queries in `session.go` and `share.go` for clarity and consistency.
- Updated comments for better understanding and maintenance.
- Ensured consistent error handling and return statements across various methods.
This commit is contained in:
M1ngdaXie
2026-02-04 22:01:47 -08:00
parent 0f4cff89a2
commit c84cbafb2c
18 changed files with 629 additions and 631 deletions

2
.gitignore vendored
View File

@@ -33,3 +33,5 @@ build/
# Docker volumes and data # Docker volumes and data
postgres_data/ postgres_data/
.claude/

View File

@@ -142,7 +142,7 @@ func GetUserFromContext(c *gin.Context) *uuid.UUID {
// 修正点:使用和存入时完全一样的 Key // 修正点:使用和存入时完全一样的 Key
val, exists := c.Get(ContextUserIDKey) val, exists := c.Get(ContextUserIDKey)
fmt.Println("within getFromContext the id is ... ") fmt.Println("within getFromContext the id is ... ")
fmt.Println(val); fmt.Println(val)
if !exists { if !exists {
return nil return nil
} }

View File

@@ -19,7 +19,6 @@ import (
return &DocumentHandler{store: s} return &DocumentHandler{store: s}
} }
// CreateDocument creates a new document (requires auth) // CreateDocument creates a new document (requires auth)
func (h *DocumentHandler) CreateDocument(c *gin.Context) { func (h *DocumentHandler) CreateDocument(c *gin.Context) {
userID := auth.GetUserFromContext(c) userID := auth.GetUserFromContext(c)
@@ -66,7 +65,6 @@ func (h *DocumentHandler) CreateDocument(c *gin.Context) {
}) })
} }
func (h *DocumentHandler) GetDocument(c *gin.Context) { func (h *DocumentHandler) GetDocument(c *gin.Context) {
id, err := uuid.Parse(c.Param("id")) id, err := uuid.Parse(c.Param("id"))
if err != nil { if err != nil {
@@ -104,6 +102,7 @@ func (h *DocumentHandler) CreateDocument(c *gin.Context) {
c.JSON(http.StatusOK, doc) c.JSON(http.StatusOK, doc)
} }
// GetDocumentState returns the Yjs state for a document // GetDocumentState returns the Yjs state for a document
// GetDocumentState retrieves document state (requires view permission) // GetDocumentState retrieves document state (requires view permission)
func (h *DocumentHandler) GetDocumentState(c *gin.Context) { func (h *DocumentHandler) GetDocumentState(c *gin.Context) {
@@ -290,7 +289,9 @@ func (h *DocumentHandler) GetDocumentPermission(c *gin.Context) {
} }
tokenPerm = p tokenPerm = p
// 处理数据库老数据的 fallback // 处理数据库老数据的 fallback
if tokenPerm == "" { tokenPerm = "view" } if tokenPerm == "" {
tokenPerm = "view"
}
} }
} }

View File

@@ -153,6 +153,7 @@ func (h *ShareHandler) DeleteShare(c *gin.Context) {
c.Status(204) c.Status(204)
} }
// CreateShareLink generates a public share link // CreateShareLink generates a public share link
func (h *ShareHandler) CreateShareLink(c *gin.Context) { func (h *ShareHandler) CreateShareLink(c *gin.Context) {
documentID, err := uuid.Parse(c.Param("id")) documentID, err := uuid.Parse(c.Param("id"))

View File

@@ -24,7 +24,6 @@ type Document struct {
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
} }
type CreateDocumentRequest struct { type CreateDocumentRequest struct {
Name string `json:"name" binding:"required"` Name string `json:"name" binding:"required"`
Type DocumentType `json:"type" binding:"required"` Type DocumentType `json:"type" binding:"required"`
@@ -38,4 +37,3 @@ type CreateDocumentRequest struct {
Documents []Document `json:"documents"` Documents []Document `json:"documents"`
Total int `json:"total"` Total int `json:"total"`
} }

View File

@@ -53,11 +53,9 @@ type Store interface {
GetDocumentVersion(ctx context.Context, versionID uuid.UUID) (*models.DocumentVersion, error) GetDocumentVersion(ctx context.Context, versionID uuid.UUID) (*models.DocumentVersion, error)
GetLatestDocumentVersion(ctx context.Context, documentID uuid.UUID) (*models.DocumentVersion, error) GetLatestDocumentVersion(ctx context.Context, documentID uuid.UUID) (*models.DocumentVersion, error)
Close() error Close() error
} }
type PostgresStore struct { type PostgresStore struct {
db *sql.DB db *sql.DB
} }
@@ -100,7 +98,6 @@ func (s *PostgresStore) CreateDocument(name string, docType models.DocumentType)
doc.Type, doc.Type,
doc.CreatedAt, doc.CreatedAt,
doc.UpdatedAt, doc.UpdatedAt,
).Scan(&doc.ID, &doc.Name, &doc.Type, &doc.CreatedAt, &doc.UpdatedAt) ).Scan(&doc.ID, &doc.Name, &doc.Type, &doc.CreatedAt, &doc.UpdatedAt)
if err != nil { if err != nil {
@@ -140,7 +137,6 @@ func (s *PostgresStore) CreateDocument(name string, docType models.DocumentType)
return doc, nil return doc, nil
} }
// ListDocuments retrieves all documents // ListDocuments retrieves all documents
func (s *PostgresStore) ListDocuments() ([]models.Document, error) { func (s *PostgresStore) ListDocuments() ([]models.Document, error) {
query := ` query := `