mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-04 02:32:57 +00:00
hot fix #1
This commit is contained in:
@@ -170,20 +170,28 @@ func (cc *CommentController) React(c *gin.Context) {
|
||||
case uint:
|
||||
userID = v
|
||||
case int:
|
||||
if v > 0 { userID = uint(v) }
|
||||
if v > 0 {
|
||||
userID = uint(v)
|
||||
}
|
||||
case int64:
|
||||
if v > 0 { userID = uint(v) }
|
||||
if v > 0 {
|
||||
userID = uint(v)
|
||||
}
|
||||
case float64:
|
||||
if v > 0 { userID = uint(v) }
|
||||
if v > 0 {
|
||||
userID = uint(v)
|
||||
}
|
||||
case string:
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(v)); err == nil && n > 0 { userID = uint(n) }
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(v)); err == nil && n > 0 {
|
||||
userID = uint(n)
|
||||
}
|
||||
}
|
||||
if userID == 0 {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
// Robust upsert without relying on a DB unique constraint: delete then insert in a transaction
|
||||
// Robust upsert with proper constraint handling
|
||||
if err := cc.DB.Transaction(func(tx *gorm.DB) error {
|
||||
// Remove any previous reaction by this user on this comment
|
||||
if err := tx.Where("comment_id = ? AND user_id = ?", cm.ID, userID).Delete(&models.CommentReaction{}).Error; err != nil {
|
||||
@@ -192,6 +200,17 @@ func (cc *CommentController) React(c *gin.Context) {
|
||||
// Insert the new reaction
|
||||
r := models.CommentReaction{CommentID: cm.ID, UserID: userID, Type: rt}
|
||||
if err := tx.Create(&r).Error; err != nil {
|
||||
// Check for unique constraint violation (PostgreSQL error code 23505)
|
||||
if strings.Contains(err.Error(), "duplicate key") || strings.Contains(err.Error(), "unique constraint") {
|
||||
// If we get a duplicate key error, try to update the existing reaction
|
||||
updateErr := tx.Model(&models.CommentReaction{}).
|
||||
Where("comment_id = ? AND user_id = ?", cm.ID, userID).
|
||||
Update("type", rt).Error
|
||||
if updateErr != nil {
|
||||
return updateErr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -221,13 +240,21 @@ func (cc *CommentController) Unreact(c *gin.Context) {
|
||||
case uint:
|
||||
userID = v
|
||||
case int:
|
||||
if v > 0 { userID = uint(v) }
|
||||
if v > 0 {
|
||||
userID = uint(v)
|
||||
}
|
||||
case int64:
|
||||
if v > 0 { userID = uint(v) }
|
||||
if v > 0 {
|
||||
userID = uint(v)
|
||||
}
|
||||
case float64:
|
||||
if v > 0 { userID = uint(v) }
|
||||
if v > 0 {
|
||||
userID = uint(v)
|
||||
}
|
||||
case string:
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(v)); err == nil && n > 0 { userID = uint(n) }
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(v)); err == nil && n > 0 {
|
||||
userID = uint(n)
|
||||
}
|
||||
}
|
||||
if userID == 0 {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
|
||||
Reference in New Issue
Block a user