1、实现删除项目

2、实现生成访问令牌
3、实现添加评论
This commit is contained in:
lifei6671
2017-04-23 20:33:21 +08:00
parent a05a13dd4e
commit d1b2c52124
9 changed files with 399 additions and 8 deletions

View File

@@ -22,6 +22,8 @@ type Book struct {
PrivatelyOwned int `orm:"column(privately_owned);type(int);default(0)" json:"privately_owned"`
// 当项目是私有时的访问Token.
PrivateToken string `orm:"column(private_token);size(500);null" json:"private_token"`
//评论状态0 正常/1 已删除
Status int `orm:"column(status);type(int);default(0)" json:"status"`
// DocCount 包含文档数量.
DocCount int `orm:"column(doc_count);type(int)" json:"doc_count"`
// CommentStatus 评论设置的状态:open 为允许所有人评论closed 为不允许评论, group_only 仅允许参与者评论 ,registered_only 仅允许注册者评论.
@@ -60,6 +62,15 @@ func (m *Book) Insert() error {
return err
}
func (m *Book) Find(id int) error {
if id <= 0 {
return ErrInvalidParameter
}
o := orm.NewOrm()
return o.Read(m)
}
func (m *Book) Update(cols... string) error {
o := orm.NewOrm()
@@ -139,6 +150,83 @@ func (m *Book) FindToPager(pageIndex, pageSize ,memberId int) (books []BookResul
return
}
// 彻底删除项目.
func (m *Book) ThoroughDeleteBook(id int) error {
if id <= 0{
return ErrInvalidParameter
}
o := orm.NewOrm()
m.BookId = id
if err := o.Read(m); err != nil {
return err
}
o.Begin()
sql1 := "DELETE FROM " + NewComment().TableNameWithPrefix() + " WHERE book_id = ?"
_,err := o.Raw(sql1,m.BookId).Exec()
if err != nil {
o.Rollback()
return err
}
sql2 := "DELETE FROM " + NewDocument().TableNameWithPrefix() + " WHERE book_id = ?"
_,err = o.Raw(sql2,m.BookId).Exec()
if err != nil {
o.Rollback()
return err
}
sql3 := "DELETE FROM " + m.TableNameWithPrefix() + " WHERE book_id = ?"
_,err = o.Raw(sql3).Exec()
if err != nil {
o.Rollback()
return err
}
sql4 := "DELETE FROM " + NewRelationship() + " WHERE book_id = ?"
_,err = o.Raw(sql4).Exec()
if err != nil {
o.Rollback()
return err
}
return o.Commit()
}

View File

@@ -25,6 +25,7 @@ type BookResult struct {
MemberId int `json:"member_id"`
RoleId int `json:"role_id"`
RoleName string `json:"role_name"`
Status int
LastModifyText string `json:"last_modify_text"`
}
@@ -81,6 +82,7 @@ func (m *BookResult) FindByIdentify(identify string,member_id int) (*BookResult,
m.ModifyTime = book.ModifyTime
m.Cover = book.Cover
m.Label = book.Label
m.Status = book.Status
m.MemberId = relationship.MemberId
m.RoleId = relationship.RoleId

View File

@@ -1,10 +1,16 @@
package models
import "time"
import (
"time"
"github.com/lifei6671/godoc/conf"
"github.com/astaxie/beego/orm"
"errors"
)
//Comment struct
type Comment struct {
CommentId int `orm:"pk;auto;unique;column(comment_id)" json:"comment_id"`
BookId int `orm:"column(book_id);type(int)" json:"book_id"`
// DocumentId 评论所属的文档.
DocumentId int `orm:"column(document_id);type(int)" json:"document_id"`
// Author 评论作者.
@@ -17,7 +23,7 @@ type Comment struct {
CommentDate time.Time `orm:"type(datetime);column(comment_date);auto_now_add" json:"comment_date"`
//Content 评论内容.
Content string `orm:"column(content);size(2000)" json:"content"`
// Approved 评论状态0 待审核/1 已审核/2 垃圾评论
// Approved 评论状态0 待审核/1 已审核/2 垃圾评论/ 3 已删除
Approved int `orm:"column(approved);type(int)" json:"approved"`
// UserAgent 评论者浏览器内容
UserAgent string `orm:"column(user_agent);size(500)" json:"user_agent"`
@@ -33,4 +39,129 @@ func (m *Comment) TableName() string {
func (m *Comment) TableEngine() string {
return "INNODB"
}
func (m *Comment) TableNameWithPrefix() string {
return conf.GetDatabasePrefix() + m.TableName()
}
func NewComment() *Comment {
return &Comment{}
}
func (m *Comment) Find(id int) error {
if id <= 0 {
return ErrInvalidParameter
}
o := orm.NewOrm()
return o.Read(m)
}
func (m *Comment) Update(cols... string) error {
o := orm.NewOrm()
_,err := o.Update(m,cols)
return err
}
//Insert 添加一条评论.
func (m *Comment) Insert() error {
if m.DocumentId <= 0{
return errors.New("评论文档不存在")
}
if m.Content == "" {
return ErrCommentContentNotEmpty
}
o := orm.NewOrm()
if m.CommentId > 0 {
comment := NewComment()
//如果父评论不存在
if err := o.Read(comment); err != nil {
return err
}
}
document := NewDocument()
//如果评论的文档不存在
if err := document.Find(m.DocumentId); err != nil {
return err
}
book := NewBook()
//如果评论的项目不存在
if err := book.Find(document.BookId); err != nil {
return err
}
//如果已关闭评论
if book.CommentStatus == "closed"{
return ErrCommentClosed
}
if book.CommentStatus == "registered_only" && m.MemberId <= 0{
return ErrPermissionDenied
}
//如果仅参与者评论
if book.CommentStatus == "group_only" {
if m.MemberId <= 0{
return ErrPermissionDenied
}
rel := NewRelationship()
if _,err := rel.FindForRoleId(book.BookId,m.MemberId);err != nil {
return ErrPermissionDenied
}
}
if m.MemberId > 0 {
member := NewMember()
//如果用户不存在
if err := member.Find(m.MemberId) ; err != nil {
return ErrMemberNoExist
}
//如果用户被禁用
if member.Status == 1 {
return ErrMemberDisabled
}
}else if m.Author == "" {
m.Author = "[匿名用户]"
}
m.BookId = book.BookId
_,err := o.Insert(m)
return err
}

View File

@@ -3,6 +3,7 @@ package models
import (
"time"
"github.com/lifei6671/godoc/conf"
"github.com/astaxie/beego/orm"
)
// Document struct.
@@ -44,7 +45,20 @@ func NewDocument() *Document {
return &Document{}
}
func (m *Document) Find(id int) error {
if id <= 0 {
return ErrInvalidParameter
}
o := orm.NewOrm()
err := o.Read(m)
if err == orm.ErrNoRows{
return ErrDataNotExist
}
return nil
}

View File

@@ -6,13 +6,17 @@ import "errors"
var(
// ErrMemberNoExist 用户不存在.
ErrMemberNoExist = errors.New("用户不存在")
ErrMemberDisabled = errors.New("用户被禁用")
// ErrorMemberPasswordError 密码错误.
ErrorMemberPasswordError = errors.New("用户密码错误")
// ErrServerAlreadyExist 指定的服务已存在.
ErrServerAlreadyExist = errors.New("服务已存在")
// ErrDataNotExist 指定的服务已存在.
ErrDataNotExist = errors.New("数据不存在")
// ErrInvalidParameter 参数错误.
ErrInvalidParameter = errors.New("Invalid parameter")
ErrPermissionDenied = errors.New("Permission denied")
ErrCommentClosed = errors.New("评论已关闭")
ErrCommentContentNotEmpty = errors.New("评论内容不能为空")
)