码农笔录博客源码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

106 rindas
2.2 KiB

pirms 6 gadiem
package service
import (
"beeblog/models"
"errors"
pirms 4 gadiem
"github.com/beego/beego/v2/client/orm"
pirms 6 gadiem
)
type NoteService struct {
}
pirms 6 gadiem
func (this *NoteService) EditNote(note *models.Note) error {
o := orm.NewOrm()
id, err := o.Update(note)
if err == nil {
note.Id = id
}
return err
}
pirms 6 gadiem
func (this *NoteService) SaveNote(note *models.Note) error {
pirms 6 gadiem
o := orm.NewOrm()
id, err := o.Insert(note)
if err == nil {
note.Id = id
}
return err
}
pirms 6 gadiem
func (this *NoteService) GetNote(note *models.Note) error {
pirms 6 gadiem
o := orm.NewOrm()
return o.Read(note)
}
pirms 6 gadiem
func (this *NoteService) DelNote(note *models.Note) error {
o := orm.NewOrm()
_, err := o.Delete(note)
return err
}
pirms 6 gadiem
func (this *NoteService) GetNoteByPid(pid int64) ([]*models.Note, error) {
pirms 6 gadiem
var notes []*models.Note
o := orm.NewOrm()
qs := o.QueryTable(models.Note{})
pirms 6 gadiem
_, err := qs.Filter("Pid", pid).All(&notes)
return notes, err
pirms 6 gadiem
}
pirms 6 gadiem
func (this *NoteService) CountNote(pid int64) (int64, error) {
o := orm.NewOrm()
totalCount, err := o.QueryTable(&models.Note{}).Filter("Pid", pid).Count()
if err != nil {
return 0, err
}
return totalCount, nil
}
pirms 6 gadiem
func (this *NoteService) SaveNoteColl(note *models.NoteColl) error {
pirms 6 gadiem
o := orm.NewOrm()
id, err := o.Insert(note)
if err == nil {
note.Id = id
}
return err
}
pirms 6 gadiem
func (this *NoteService) EditNoteColl(title string, id int64, uid int64) error {
o := orm.NewOrm()
noteColl := &models.NoteColl{Id: id}
if err := o.Read(noteColl); err != nil {
return err
}
if noteColl.UserId != uid {
return errors.New("403")
}
noteColl.Title = title
_, err := o.Update(noteColl, "Title")
return err
}
pirms 6 gadiem
func (this *NoteService) GetNoteColl(uid int64) ([]*models.NoteColl, error) {
pirms 6 gadiem
var notes []*models.NoteColl
o := orm.NewOrm()
qs := o.QueryTable(models.NoteColl{})
pirms 6 gadiem
_, err := qs.Filter("UserId", uid).All(&notes)
return notes, err
}
pirms 6 gadiem
func (this *NoteService) DelNoteColl(id int64, uid int64) error {
o := orm.NewOrm()
noteColl := &models.NoteColl{Id: id}
if err := o.Read(noteColl); err != nil {
return err
}
if uid != noteColl.UserId {
return errors.New("403")
}
_, err := o.QueryTable(models.Note{}).Filter("Pid", id).Delete()
if err == nil {
_, err = o.Delete(noteColl)
}
return err
}