refactor: reorganize modules and add Linux build tooling
This commit is contained in:
66
module/base/cloud/internal/logic/note/create_note.go
Normal file
66
module/base/cloud/internal/logic/note/create_note.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package note
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cloud/internal/impl"
|
||||
"bsm/full/module/base/cloud/internal/models"
|
||||
pb "bsm/full/module/base/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/engine/types"
|
||||
)
|
||||
|
||||
// 创建笔记
|
||||
func CreateNote(ctx context.Context, in *pb.CreateNoteRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// valid code
|
||||
if strings.TrimSpace(in.Title) == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
if strings.TrimSpace(in.CloudIdentity) == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
record := models.CloudNote{
|
||||
Std_IICUDS: types.Std_IICUDS{
|
||||
Identity: utils.UUID(),
|
||||
},
|
||||
Std_Passport: types.Std_Passport{
|
||||
PassportID: auth.ID,
|
||||
PassportIdentity: auth.Identity,
|
||||
},
|
||||
CloudBase: models.CloudBase{
|
||||
CloudID: uint(in.CloudId),
|
||||
CloudIdentity: in.CloudIdentity,
|
||||
},
|
||||
Title: in.Title,
|
||||
Content: in.Content,
|
||||
Category: in.Category,
|
||||
Tags: in.Tags,
|
||||
IsMarkdown: in.IsMarkdown,
|
||||
IsPinned: in.IsPinned,
|
||||
IsPrivate: in.IsPrivate,
|
||||
Views: 0,
|
||||
}
|
||||
|
||||
if err := impl.DBService.Create(&record).Error; err != nil {
|
||||
printer.Error("Create note error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: record.Identity,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
56
module/base/cloud/internal/logic/note/delete_attachment.go
Normal file
56
module/base/cloud/internal/logic/note/delete_attachment.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package note
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cloud/internal/impl"
|
||||
"bsm/full/module/base/cloud/internal/models"
|
||||
pb "bsm/full/module/base/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 删除附件
|
||||
func DeleteAttachment(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
var attachment models.NoteAttachment
|
||||
query := impl.DBService.Joins("JOIN cloud_notes ON note_attachments.note_id = cloud_notes.id").
|
||||
Where("cloud_notes.passport_id = ?", auth.ID)
|
||||
|
||||
if in.Id > 0 {
|
||||
query = query.Where("note_attachments.id = ?", in.Id)
|
||||
} else {
|
||||
// 注意:NoteAttachment模型没有identity字段,这里假设通过ID删除
|
||||
query = query.Where("note_attachments.id = ?", in.Id)
|
||||
}
|
||||
|
||||
if err := query.First(&attachment).Error; err != nil {
|
||||
printer.Error("Attachment not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 删除附件
|
||||
if err := impl.DBService.Delete(&attachment).Error; err != nil {
|
||||
printer.Error("Delete attachment error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
60
module/base/cloud/internal/logic/note/delete_note.go
Normal file
60
module/base/cloud/internal/logic/note/delete_note.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package note
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cloud/internal/impl"
|
||||
"bsm/full/module/base/cloud/internal/models"
|
||||
pb "bsm/full/module/base/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 删除笔记
|
||||
func DeleteNote(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
var note models.CloudNote
|
||||
query := impl.DBService.Where("passport_id = ?", auth.ID)
|
||||
|
||||
if in.Id > 0 {
|
||||
query = query.Where("id = ?", in.Id)
|
||||
} else {
|
||||
query = query.Where("identity = ?", in.Identity)
|
||||
}
|
||||
|
||||
if err := query.First(¬e).Error; err != nil {
|
||||
printer.Error("Note not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 删除笔记下的所有附件
|
||||
if err := impl.DBService.Where("note_id = ?", note.ID).Delete(&models.NoteAttachment{}).Error; err != nil {
|
||||
printer.Error("Delete attachments error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 删除笔记
|
||||
if err := impl.DBService.Delete(¬e).Error; err != nil {
|
||||
printer.Error("Delete note error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
74
module/base/cloud/internal/logic/note/get_note.go
Normal file
74
module/base/cloud/internal/logic/note/get_note.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package note
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cloud/internal/impl"
|
||||
"bsm/full/module/base/cloud/internal/models"
|
||||
pb "bsm/full/module/base/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 获取笔记详情
|
||||
func GetNote(ctx context.Context, in *pb.IdentRequest) (reply *pb.CloudNoteItem, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
var note models.CloudNote
|
||||
query := impl.DBService.Where("passport_id = ?", auth.ID)
|
||||
|
||||
if in.Id > 0 {
|
||||
query = query.Where("id = ?", in.Id)
|
||||
} else {
|
||||
query = query.Where("identity = ?", in.Identity)
|
||||
}
|
||||
|
||||
if err := query.Preload("Attachments").First(¬e).Error; err != nil {
|
||||
printer.Error("Note not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 转换附件数据
|
||||
var attachments []*pb.NoteAttachmentItem
|
||||
for _, attachment := range note.Attachments {
|
||||
attachments = append(attachments, &pb.NoteAttachmentItem{
|
||||
Id: uint64(attachment.ID),
|
||||
NoteId: uint64(attachment.NoteID),
|
||||
FileName: attachment.FileName,
|
||||
FilePath: attachment.FilePath,
|
||||
FileSize: attachment.FileSize,
|
||||
MimeType: attachment.MimeType,
|
||||
CreatedAt: attachment.CreatedAt.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
reply = &pb.CloudNoteItem{
|
||||
Id: uint64(note.ID),
|
||||
Identity: note.Identity,
|
||||
Title: note.Title,
|
||||
Content: note.Content,
|
||||
Category: note.Category,
|
||||
Tags: note.Tags,
|
||||
IsMarkdown: note.IsMarkdown,
|
||||
IsPinned: note.IsPinned,
|
||||
IsPrivate: note.IsPrivate,
|
||||
Views: int32(note.Views),
|
||||
CreatedAt: note.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: note.UpdatedAt.Format(time.RFC3339),
|
||||
Attachments: attachments,
|
||||
}
|
||||
|
||||
return reply, nil
|
||||
}
|
||||
56
module/base/cloud/internal/logic/note/increment_views.go
Normal file
56
module/base/cloud/internal/logic/note/increment_views.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package note
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cloud/internal/impl"
|
||||
"bsm/full/module/base/cloud/internal/models"
|
||||
pb "bsm/full/module/base/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 增加浏览次数
|
||||
func IncrementViews(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
var note models.CloudNote
|
||||
query := impl.DBService.Where("passport_id = ?", auth.ID)
|
||||
|
||||
if in.Id > 0 {
|
||||
query = query.Where("id = ?", in.Id)
|
||||
} else {
|
||||
query = query.Where("identity = ?", in.Identity)
|
||||
}
|
||||
|
||||
if err := query.First(¬e).Error; err != nil {
|
||||
printer.Error("Note not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 增加浏览次数
|
||||
note.Views++
|
||||
|
||||
if err := impl.DBService.Save(¬e).Error; err != nil {
|
||||
printer.Error("Increment views error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
62
module/base/cloud/internal/logic/note/insert_attachment.go
Normal file
62
module/base/cloud/internal/logic/note/insert_attachment.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package note
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cloud/internal/impl"
|
||||
"bsm/full/module/base/cloud/internal/models"
|
||||
pb "bsm/full/module/base/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 上传附件
|
||||
func InsertAttachment(ctx context.Context, in *pb.NoteAttachmentItem) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request
|
||||
if in.NoteId == 0 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
if strings.TrimSpace(in.FileName) == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
if strings.TrimSpace(in.FilePath) == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
// 验证笔记是否存在且属于当前用户
|
||||
var note models.CloudNote
|
||||
if err := impl.DBService.Where("id = ? AND passport_id = ?", in.NoteId, auth.ID).First(¬e).Error; err != nil {
|
||||
printer.Error("Note not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 创建附件记录
|
||||
attachment := models.NoteAttachment{
|
||||
NoteID: uint(in.NoteId),
|
||||
FileName: in.FileName,
|
||||
FilePath: in.FilePath,
|
||||
FileSize: in.FileSize,
|
||||
MimeType: in.MimeType,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := impl.DBService.Create(&attachment).Error; err != nil {
|
||||
printer.Error("Insert attachment error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: string(rune(attachment.ID)),
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
89
module/base/cloud/internal/logic/note/list_notes.go
Normal file
89
module/base/cloud/internal/logic/note/list_notes.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package note
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cloud/internal/impl"
|
||||
"bsm/full/module/base/cloud/internal/models"
|
||||
pb "bsm/full/module/base/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 获取笔记列表
|
||||
func ListNotes(ctx context.Context, in *pb.FetchRequest) (reply *pb.ListNotesResponse, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request page_no,page_size.
|
||||
if in.GetPageNo() < 1 {
|
||||
in.PageNo = 1
|
||||
}
|
||||
if in.GetPageSize() < 10 {
|
||||
in.PageSize = 50
|
||||
}
|
||||
|
||||
// logic code
|
||||
var notes []models.CloudNote
|
||||
var total int64
|
||||
|
||||
// 获取总数
|
||||
if err := impl.DBService.Model(&models.CloudNote{}).Where("passport_id = ?", auth.ID).Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
offset := (in.PageNo - 1) * in.PageSize
|
||||
if err := impl.DBService.Where("passport_id = ?", auth.ID).
|
||||
Preload("Attachments").
|
||||
Order("is_pinned DESC, created_at DESC").
|
||||
Offset(int(offset)).
|
||||
Limit(int(in.PageSize)).
|
||||
Find(¬es).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 转换数据
|
||||
var noteItems []*pb.CloudNoteItem
|
||||
for _, note := range notes {
|
||||
// 转换附件数据
|
||||
var attachments []*pb.NoteAttachmentItem
|
||||
for _, attachment := range note.Attachments {
|
||||
attachments = append(attachments, &pb.NoteAttachmentItem{
|
||||
Id: uint64(attachment.ID),
|
||||
NoteId: uint64(attachment.NoteID),
|
||||
FileName: attachment.FileName,
|
||||
FilePath: attachment.FilePath,
|
||||
FileSize: attachment.FileSize,
|
||||
MimeType: attachment.MimeType,
|
||||
CreatedAt: attachment.CreatedAt.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
noteItems = append(noteItems, &pb.CloudNoteItem{
|
||||
Id: uint64(note.ID),
|
||||
Identity: note.Identity,
|
||||
Title: note.Title,
|
||||
Content: note.Content,
|
||||
Category: note.Category,
|
||||
Tags: note.Tags,
|
||||
IsMarkdown: note.IsMarkdown,
|
||||
IsPinned: note.IsPinned,
|
||||
IsPrivate: note.IsPrivate,
|
||||
Views: int32(note.Views),
|
||||
CreatedAt: note.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: note.UpdatedAt.Format(time.RFC3339),
|
||||
Attachments: attachments,
|
||||
})
|
||||
}
|
||||
|
||||
reply = &pb.ListNotesResponse{
|
||||
Notes: noteItems,
|
||||
Total: total,
|
||||
}
|
||||
|
||||
return reply, nil
|
||||
}
|
||||
99
module/base/cloud/internal/logic/note/search_notes.go
Normal file
99
module/base/cloud/internal/logic/note/search_notes.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package note
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cloud/internal/impl"
|
||||
"bsm/full/module/base/cloud/internal/models"
|
||||
pb "bsm/full/module/base/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 搜索笔记
|
||||
func SearchNotes(ctx context.Context, in *pb.FetchRequest) (reply *pb.ListNotesResponse, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request page_no,page_size.
|
||||
if in.GetPageNo() < 1 {
|
||||
in.PageNo = 1
|
||||
}
|
||||
if in.GetPageSize() < 10 {
|
||||
in.PageSize = 50
|
||||
}
|
||||
|
||||
// logic code
|
||||
var notes []models.CloudNote
|
||||
var total int64
|
||||
|
||||
// 构建搜索查询
|
||||
query := impl.DBService.Model(&models.CloudNote{}).Where("passport_id = ?", auth.ID)
|
||||
|
||||
// 添加搜索条件
|
||||
if keyword, exists := in.Params["keyword"]; exists && keyword != "" {
|
||||
searchKeyword := "%" + strings.ToLower(keyword) + "%"
|
||||
query = query.Where("LOWER(title) LIKE ? OR LOWER(content) LIKE ? OR LOWER(tags) LIKE ?", searchKeyword, searchKeyword, searchKeyword)
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
offset := (in.PageNo - 1) * in.PageSize
|
||||
if err := query.
|
||||
Preload("Attachments").
|
||||
Order("is_pinned DESC, created_at DESC").
|
||||
Offset(int(offset)).
|
||||
Limit(int(in.PageSize)).
|
||||
Find(¬es).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 转换数据
|
||||
var noteItems []*pb.CloudNoteItem
|
||||
for _, note := range notes {
|
||||
// 转换附件数据
|
||||
var attachments []*pb.NoteAttachmentItem
|
||||
for _, attachment := range note.Attachments {
|
||||
attachments = append(attachments, &pb.NoteAttachmentItem{
|
||||
Id: uint64(attachment.ID),
|
||||
NoteId: uint64(attachment.NoteID),
|
||||
FileName: attachment.FileName,
|
||||
FilePath: attachment.FilePath,
|
||||
FileSize: attachment.FileSize,
|
||||
MimeType: attachment.MimeType,
|
||||
CreatedAt: attachment.CreatedAt.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
noteItems = append(noteItems, &pb.CloudNoteItem{
|
||||
Id: uint64(note.ID),
|
||||
Identity: note.Identity,
|
||||
Title: note.Title,
|
||||
Content: note.Content,
|
||||
Category: note.Category,
|
||||
Tags: note.Tags,
|
||||
IsMarkdown: note.IsMarkdown,
|
||||
IsPinned: note.IsPinned,
|
||||
IsPrivate: note.IsPrivate,
|
||||
Views: int32(note.Views),
|
||||
CreatedAt: note.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: note.UpdatedAt.Format(time.RFC3339),
|
||||
Attachments: attachments,
|
||||
})
|
||||
}
|
||||
|
||||
reply = &pb.ListNotesResponse{
|
||||
Notes: noteItems,
|
||||
Total: total,
|
||||
}
|
||||
|
||||
return reply, nil
|
||||
}
|
||||
48
module/base/cloud/internal/logic/note/toggle_pin.go
Normal file
48
module/base/cloud/internal/logic/note/toggle_pin.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package note
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cloud/internal/impl"
|
||||
"bsm/full/module/base/cloud/internal/models"
|
||||
pb "bsm/full/module/base/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 置顶/取消置顶笔记
|
||||
func TogglePin(ctx context.Context, in *pb.TogglePinRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request
|
||||
if in.Id == 0 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
var note models.CloudNote
|
||||
if err := impl.DBService.Where("id = ? AND passport_id = ?", in.Id, auth.ID).First(¬e).Error; err != nil {
|
||||
printer.Error("Note not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 更新置顶状态
|
||||
note.IsPinned = in.IsPinned
|
||||
|
||||
if err := impl.DBService.Save(¬e).Error; err != nil {
|
||||
printer.Error("Toggle pin error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
66
module/base/cloud/internal/logic/note/update_note.go
Normal file
66
module/base/cloud/internal/logic/note/update_note.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package note
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cloud/internal/impl"
|
||||
"bsm/full/module/base/cloud/internal/models"
|
||||
pb "bsm/full/module/base/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 更新笔记
|
||||
func UpdateNote(ctx context.Context, in *pb.CloudNoteItem) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
if strings.TrimSpace(in.Title) == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
var note models.CloudNote
|
||||
query := impl.DBService.Where("passport_id = ?", auth.ID)
|
||||
|
||||
if in.Id > 0 {
|
||||
query = query.Where("id = ?", in.Id)
|
||||
} else {
|
||||
query = query.Where("identity = ?", in.Identity)
|
||||
}
|
||||
|
||||
if err := query.First(¬e).Error; err != nil {
|
||||
printer.Error("Note not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 更新字段
|
||||
note.Title = in.Title
|
||||
note.Content = in.Content
|
||||
note.Category = in.Category
|
||||
note.Tags = in.Tags
|
||||
note.IsMarkdown = in.IsMarkdown
|
||||
note.IsPinned = in.IsPinned
|
||||
note.IsPrivate = in.IsPrivate
|
||||
|
||||
if err := impl.DBService.Save(¬e).Error; err != nil {
|
||||
printer.Error("Update note error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user