初始化项目框架,完成部分接口开发
This commit is contained in:
68
internal/logic/read_record/read_record.go
Normal file
68
internal/logic/read_record/read_record.go
Normal file
@ -0,0 +1,68 @@
|
||||
package read_record
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
)
|
||||
|
||||
type sReadRecord struct{}
|
||||
|
||||
func New() service.IReadRecord {
|
||||
return &sReadRecord{}
|
||||
}
|
||||
func init() {
|
||||
service.RegisterReadRecord(New())
|
||||
}
|
||||
|
||||
// List retrieves a paginated list of read records
|
||||
func (s *sReadRecord) List(ctx context.Context, in *model.ReadRecordListIn) (out *model.ReadRecordListOut, err error) {
|
||||
out = &model.ReadRecordListOut{}
|
||||
m := dao.ReadRecords.Ctx(ctx)
|
||||
if in.UserId != 0 {
|
||||
m = m.Where(dao.ReadRecords.Columns().UserId, in.UserId)
|
||||
}
|
||||
if in.BookId != 0 {
|
||||
m = m.Where(dao.ReadRecords.Columns().BookId, in.BookId)
|
||||
}
|
||||
if in.ChapterId != 0 {
|
||||
m = m.Where(dao.ReadRecords.Columns().ChapterId, in.ChapterId)
|
||||
}
|
||||
if err = m.Page(in.Page, in.Size).ScanAndCount(&out.List, &out.Total, false); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Create adds a new read record
|
||||
func (s *sReadRecord) Create(ctx context.Context, in *model.ReadRecordAddIn) (out *model.ReadRecordCRUDOut, err error) {
|
||||
if _, err := dao.ReadRecords.Ctx(ctx).Data(do.ReadRecords{
|
||||
UserId: in.UserId,
|
||||
BookId: in.BookId,
|
||||
ChapterId: in.ChapterId,
|
||||
}).Insert(); err != nil {
|
||||
return nil, ecode.Fail.Sub("read_record_create_failed")
|
||||
}
|
||||
return &model.ReadRecordCRUDOut{Success: true}, nil
|
||||
}
|
||||
|
||||
// Delete removes a read record by id
|
||||
func (s *sReadRecord) Delete(ctx context.Context, in *model.ReadRecordDelIn) (out *model.ReadRecordCRUDOut, err error) {
|
||||
exist, err := dao.ReadRecords.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("read_record_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("read_record_not_found")
|
||||
}
|
||||
_, err = dao.ReadRecords.Ctx(ctx).WherePri(in.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("read_record_delete_failed")
|
||||
}
|
||||
return &model.ReadRecordCRUDOut{Success: true}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user