Procházet zdrojové kódy

feat: add memo count for words

RegMs If před 3 roky
rodič
revize
cf58e9663b
5 změnil soubory, kde provedl 98 přidání a 25 odebrání
  1. 40 0
      controller/memos.go
  2. 3 1
      main.go
  3. 5 8
      model/dicts.go
  4. 27 0
      model/memos.go
  5. 23 16
      model/words.go

+ 40 - 0
controller/memos.go

@@ -0,0 +1,40 @@
+package controller
+
+import (
+	"errors"
+	"net/http"
+	"woord-core-service/model"
+
+	"github.com/gin-gonic/gin"
+)
+
+type CreateMemoRequest struct {
+	WordID uint `form:"wordID" binding:"required"`
+}
+
+// 创建复习记录
+func CreateMemo(c *gin.Context) {
+	var request CreateMemoRequest
+	if err := c.ShouldBind(&request); err != nil {
+		respondError(c, http.StatusBadRequest, err)
+		return
+	}
+
+	_, err := model.GetWord(request.WordID, c.MustGet(AuthUserKey).(uint))
+	if err != nil {
+		if errors.Is(err, model.ErrWordNotFound) {
+			respondError(c, http.StatusNotFound, err)
+		} else {
+			respondUnknownError(c, err)
+		}
+		return
+	}
+
+	err = model.CreateMemo(request.WordID)
+	if err != nil {
+		respondUnknownError(c, err)
+		return
+	}
+
+	respondOK(c, nil)
+}

+ 3 - 1
main.go

@@ -24,7 +24,7 @@ func main() {
 		panic(err)
 	}
 	// 自动迁移 Schema
-	global.DB.AutoMigrate(&model.User{}, &model.Dict{}, &model.Word{})
+	global.DB.AutoMigrate(&model.User{}, &model.Dict{}, &model.Word{}, &model.Memo{})
 
 	r := gin.Default()
 
@@ -54,6 +54,8 @@ func main() {
 		auth.POST("/word/create", controller.CreateWord)
 		auth.PUT("/word/update", controller.UpdateWord)
 		auth.DELETE("/word/delete", controller.DeleteWord)
+
+		auth.POST("/memo/create", controller.CreateMemo)
 	}
 
 	r.Run(":8080")

+ 5 - 8
model/dicts.go

