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:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -33,3 +33,5 @@ build/
|
|||||||
|
|
||||||
# Docker volumes and data
|
# Docker volumes and data
|
||||||
postgres_data/
|
postgres_data/
|
||||||
|
|
||||||
|
.claude/
|
||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -181,7 +181,7 @@ func (h *AuthHandler) GithubCallback(c *gin.Context) {
|
|||||||
log.Printf("Failed to parse GitHub response: %v | Data: %s", err, string(data))
|
log.Printf("Failed to parse GitHub response: %v | Data: %s", err, string(data))
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid GitHub response"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid GitHub response"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If email is not public, fetch it separately
|
// If email is not public, fetch it separately
|
||||||
if userInfo.Email == "" {
|
if userInfo.Email == "" {
|
||||||
|
|||||||
@@ -11,16 +11,15 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DocumentHandler struct {
|
type DocumentHandler struct {
|
||||||
store *store.PostgresStore
|
store *store.PostgresStore
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDocumentHandler(s *store.PostgresStore) *DocumentHandler {
|
func NewDocumentHandler(s *store.PostgresStore) *DocumentHandler {
|
||||||
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)
|
||||||
if userID == nil {
|
if userID == nil {
|
||||||
@@ -44,7 +43,7 @@ func (h *DocumentHandler) CreateDocument(c *gin.Context) {
|
|||||||
c.JSON(http.StatusCreated, doc)
|
c.JSON(http.StatusCreated, doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *DocumentHandler) ListDocuments(c *gin.Context) {
|
func (h *DocumentHandler) ListDocuments(c *gin.Context) {
|
||||||
userID := auth.GetUserFromContext(c)
|
userID := auth.GetUserFromContext(c)
|
||||||
fmt.Println("Getting userId, which is : ")
|
fmt.Println("Getting userId, which is : ")
|
||||||
fmt.Println(userID)
|
fmt.Println(userID)
|
||||||
@@ -66,8 +65,7 @@ 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 {
|
||||||
respondBadRequest(c, "Invalid document ID format")
|
respondBadRequest(c, "Invalid document ID format")
|
||||||
@@ -104,8 +102,9 @@ 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 retrieves document state (requires view permission)
|
// GetDocumentState returns the Yjs state for a document
|
||||||
|
// GetDocumentState retrieves document state (requires view permission)
|
||||||
func (h *DocumentHandler) GetDocumentState(c *gin.Context) {
|
func (h *DocumentHandler) GetDocumentState(c *gin.Context) {
|
||||||
id, err := uuid.Parse(c.Param("id"))
|
id, err := uuid.Parse(c.Param("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -143,7 +142,7 @@ func (h *DocumentHandler) GetDocumentState(c *gin.Context) {
|
|||||||
c.Data(http.StatusOK, "application/octet-stream", state)
|
c.Data(http.StatusOK, "application/octet-stream", state)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateDocumentState updates document state (requires edit permission)
|
// UpdateDocumentState updates document state (requires edit permission)
|
||||||
func (h *DocumentHandler) UpdateDocumentState(c *gin.Context) {
|
func (h *DocumentHandler) UpdateDocumentState(c *gin.Context) {
|
||||||
id, err := uuid.Parse(c.Param("id"))
|
id, err := uuid.Parse(c.Param("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -195,7 +194,7 @@ func (h *DocumentHandler) UpdateDocumentState(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{"message": "State updated successfully"})
|
c.JSON(http.StatusOK, gin.H{"message": "State updated successfully"})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteDocument deletes a document (owner only)
|
// DeleteDocument deletes a document (owner only)
|
||||||
func (h *DocumentHandler) DeleteDocument(c *gin.Context) {
|
func (h *DocumentHandler) DeleteDocument(c *gin.Context) {
|
||||||
id, err := uuid.Parse(c.Param("id"))
|
id, err := uuid.Parse(c.Param("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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"))
|
||||||
|
|||||||
@@ -206,8 +206,8 @@ func (s *ShareHandlerSuite) TestListShares_OwnerSeesAll() {
|
|||||||
s.assertSuccessResponse(w, http.StatusOK)
|
s.assertSuccessResponse(w, http.StatusOK)
|
||||||
|
|
||||||
var response models.ShareListResponse
|
var response models.ShareListResponse
|
||||||
s.parseJSONResponse(w, &response)
|
s.parseJSONResponse(w, &response)
|
||||||
shares := response.Shares
|
shares := response.Shares
|
||||||
s.GreaterOrEqual(len(shares), 1, "Should have at least one share")
|
s.GreaterOrEqual(len(shares), 1, "Should have at least one share")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,8 +229,8 @@ func (s *ShareHandlerSuite) TestListShares_EmptyList() {
|
|||||||
s.assertSuccessResponse(w, http.StatusOK)
|
s.assertSuccessResponse(w, http.StatusOK)
|
||||||
|
|
||||||
var response models.ShareListResponse
|
var response models.ShareListResponse
|
||||||
s.parseJSONResponse(w, &response)
|
s.parseJSONResponse(w, &response)
|
||||||
shares := response.Shares
|
shares := response.Shares
|
||||||
s.Equal(0, len(shares), "Should have no shares")
|
s.Equal(0, len(shares), "Should have no shares")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,8 +243,8 @@ func (s *ShareHandlerSuite) TestListShares_IncludesUserDetails() {
|
|||||||
s.assertSuccessResponse(w, http.StatusOK)
|
s.assertSuccessResponse(w, http.StatusOK)
|
||||||
|
|
||||||
var response models.ShareListResponse
|
var response models.ShareListResponse
|
||||||
s.parseJSONResponse(w, &response)
|
s.parseJSONResponse(w, &response)
|
||||||
shares := response.Shares
|
shares := response.Shares
|
||||||
|
|
||||||
if len(shares) > 0 {
|
if len(shares) > 0 {
|
||||||
share := shares[0]
|
share := shares[0]
|
||||||
|
|||||||
@@ -24,18 +24,16 @@ 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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateStateRequest struct {
|
type UpdateStateRequest struct {
|
||||||
State []byte `json:"state" binding:"required"`
|
State []byte `json:"state" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DocumentListResponse struct {
|
type DocumentListResponse struct {
|
||||||
Documents []Document `json:"documents"`
|
Documents []Document `json:"documents"`
|
||||||
Total int `json:"total"`
|
Total int `json:"total"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
@@ -78,7 +76,7 @@ func NewPostgresStore(databaseUrl string) (*PostgresStore, error) {
|
|||||||
|
|
||||||
func (s *PostgresStore) Close() error {
|
func (s *PostgresStore) Close() error {
|
||||||
return s.db.Close()
|
return s.db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PostgresStore) CreateDocument(name string, docType models.DocumentType) (*models.Document, error) {
|
func (s *PostgresStore) CreateDocument(name string, docType models.DocumentType) (*models.Document, error) {
|
||||||
doc := &models.Document{
|
doc := &models.Document{
|
||||||
@@ -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 {
|
||||||
@@ -109,8 +106,8 @@ func (s *PostgresStore) CreateDocument(name string, docType models.DocumentType)
|
|||||||
return doc, nil
|
return doc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDocument retrieves a document by ID
|
// GetDocument retrieves a document by ID
|
||||||
func (s *PostgresStore) GetDocument(id uuid.UUID) (*models.Document, error) {
|
func (s *PostgresStore) GetDocument(id uuid.UUID) (*models.Document, error) {
|
||||||
doc := &models.Document{}
|
doc := &models.Document{}
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
@@ -138,11 +135,10 @@ 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 := `
|
||||||
SELECT id, name, type, created_at, updated_at
|
SELECT id, name, type, created_at, updated_at
|
||||||
FROM documents
|
FROM documents
|
||||||
@@ -166,9 +162,9 @@ func (s *PostgresStore) CreateDocument(name string, docType models.DocumentType)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return documents, nil
|
return documents, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PostgresStore) UpdateDocumentState(id uuid.UUID, state []byte) error {
|
func (s *PostgresStore) UpdateDocumentState(id uuid.UUID, state []byte) error {
|
||||||
query := `
|
query := `
|
||||||
UPDATE documents
|
UPDATE documents
|
||||||
SET yjs_state = $1, updated_at = $2
|
SET yjs_state = $1, updated_at = $2
|
||||||
@@ -190,9 +186,9 @@ func (s *PostgresStore) CreateDocument(name string, docType models.DocumentType)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PostgresStore) DeleteDocument(id uuid.UUID) error {
|
func (s *PostgresStore) DeleteDocument(id uuid.UUID) error {
|
||||||
query := `DELETE FROM documents WHERE id = $1`
|
query := `DELETE FROM documents WHERE id = $1`
|
||||||
|
|
||||||
result, err := s.db.Exec(query, id)
|
result, err := s.db.Exec(query, id)
|
||||||
@@ -210,7 +206,7 @@ func (s *PostgresStore) CreateDocument(name string, docType models.DocumentType)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateDocumentWithOwner creates a new document with owner
|
// CreateDocumentWithOwner creates a new document with owner
|
||||||
func (s *PostgresStore) CreateDocumentWithOwner(name string, docType models.DocumentType, ownerID *uuid.UUID) (*models.Document, error) {
|
func (s *PostgresStore) CreateDocumentWithOwner(name string, docType models.DocumentType, ownerID *uuid.UUID) (*models.Document, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user