fix
This commit is contained in:
@@ -3,7 +3,6 @@ package biz
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -43,51 +42,23 @@ func NewAnnouncementUsecase(repo AnnouncementRepo) *AnnouncementUsecase {
|
||||
return &AnnouncementUsecase{repo: repo}
|
||||
}
|
||||
|
||||
func validateAnnouncement(item *Announcement) error {
|
||||
if item == nil || item.Title == "" {
|
||||
return errors.New("公告标题不能为空")
|
||||
}
|
||||
if len(item.Attachments) == 0 || !json.Valid(item.Attachments) {
|
||||
item.Attachments = json.RawMessage("[]")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (uc *AnnouncementUsecase) Create(ctx context.Context, item *Announcement) error {
|
||||
if err := validateAnnouncement(item); err != nil {
|
||||
return err
|
||||
}
|
||||
return uc.repo.Create(ctx, item)
|
||||
}
|
||||
|
||||
func (uc *AnnouncementUsecase) Update(ctx context.Context, item *Announcement) error {
|
||||
if item == nil || item.ID == 0 {
|
||||
return errors.New("公告ID不能为空")
|
||||
}
|
||||
if err := validateAnnouncement(item); err != nil {
|
||||
return err
|
||||
}
|
||||
return uc.repo.Update(ctx, item)
|
||||
}
|
||||
|
||||
func (uc *AnnouncementUsecase) Delete(ctx context.Context, id uint) error {
|
||||
if id == 0 {
|
||||
return errors.New("公告ID不能为空")
|
||||
}
|
||||
return uc.repo.Delete(ctx, id)
|
||||
}
|
||||
|
||||
func (uc *AnnouncementUsecase) DeleteByIDs(ctx context.Context, ids []uint) error {
|
||||
if len(ids) == 0 {
|
||||
return errors.New("公告ID不能为空")
|
||||
}
|
||||
return uc.repo.DeleteByIDs(ctx, ids)
|
||||
}
|
||||
|
||||
func (uc *AnnouncementUsecase) Find(ctx context.Context, id uint) (*Announcement, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("公告ID不能为空")
|
||||
}
|
||||
return uc.repo.Find(ctx, id)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user