@@ -41,13 +41,13 @@ type DictResult struct {
 	ValueTitle   string `json:"valueTitle"`
 	MeaningTitle string `json:"meaningTitle"`
 	ExtraTitle   string `json:"extraTitle"`
-	WordCount    uint   `json:"wordCount"`
+	WordsCount   uint   `json:"wordsCount"`
 }
 
 // 列出所有词库
 func ListDicts(userID uint) ([]DictResult, error) {
 	result := []DictResult{}
-	if err := global.DB.Model(&Dict{}).Select("*, (?) AS word_count", global.DB.Model(&Word{}).Select("COUNT()").Where("dict_id = dicts.id")).Order("id desc").Find(&result, "user_id = ?", userID).Error; err != nil {
+	if err := global.DB.Model(&Dict{}).Select("*, (?) AS words_count", global.DB.Model(&Word{}).Select("COUNT()").Where("dict_id = dicts.id")).Order("id desc").Find(&result, "user_id = ?", userID).Error; err != nil {
 		return nil, err
 	}
 	return result, nil
@@ -56,7 +56,7 @@ func ListDicts(userID uint) ([]DictResult, error) {
 // 获取词库
 func GetDict(id, userID uint) (*DictResult, error) {
 	result := &DictResult{}
-	if err := global.DB.Model(&Dict{}).Select("*, (?) AS word_count", global.DB.Model(&Word{}).Select("COUNT()").Where("dict_id = dicts.id")).Take(result, "id = ? AND user_id = ?", id, userID).Error; err != nil {
+	if err := global.DB.Model(&Dict{}).Select("*, (?) AS words_count", global.DB.Model(&Word{}).Select("COUNT()").Where("dict_id = dicts.id")).Take(result, "id = ? AND user_id = ?", id, userID).Error; err != nil {
 		if errors.Is(err, gorm.ErrRecordNotFound) {
 			return nil, ErrDictNotFound
 		}
@@ -94,15 +94,12 @@ func UpdateDict(id uint, name, valueTitle, meaningTitle, extraTitle string) (*Di
 	}
 
 	dict := &Dict{
-		Model: &gorm.Model{
-			ID: id,
-		},
 		Name:         name,
 		ValueTitle:   valueTitle,
 		MeaningTitle: meaningTitle,
 		ExtraTitle:   extraTitle,
 	}
-	if err := global.DB.Model(dict).Select("name", "value_title", "meaning_title", "extra_title").Updates(dict).Error; err != nil {
+	if err := global.DB.Where("id = ?", id).Select("name", "value_title", "meaning_title", "extra_title").Updates(dict).Error; err != nil {
 		return nil, err
 	}
 	return &DictResult{
@@ -111,7 +108,7 @@ func UpdateDict(id uint, name, valueTitle, meaningTitle, extraTitle string) (*Di
 		ValueTitle:   dict.ValueTitle,
 		MeaningTitle: dict.MeaningTitle,
 		ExtraTitle:   dict.ExtraTitle,
-		WordCount:    uint(count),
+		WordsCount:   uint(count),
 	}, nil
 }
 

+ 27 - 0
model/memos.go

@@ -0,0 +1,27 @@
+package model
+
+import (
+	"time"
+	"woord-core-service/global"
+
+	"gorm.io/gorm"
+)
+
+// 复习记录
+type Memo struct {
+	ID        uint      `gorm:"primarykey"`
+	CreatedAt time.Time `gorm:"index"`
+	UpdatedAt time.Time
+	DeletedAt gorm.DeletedAt `gorm:"index"`
+
+	// 所属单词 ID
+	WordID uint
+}
+
+// 创建复习记录
+func CreateMemo(wordID uint) error {
+	memo := &Memo{
+		WordID: wordID,
+	}
+	return global.DB.Select("word_id").Create(memo).Error
+}

+ 23 - 16
model/words.go

@@ -29,22 +29,26 @@ type Word struct {
 	// 是否为易错词
 	Star bool
 
+	// 单词所有复习记录
+	Memos []Memo
+
 	// 所属词库 ID
 	DictID uint
 }
 
 type WordResult struct {
-	ID      uint   `json:"id"`
-	Value   string `json:"value"`
-	Meaning string `json:"meaning"`
-	Extra   string `json:"extra"`
-	Star    bool   `json:"star"`
+	ID         uint   `json:"id"`
+	Value      string `json:"value"`
+	Meaning    string `json:"meaning"`
+	Extra      string `json:"extra"`
+	Star       bool   `json:"star"`
+	MemosCount uint   `json:"memosCount"`
 }
 
 // 列出所有单词
 func ListWords(dictID uint) ([]WordResult, error) {
 	result := []WordResult{}
-	if err := global.DB.Model(&Word{}).Order("id desc").Find(&result, "dict_id = ?", dictID).Error; err != nil {
+	if err := global.DB.Model(&Word{}).Select("*, (?) AS memos_count", global.DB.Model(&Memo{}).Select("COUNT()").Where("word_id = words.id")).Order("id desc").Find(&result, "dict_id = ?", dictID).Error; err != nil {
 		return nil, err
 	}
 	return result, nil
@@ -53,7 +57,7 @@ func ListWords(dictID uint) ([]WordResult, error) {
 // 获取单词
 func GetWord(id, userID uint) (*WordResult, error) {
 	result := &WordResult{}
-	if err := global.DB.Model(&Word{}).Take(result, "id = ? AND (?) = ?", id, global.DB.Model(&Dict{}).Select("user_id").Where("id = dict_id"), userID).Error; err != nil {
+	if err := global.DB.Model(&Word{}).Select("*, (?) AS memos_count", global.DB.Model(&Memo{}).Select("COUNT()").Where("word_id = words.id")).Take(result, "id = ? AND (?) = ?", id, global.DB.Model(&Dict{}).Select("user_id").Where("id = dict_id"), userID).Error; err != nil {
 		if errors.Is(err, gorm.ErrRecordNotFound) {
 			return nil, ErrWordNotFound
 		}
@@ -97,24 +101,27 @@ func UpdateWord(id uint, value, meaning, extra string, star bool) (*WordResult,
 		return nil, ErrWordAlreadyExists
 	}
 
+	count := int64(0)
+	if err := global.DB.Model(&Memo{}).Where("word_id = ?", id).Count(&count).Error; err != nil {
+		return nil, err
+	}
+
 	word := &Word{
-		Model: &gorm.Model{
-			ID: id,
-		},
 		Value:   value,
 		Meaning: meaning,
 		Extra:   extra,
 		Star:    star,
 	}
-	if err := global.DB.Model(word).Select("value", "meaning", "extra", "star").Updates(word).Error; err != nil {
+	if err := global.DB.Where("id = ?", id).Select("value", "meaning", "extra", "star").Updates(word).Error; err != nil {
 		return nil, err
 	}
 	return &WordResult{
-		ID:      word.ID,
-		Value:   word.Value,
-		Meaning: word.Meaning,
-		Extra:   word.Extra,
-		Star:    word.Star,
+		ID:         word.ID,
+		Value:      word.Value,
+		Meaning:    word.Meaning,
+		Extra:      word.Extra,
+		Star:       word.Star,
+		MemosCount: uint(count),
 	}, nil
 }