| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package words
- import (
- "context"
- v1 "star/api/words/v1"
- "star/internal/dao"
- "star/internal/model/do"
- "star/internal/model/entity"
- "github.com/gogf/gf/v2/errors/gerror"
- )
- type Words struct {
- }
- func New() *Words {
- return &Words{}
- }
- type CreateInput struct {
- Uid uint
- Word string
- Definition string
- ExampleSentence string
- ChineseTranslation string
- Pronunciation string
- ProficiencyLevel v1.ProficiencyLevel
- }
- func (w *Words) Create(ctx context.Context, in CreateInput) error {
- var cls = dao.Words.Columns()
- count, err := dao.Words.Ctx(ctx).
- Where(cls.Uid, in.Uid).
- Where(cls.Word, in.Word).Count()
- if err != nil {
- return err
- }
- if count > 0 {
- return gerror.New("单词已存在")
- }
- _, err = dao.Words.Ctx(ctx).Data(do.Words{
- Uid: in.Uid,
- Word: in.Word,
- Definition: in.Definition,
- ExampleSentence: in.ExampleSentence,
- ChineseTranslation: in.ChineseTranslation,
- Pronunciation: in.Pronunciation,
- ProficiencyLevel: in.ProficiencyLevel,
- }).Insert()
- if err != nil {
- return err
- }
- return nil
- }
- type UpdateInput struct {
- Uid uint
- Word string
- Definition string
- ExampleSentence string
- ChineseTranslation string
- Pronunciation string
- ProficiencyLevel v1.ProficiencyLevel
- }
- func (w *Words) Update(ctx context.Context, id uint, in UpdateInput) error {
- var cls = dao.Words.Columns()
- count, err := dao.Words.Ctx(ctx).
- Where(cls.Uid, in.Uid).
- Where(cls.Word, in.Word).
- WhereNot(cls.Id, id).Count()
- if err != nil {
- return err
- }
- if count > 0 {
- return gerror.New("单词已存在")
- }
- _, err = dao.Words.Ctx(ctx).Data(do.Words{
- Id: id,
- Word: in.Word,
- Definition: in.Definition,
- ExampleSentence: in.ExampleSentence,
- ChineseTranslation: in.ChineseTranslation,
- Pronunciation: in.Pronunciation,
- ProficiencyLevel: in.ProficiencyLevel,
- }).Where(cls.Id, id).Where(cls.Uid, in.Uid).Update()
- if err != nil {
- return err
- }
- return nil
- }
- type ListInput struct {
- Uid uint
- Word string
- Page int
- Size int
- }
- func (w *Words) List(ctx context.Context, in ListInput) (list []entity.Words, total int, err error) {
- // 对于查询初始值的处理
- if in.Page == 0 {
- in.Page = 1
- }
- if in.Size == 0 {
- in.Size = 15
- }
- var (
- cls = dao.Words.Columns()
- orm = dao.Words.Ctx(ctx)
- )
- // 组成查询链
- if in.Uid > 0 {
- orm = orm.Where(cls.Uid, in.Uid)
- }
- // 模糊查询
- if len(in.Word) != 0 {
- orm = orm.WhereLike(cls.Word, "%"+in.Word+"%")
- }
- orm = orm.OrderDesc(cls.CreatedAt).OrderDesc(cls.Id).Page(in.Page, in.Size)
- if err = orm.ScanAndCount(&list, &total, true); err != nil {
- return
- }
- return
- }
- func (w *Words) Detail(ctx context.Context, uid, id uint) (word *entity.Words, err error) {
- var (
- cls = dao.Words.Columns()
- orm = dao.Words.Ctx(ctx)
- )
- orm = orm.Where(cls.Id, id)
- if uid > 0 {
- orm = orm.Where(cls.Uid, uid)
- }
- err = orm.Scan(&word)
- return
- }
- func (w *Words) Delete(ctx context.Context, uid, id uint) (err error) {
- var (
- cls = dao.Words.Columns()
- orm = dao.Words.Ctx(ctx)
- )
- orm = orm.Where(cls.Id, id)
- if uid > 0 {
- orm = orm.Where(cls.Uid, uid)
- }
- _, err = orm.Delete()
- return
- }
|