words.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package words
  2. import (
  3. "context"
  4. v1 "star/api/words/v1"
  5. "star/internal/dao"
  6. "star/internal/model/do"
  7. "star/internal/model/entity"
  8. "github.com/gogf/gf/v2/errors/gerror"
  9. )
  10. type Words struct {
  11. }
  12. func New() *Words {
  13. return &Words{}
  14. }
  15. type CreateInput struct {
  16. Uid uint
  17. Word string
  18. Definition string
  19. ExampleSentence string
  20. ChineseTranslation string
  21. Pronunciation string
  22. ProficiencyLevel v1.ProficiencyLevel
  23. }
  24. func (w *Words) Create(ctx context.Context, in CreateInput) error {
  25. var cls = dao.Words.Columns()
  26. count, err := dao.Words.Ctx(ctx).
  27. Where(cls.Uid, in.Uid).
  28. Where(cls.Word, in.Word).Count()
  29. if err != nil {
  30. return err
  31. }
  32. if count > 0 {
  33. return gerror.New("单词已存在")
  34. }
  35. _, err = dao.Words.Ctx(ctx).Data(do.Words{
  36. Uid: in.Uid,
  37. Word: in.Word,
  38. Definition: in.Definition,
  39. ExampleSentence: in.ExampleSentence,
  40. ChineseTranslation: in.ChineseTranslation,
  41. Pronunciation: in.Pronunciation,
  42. ProficiencyLevel: in.ProficiencyLevel,
  43. }).Insert()
  44. if err != nil {
  45. return err
  46. }
  47. return nil
  48. }
  49. type UpdateInput struct {
  50. Uid uint
  51. Word string
  52. Definition string
  53. ExampleSentence string
  54. ChineseTranslation string
  55. Pronunciation string
  56. ProficiencyLevel v1.ProficiencyLevel
  57. }
  58. func (w *Words) Update(ctx context.Context, id uint, in UpdateInput) error {
  59. var cls = dao.Words.Columns()
  60. count, err := dao.Words.Ctx(ctx).
  61. Where(cls.Uid, in.Uid).
  62. Where(cls.Word, in.Word).
  63. WhereNot(cls.Id, id).Count()
  64. if err != nil {
  65. return err
  66. }
  67. if count > 0 {
  68. return gerror.New("单词已存在")
  69. }
  70. _, err = dao.Words.Ctx(ctx).Data(do.Words{
  71. Id: id,
  72. Word: in.Word,
  73. Definition: in.Definition,
  74. ExampleSentence: in.ExampleSentence,
  75. ChineseTranslation: in.ChineseTranslation,
  76. Pronunciation: in.Pronunciation,
  77. ProficiencyLevel: in.ProficiencyLevel,
  78. }).Where(cls.Id, id).Where(cls.Uid, in.Uid).Update()
  79. if err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. type ListInput struct {
  85. Uid uint
  86. Word string
  87. Page int
  88. Size int
  89. }
  90. func (w *Words) List(ctx context.Context, in ListInput) (list []entity.Words, total int, err error) {
  91. // 对于查询初始值的处理
  92. if in.Page == 0 {
  93. in.Page = 1
  94. }
  95. if in.Size == 0 {
  96. in.Size = 15
  97. }
  98. var (
  99. cls = dao.Words.Columns()
  100. orm = dao.Words.Ctx(ctx)
  101. )
  102. // 组成查询链
  103. if in.Uid > 0 {
  104. orm = orm.Where(cls.Uid, in.Uid)
  105. }
  106. // 模糊查询
  107. if len(in.Word) != 0 {
  108. orm = orm.WhereLike(cls.Word, "%"+in.Word+"%")
  109. }
  110. orm = orm.OrderDesc(cls.CreatedAt).OrderDesc(cls.Id).Page(in.Page, in.Size)
  111. if err = orm.ScanAndCount(&list, &total, true); err != nil {
  112. return
  113. }
  114. return
  115. }
  116. func (w *Words) Detail(ctx context.Context, uid, id uint) (word *entity.Words, err error) {
  117. var (
  118. cls = dao.Words.Columns()
  119. orm = dao.Words.Ctx(ctx)
  120. )
  121. orm = orm.Where(cls.Id, id)
  122. if uid > 0 {
  123. orm = orm.Where(cls.Uid, uid)
  124. }
  125. err = orm.Scan(&word)
  126. return
  127. }
  128. func (w *Words) Delete(ctx context.Context, uid, id uint) (err error) {
  129. var (
  130. cls = dao.Words.Columns()
  131. orm = dao.Words.Ctx(ctx)
  132. )
  133. orm = orm.Where(cls.Id, id)
  134. if uid > 0 {
  135. orm = orm.Where(cls.Uid, uid)
  136. }
  137. _, err = orm.Delete()
  138. return
  139. }