mirror of
https://github.com/mindoc-org/mindoc.git
synced 2026-02-27 17:03:57 +08:00
搭建框架
This commit is contained in:
39
models/book.go
Normal file
39
models/book.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
// Book struct .
|
||||
type Book struct {
|
||||
BookId int `orm:"pk;auto;unique;column(book_id)" json:"book_id"`
|
||||
// BookName 项目名称.
|
||||
BookName string `orm:"column(book_name);size(500)" json:"book_name"`
|
||||
// Identify 项目唯一标识.
|
||||
Identify string `orm:"column(identify);size(100);unique" json:"identify"`
|
||||
// Description 项目描述.
|
||||
Description string `orm:"column(description);size(2000)" json:"description"`
|
||||
// PrivatelyOwned 项目私有: 0 公开/ 1 私有
|
||||
PrivatelyOwned int `orm:"column(privately_owned);type(int);default(0)" json:"privately_owned"`
|
||||
// 当项目是私有时的访问Token.
|
||||
PrivateToken string `orm:"column(private_token);size(500)" json:"private_token"`
|
||||
// DocCount 包含文档数量.
|
||||
DocCount int `orm:"column(doc_count);type(int)" json:"doc_count"`
|
||||
// CommentStatus 评论设置的状态:open为允许所有人评论,closed为不允许评论, group_only 仅允许参与者评论 ,registered_only 仅允许注册者评论.
|
||||
CommentStatus string `orm:"column(comment_status);size(20);default(open)" json:"comment_status"`
|
||||
CommentCount int `orm:"column(comment_count);type(int)" json:"comment_count"`
|
||||
|
||||
// CreateTime 创建时间 .
|
||||
CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add"`
|
||||
MemberId int `orm:"column(member_id);size(100)" json:"member_id"`
|
||||
ModifyTime time.Time `orm:"type(datetime);column(modify_time);null;auto_now" json:"modify_time"`
|
||||
Version int64 `orm:"type(bigint);column(version)" json:"version"`
|
||||
}
|
||||
|
||||
// TableName 获取对应数据库表名.
|
||||
func (m *Book) TableName() string {
|
||||
return "books"
|
||||
}
|
||||
// TableEngine 获取数据使用的引擎.
|
||||
func (m *Book) TableEngine() string {
|
||||
return "INNODB"
|
||||
}
|
||||
|
||||
36
models/comment.go
Normal file
36
models/comment.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
//Comment struct
|
||||
type Comment struct {
|
||||
CommentId int `orm:"pk;auto;unique;column(comment_id)" json:"comment_id"`
|
||||
// DocumentId 评论所属的文档.
|
||||
DocumentId int `orm:"column(document_id);type(int)" json:"document_id"`
|
||||
// Author 评论作者.
|
||||
Author string `orm:"column(author);size(100)" json:"author"`
|
||||
//MemberId 评论用户ID.
|
||||
MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
|
||||
// IPAddress 评论者的IP地址
|
||||
IPAddress string `orm:"column(ip_address);size(100)" json:"ip_address"`
|
||||
// 评论日期.
|
||||
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 int `orm:"column(approved);type(int)" json:"approved"`
|
||||
// UserAgent 评论者浏览器内容
|
||||
UserAgent string `orm:"column(user_agent);size(500)" json:"user_agent"`
|
||||
// Parent 评论所属父级
|
||||
ParentId int `orm:"column(parent_id);type(int);default(0)" json:"parent_id"`
|
||||
}
|
||||
|
||||
// TableName 获取对应数据库表名.
|
||||
func (m *Comment) TableName() string {
|
||||
return "comments"
|
||||
}
|
||||
// TableEngine 获取数据使用的引擎.
|
||||
func (m *Comment) TableEngine() string {
|
||||
return "INNODB"
|
||||
}
|
||||
|
||||
34
models/document.go
Normal file
34
models/document.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
// Document struct.
|
||||
type Document struct {
|
||||
DocumentId int `orm:"pk;auto;unique;column(document_id)" json:"document_id"`
|
||||
DocumentName string `orm:"column(document_name);size(500)" json:"document_name"`
|
||||
// Identify 文档唯一标识
|
||||
Identify string `orm:"column(identify);size(100);unique" json:"identify"`
|
||||
BookId int `orm:"column(book_id);type(int);index" json:"book_id"`
|
||||
OrderSort int `orm:"column(order_sort);default(0);type(int)" json:"order_sort"`
|
||||
// Markdown markdown格式文档.
|
||||
Markdown string `orm:"column(markdown);type(longtext)" json:"markdown"`
|
||||
// Release 发布后的Html格式内容.
|
||||
Release string `orm:"column(release);type(longtext)" json:"release"`
|
||||
// Content 未发布的 Html 格式内容.
|
||||
Content string `orm:"column(content);type(longtext)" json:"content"`
|
||||
CreateTime time.Time `orm:"column(create_time);type(datetime)" json:"create_time"`
|
||||
CreateAt int `orm:"column(create_at);type(int)" json:"create_at"`
|
||||
ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now" json:"modify_time"`
|
||||
ModifyAt int `orm:"column(modify_at);type(int)" json:"modify_at"`
|
||||
Version int64 `orm:"type(bigint);column(version)" json:"version"`
|
||||
}
|
||||
|
||||
|
||||
// TableName 获取对应数据库表名.
|
||||
func (m *Document) TableName() string {
|
||||
return "documents"
|
||||
}
|
||||
// TableEngine 获取数据使用的引擎.
|
||||
func (m *Document) TableEngine() string {
|
||||
return "INNODB"
|
||||
}
|
||||
16
models/errors.go
Normal file
16
models/errors.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Package models 为项目所需的模型对象定义.
|
||||
package models
|
||||
|
||||
import "errors"
|
||||
|
||||
var(
|
||||
// ErrMemberNoExist 用户不存在.
|
||||
ErrMemberNoExist = errors.New("用户不存在")
|
||||
// ErrorMemberPasswordError 密码错误.
|
||||
ErrorMemberPasswordError = errors.New("用户密码错误")
|
||||
// ErrServerAlreadyExist 指定的服务已存在.
|
||||
ErrServerAlreadyExist = errors.New("服务已存在")
|
||||
|
||||
// ErrInvalidParameter 参数错误.
|
||||
ErrInvalidParameter = errors.New("Invalid parameter")
|
||||
)
|
||||
@@ -1,6 +1,97 @@
|
||||
// Package models .
|
||||
package models
|
||||
|
||||
type Member struct {
|
||||
import (
|
||||
"time"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/lifei6671/godoc/utils"
|
||||
)
|
||||
|
||||
type Member struct {
|
||||
MemberId int `orm:"pk;auto;unique;column(member_id)"`
|
||||
Account string `orm:"size(100);unique;column(account)"`
|
||||
Password string `orm:"size(1000);column(password)"`
|
||||
Description string `orm:"column(description);size(2000)" json:"description"`
|
||||
Email string `orm:"size(255);column(email);null;default(null)"`
|
||||
Phone string `orm:"size(255);column(phone);null;default(null)"`
|
||||
Avatar string `orm:"size(1000);column(avatar)"`
|
||||
Role int `orm:"column(role);type(int);default(1);index"` //用户角色:0 管理员/ 1 普通用户
|
||||
Status int `orm:"column(status);type(int);default(0)"` //用户状态:0 正常/1 禁用
|
||||
CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add"`
|
||||
CreateAt int `orm:"type(int);column(create_at)"`
|
||||
LastLoginTime time.Time `orm:"type(datetime);column(last_login_time);null"`
|
||||
}
|
||||
|
||||
// TableName 获取对应数据库表名.
|
||||
func (m *Member) TableName() string {
|
||||
return "members"
|
||||
}
|
||||
// TableEngine 获取数据使用的引擎.
|
||||
func (m *Member) TableEngine() string {
|
||||
return "INNODB"
|
||||
}
|
||||
|
||||
func NewMember() *Member {
|
||||
return &Member{}
|
||||
}
|
||||
|
||||
// Login 用户登录.
|
||||
func (m *Member) Login(account string,password string) (*Member,error) {
|
||||
o := orm.NewOrm()
|
||||
|
||||
member := &Member{}
|
||||
|
||||
err := o.QueryTable("md_" + m.TableName()).Filter("account",account).Filter("status",0).One(member);
|
||||
|
||||
if err != nil {
|
||||
return member,ErrMemberNoExist
|
||||
}
|
||||
|
||||
ok,err := utils.PasswordVerify(member.Password,password) ;
|
||||
|
||||
if ok && err == nil {
|
||||
return member,nil
|
||||
}
|
||||
|
||||
return member,ErrorMemberPasswordError
|
||||
}
|
||||
|
||||
// Add 添加一个用户.
|
||||
func (member *Member) Add () (error) {
|
||||
o := orm.NewOrm()
|
||||
|
||||
hash ,err := utils.PasswordHash(member.Password);
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
member.Password = hash
|
||||
|
||||
_,err = o.Insert(member)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update 更新用户信息.
|
||||
func (m *Member) Update(cols... string) (error) {
|
||||
o := orm.NewOrm()
|
||||
|
||||
if _,err := o.Update(m,cols...);err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Member) Find(id int) error{
|
||||
o := orm.NewOrm()
|
||||
|
||||
m.MemberId = id
|
||||
if err := o.Read(m); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
92
models/options.go
Normal file
92
models/options.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/lifei6671/godoc/conf"
|
||||
)
|
||||
|
||||
|
||||
// Option struct .
|
||||
type Option struct {
|
||||
OptionId int `orm:"pk;auto;unique;column(option_id)" json:"option_id"`
|
||||
OptionTitle string `orm:"column(option_title);size(500)" json:"option_title"`
|
||||
OptionName string `orm:"column(option_name);unique;size(80)" json:"option_name"`
|
||||
OptionValue string `orm:"column(option_value);type(text);null" json:"option_value"`
|
||||
Remark string `orm:"column(remark);type(text);null" json:"remark"`
|
||||
}
|
||||
|
||||
// TableName 获取对应数据库表名.
|
||||
func (m *Option) TableName() string {
|
||||
return "options"
|
||||
}
|
||||
// TableEngine 获取数据使用的引擎.
|
||||
func (m *Option) TableEngine() string {
|
||||
return "INNODB"
|
||||
}
|
||||
|
||||
func NewOption() *Option {
|
||||
return &Option{}
|
||||
}
|
||||
|
||||
func (p *Option) Find(id int) error {
|
||||
o := orm.NewOrm()
|
||||
|
||||
p.OptionId = id
|
||||
|
||||
if err := o.Read(p);err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Option) FindByKey(key string) error {
|
||||
o := orm.NewOrm()
|
||||
|
||||
p.OptionName = key
|
||||
if err := o.Read(p);err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetOptionValue(key, def string) string {
|
||||
|
||||
option := NewOption()
|
||||
|
||||
if err := option.FindByKey(key); err == nil {
|
||||
return option.OptionValue
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func (p *Option) InsertOrUpdate() error {
|
||||
|
||||
o := orm.NewOrm()
|
||||
var err error
|
||||
if p.OptionId > 0 {
|
||||
_,err = o.Update(o)
|
||||
}else{
|
||||
_,err = o.Insert(p)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Option) InsertMulti(option... Option ) (error){
|
||||
|
||||
o := orm.NewOrm()
|
||||
|
||||
_,err := o.InsertMulti(len(option),option)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Option) All() ([]Option,error) {
|
||||
o := orm.NewOrm()
|
||||
var options []Option
|
||||
|
||||
_,err := o.QueryTable(conf.GetDatabasePrefix() + p.TableName()).All(&options)
|
||||
|
||||
if err != nil {
|
||||
return options,err
|
||||
}
|
||||
return options,nil
|
||||
}
|
||||
26
models/relationship.go
Normal file
26
models/relationship.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package models
|
||||
|
||||
type Relationship struct {
|
||||
RelationshipId int `orm:"pk;auto;unique;column(relationship_id)" json:"relationship_id"`
|
||||
MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
|
||||
BookId int `orm:"column(book_id);type(int)" json:"book_id"`
|
||||
// RoleId 角色:0 创始人(创始人不能被移除) / 1 管理员/2 编辑者/3 观察者
|
||||
RoleId int `orm:"column(role_id);type(int)" json:"role_id"`
|
||||
}
|
||||
|
||||
|
||||
// TableName 获取对应数据库表名.
|
||||
func (m *Relationship) TableName() string {
|
||||
return "relationship"
|
||||
}
|
||||
// TableEngine 获取数据使用的引擎.
|
||||
func (m *Relationship) TableEngine() string {
|
||||
return "INNODB"
|
||||
}
|
||||
|
||||
// 联合唯一键
|
||||
func (u *Relationship) TableUnique() [][]string {
|
||||
return [][]string{
|
||||
[]string{"MemberId", "BookId"},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user