Files
AI-Support/backend/internal/models/knowledge.go
2025-09-13 06:48:55 +03:00

86 lines
3.1 KiB
Go

package models
import (
"time"
"gorm.io/gorm"
)
// KnowledgeBase represents a knowledge base entry (FAQ)
type KnowledgeBase struct {
ID uint `json:"id" gorm:"primaryKey"`
Question string `json:"question" gorm:"not null"`
Answer string `json:"answer" gorm:"not null"`
Category string `json:"category" gorm:"not null"`
Tags string `json:"tags"` // Comma-separated tags
Priority int `json:"priority" gorm:"default:1"` // Higher number = higher priority
ViewCount int `json:"viewCount" gorm:"default:0"`
Helpful int `json:"helpful" gorm:"default:0"` // Number of times marked as helpful
NotHelpful int `json:"notHelpful" gorm:"default:0"` // Number of times marked as not helpful
Active bool `json:"active" gorm:"default:true"`
CreatedBy uint `json:"createdBy"`
UpdatedBy uint `json:"updatedBy"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
// Relationships
Creator User `json:"creator" gorm:"foreignKey:CreatedBy"`
Updater User `json:"updater" gorm:"foreignKey:UpdatedBy"`
}
// KnowledgeBaseFeedback represents user feedback on a knowledge base entry
type KnowledgeBaseFeedback struct {
ID uint `json:"id" gorm:"primaryKey"`
KnowledgeBaseID uint `json:"knowledgeBaseId" gorm:"not null"`
UserID uint `json:"userId" gorm:"not null"`
Helpful bool `json:"helpful"` // true if helpful, false if not helpful
Comment string `json:"comment"`
CreatedAt time.Time `json:"createdAt"`
// Relationships
KnowledgeBase KnowledgeBase `json:"knowledgeBase" gorm:"foreignKey:KnowledgeBaseID"`
User User `json:"user" gorm:"foreignKey:UserID"`
}
// BeforeCreate is a GORM hook that validates the knowledge base entry before creation
func (k *KnowledgeBase) BeforeCreate(tx *gorm.DB) error {
if k.Priority < 1 {
k.Priority = 1
}
return nil
}
// BeforeUpdate is a GORM hook that validates the knowledge base entry before update
func (k *KnowledgeBase) BeforeUpdate(tx *gorm.DB) error {
if k.Priority < 1 {
k.Priority = 1
}
return nil
}
// IncrementViewCount increments the view count of a knowledge base entry
func (k *KnowledgeBase) IncrementViewCount(tx *gorm.DB) error {
return tx.Model(k).UpdateColumn("view_count", gorm.Expr("view_count + ?", 1)).Error
}
// MarkHelpful marks a knowledge base entry as helpful
func (k *KnowledgeBase) MarkHelpful(tx *gorm.DB) error {
return tx.Model(k).UpdateColumn("helpful", gorm.Expr("helpful + ?", 1)).Error
}
// MarkNotHelpful marks a knowledge base entry as not helpful
func (k *KnowledgeBase) MarkNotHelpful(tx *gorm.DB) error {
return tx.Model(k).UpdateColumn("not_helpful", gorm.Expr("not_helpful + ?", 1)).Error
}
// GetHelpfulnessPercentage returns the helpfulness percentage
func (k *KnowledgeBase) GetHelpfulnessPercentage() float64 {
total := k.Helpful + k.NotHelpful
if total == 0 {
return 0
}
return float64(k.Helpful) / float64(total) * 100
}