Documentation ¶
Index ¶
- Constants
- Variables
- func ChooseCol(collection string) (col *mongo.Collection, mErr mo2errors.Mo2Errors)
- func CreateAccountIndex() (err error)
- func CreateActiveAccounts(ctx context.Context, accs []model.Account, pass string) error
- func CreateAnonymousAccount() (a model.Account)
- func DeleteAccount(id primitive.ObjectID) (a model.Account, e mo2errors.Mo2Errors)
- func DeleteAccountByEmail(email string) (a model.Account, e mo2errors.Mo2Errors)
- func DeleteBlogs(isDraft bool, blogIDs ...primitive.ObjectID) (mErr mo2errors.Mo2Errors)
- func DeleteByRecycleItemID(ID primitive.ObjectID) (mErr mo2errors.Mo2Errors)
- func DeleteByRecycleItemInfo(ID primitive.ObjectID, handler string) (mErr mo2errors.Mo2Errors)
- func DeleteCategory(ids ...primitive.ObjectID) (mErr mo2errors.Mo2Errors)
- func DeleteCategoryCompletely(ids ...primitive.ObjectID) (mErr mo2errors.Mo2Errors)
- func DeleteExpireItems() (mErr mo2errors.Mo2Errors)
- func DeleteGroupByID(id primitive.ObjectID) mo2errors.Mo2Errors
- func DeleteGroupByOwnerID(id primitive.ObjectID) mo2errors.Mo2Errors
- func DeleteProject(ctx context.Context, id primitive.ObjectID) (*mongo.DeleteResult, error)
- func DeleteRecycleItems(ids ...primitive.ObjectID) (mErr mo2errors.Mo2Errors)
- func EnsureEmailUnique(email string) (unique bool, e mo2errors.Mo2Errors)
- func FindAccount(id primitive.ObjectID) (a model.Account, exist bool)
- func FindAccountByEmail(email string) (account model.Account, e mo2errors.Mo2Errors)
- func FindAccountByName(name string) (a model.Account, exist bool)
- func FindAccountInfo(id primitive.ObjectID) (u dto.UserInfo, exist bool)
- func FindAllAccounts() (as []model.Account)
- func FindAllAccountsInfo() (us []dto.UserInfo)
- func FindAllCategories() (cs []model.Directory)
- func FindBlogById(id primitive.ObjectID, isDraft bool, opts ...*options.FindOneOptions) (b model.Blog)
- func FindBlogs(filter model.Filter) (b []model.Blog)
- func FindBlogsByCategoryId(catID primitive.ObjectID, filter model.Filter) (bs []model.Blog, mErr mo2errors.Mo2Errors)
- func FindBlogsByUser(u dto.LoginUserInfo, filter model.Filter) (b []model.Blog)
- func FindBlogsByValue(field string, val interface{}, filter model.Filter) (b []model.Blog)
- func FindCategories(ids []primitive.ObjectID) (cs []model.Directory, mErr mo2errors.Mo2Errors)
- func FindCategoriesByUserId(userId ...primitive.ObjectID) (cs []model.Directory, mErr mo2errors.Mo2Errors)
- func FindCategoryById(id primitive.ObjectID) (c model.Directory)
- func FindDirectoryInfo(collection string, ids ...primitive.ObjectID) (info []model.Directory, mErr mo2errors.Mo2Errors)
- func FindGroup(id primitive.ObjectID) (group model.Group, mErr mo2errors.Mo2Errors)
- func FindOrCreateRoot4User(userID primitive.ObjectID) (cat model.Directory, mErr mo2errors.Mo2Errors)
- func FindSubCategories(ID primitive.ObjectID) (cs []model.Directory, mErr mo2errors.Mo2Errors)
- func GenerateEmailToken(addAccount model.AddAccount) (account *model.Account, err error)
- func GetClient() *mongo.Client
- func GetCollection(colName string) *mongo.Collection
- func GetCommentNum(articleID primitive.ObjectID) int64
- func GetComments(articleID primitive.ObjectID, page int64, pagesize int64) (cs []model.Comment)
- func GetProject(ctx context.Context, query interface{}) (p *model.Project, err error)
- func If(condition bool, trueVal interface{}, falseVal interface{}) interface{}
- func InitAccount(newAccount model.AddAccount, token string) (account model.Account, err error)
- func ListAccountsBrief(ids []primitive.ObjectID) (bs []dto.UserInfoBrief)
- func ListAllAccountsBrief() (bs []dto.UserInfoBrief)
- func ListProject(ctx context.Context, page, pageSize int64, query interface{}) ([]*model.Project, error)
- func ProcessBlog(isDraft bool, b *model.Blog, operation string) (mErr mo2errors.Mo2Errors)
- func RelateCategories2Blog(s2e dto.RelateEntitySet2Entity)
- func RelateCategories2Blogs(s2s dto.RelateEntitySet2EntitySet) (result []model.Blog)
- func RelateCategories2User(catIDs []primitive.ObjectID, userIds ...primitive.ObjectID) (mErr mo2errors.Mo2Errors)
- func RelateCategory2Blog(e2e dto.RelateEntity2Entity)
- func RelateCategory2User(catID primitive.ObjectID, userIds ...primitive.ObjectID) (mErr mo2errors.Mo2Errors)
- func RelateMainCategory2User(e2e dto.RelateEntity2Entity) (mErr mo2errors.Mo2Errors)
- func RelateSubCategories2Category(s2e dto.RelateEntitySet2Entity)
- func RelateSubCategory2Category(e2e dto.RelateEntity2Entity)
- func RemoveCategoriesInAllBlogs(catIDs ...primitive.ObjectID) (mErr mo2errors.Mo2Errors)
- func RightFilter(userID primitive.ObjectID, requestIDs ...primitive.ObjectID) (allowIDs []primitive.ObjectID, mErr mo2errors.Mo2Errors)
- func ScoreBlog(ctx context.Context, b *model.Blog, prev float64, now float64) (sum float64, num int)
- func SortCategories(c model.Directory, m map[string][]model.Directory)
- func UpdateSubCategories(catID primitive.ObjectID, parentID primitive.ObjectID) (mErr mo2errors.Mo2Errors)
- func UpsertAccount(a *model.Account) (merr mo2errors.Mo2Errors)
- func UpsertAccountWithF(a *model.Account, filter interface{}) (merr mo2errors.Mo2Errors)
- func UpsertBlog(b *model.Blog, isDraft bool) (mErr mo2errors.Mo2Errors)
- func UpsertCategory(c *model.Directory) (mErr mo2errors.Mo2Errors)
- func UpsertComment(c *model.Comment)
- func UpsertDirectoryByUser(c *model.Directory, userID primitive.ObjectID) (mErr mo2errors.Mo2Errors)
- func UpsertGroup(group model.Group) mo2errors.Mo2Errors
- func UpsertProject(ctx context.Context, p *model.Project, update bson.M) (*mongo.UpdateResult, error)
- func UpsertRecycleItem(item model.RecycleItem) (mErr mo2errors.Mo2Errors)
- func UpsertSubComment(id primitive.ObjectID, c *model.Subcomment)
- func VerifyAccount(info model.LoginAccount) (account model.Account, err error)
- func VerifyEmail(info model.VerifyEmail) (account model.Account, err error)
Examples ¶
Constants ¶
const ( OperationKey = "operation" OperationRecycle = "recycle" OperationRestore = "restore" )
const (
CategoryCol = "category"
)
const (
DurationBeforeBlogDelete = time.Hour * 24 * 30
)
Variables ¶
var AccCol = GetCollection("accounts")
AccCol account collection
var BlogCol *mongo.Collection = GetCollection("blog")
BlogCol blog col
var DraftCol *mongo.Collection = GetCollection("draft")
DraftCol draft col
var GroupCol = GetCollection(groupStr)
var ProjCol = GetCollection(projStr)
Functions ¶
func ChooseCol ¶
func ChooseCol(collection string) (col *mongo.Collection, mErr mo2errors.Mo2Errors)
ChooseCol 选择数据集合 根据collection名称选取相应名称的集合
func CreateAccountIndex ¶
func CreateAccountIndex() (err error)
func CreateActiveAccounts ¶
func CreateAnonymousAccount ¶
create an anonymous account
func DeleteAccount ¶
func DeleteAccountByEmail ¶
func DeleteBlogs ¶
DeleteBlogs 彻底删除
func DeleteByRecycleItemID ¶
DeleteByRecycleItemID 根据itemID删除Item
func DeleteByRecycleItemInfo ¶
DeleteByRecycleItemInfo 根据itemInfo id&handler 删除Item
func DeleteCategory ¶
DeleteCategory
func DeleteCategoryCompletely ¶
DeleteCategoryCompletely 删除category,并删除blog中的冗余数据
Example ¶
testNum := 100 catNum := 10 isDraft := true catIDs := InsertCategories4Test(catNum) ids := InsertBlogs4Test(isDraft, testNum) defer func() { if mErr := DeleteBlogs(isDraft, ids...); mErr.IsError() { log.Println(mErr) } DeleteCategory(catIDs...) }() RelateCategories2Blogs(dto.RelateEntitySet2EntitySet{ RelatedIDs: catIDs, RelateToIDs: ids, }) if bs, mErr := FindBlogsByCategoryId(catIDs[0], model.Filter{IsDeleted: false, IsDraft: isDraft}); mErr.IsError() { log.Println(mErr) } else { fmt.Println("before delete: ", len(bs)) } t1 := time.Now() if mErr := DeleteCategoryCompletely(catIDs...); mErr.IsError() { fmt.Println(mErr) } log.Println(time.Since(t1)) if bs, mErr := FindBlogsByCategoryId(catIDs[0], model.Filter{IsDeleted: false, IsDraft: isDraft}); mErr.IsError() { log.Println(mErr) } else { fmt.Println("after delete: ", len(bs)) } fmt.Println(FindCategoryById(catIDs[0]))
Output: before delete: 100 after delete: 0 {ObjectID("000000000000000000000000") ObjectID("000000000000000000000000") { } []}
func DeleteExpireItems ¶
DeleteExpireItems 删除已到规定删除时间的Items
func DeleteGroupByID ¶
DeleteGroupByID delete group by id
func DeleteGroupByOwnerID ¶
DeleteGroupByOwnerID delete groups by owner id
func DeleteProject ¶
func DeleteRecycleItems ¶
DeleteRecycleItems 根据id删除Items
func EnsureEmailUnique ¶
EnsureEmailUnique return true if unique, else return false
if not unique, check whether the user exists if active, already exists, return false if not active, delete document, return ture if unique, return true
func FindAccount ¶
FindAccount find
func FindAccountByEmail ¶
FindAccountByEmail get account by email
func FindAccountByName ¶
FindAccountByName as name
func FindAccountInfo ¶
FindAccountInfo find
func FindAllCategories ¶
FindAllCategories find all categories
func FindBlogById ¶
func FindBlogById(id primitive.ObjectID, isDraft bool, opts ...*options.FindOneOptions) (b model.Blog)
find blog by id
func FindBlogsByCategoryId ¶
func FindBlogsByCategoryId(catID primitive.ObjectID, filter model.Filter) (bs []model.Blog, mErr mo2errors.Mo2Errors)
FindBlogsByCategoryId 寻找包括categoryId的所有blogs的信息
func FindBlogsByUser ¶
find blog by user
func FindBlogsByValue ¶
find blog by value
func FindCategories ¶
FindCategories 寻找位于ids列表中所有categories的详细信息
func FindCategoriesByUserId ¶
func FindCategoriesByUserId(userId ...primitive.ObjectID) (cs []model.Directory, mErr mo2errors.Mo2Errors)
FindCategoriesByUserId find categories by user id
func FindCategoryById ¶
FindCategoryById find category by id
func FindDirectoryInfo ¶
func FindDirectoryInfo(collection string, ids ...primitive.ObjectID) (info []model.Directory, mErr mo2errors.Mo2Errors)
FindDirectoryInfo 根据collection选取集合,并在其中寻找在id在ids列表中的所有directory类型的信息 现在返回的数据有id,info与name
func FindOrCreateRoot4User ¶
func FindOrCreateRoot4User(userID primitive.ObjectID) (cat model.Directory, mErr mo2errors.Mo2Errors)
FindOrCreateRoot4User 寻找用户的根目录,若不存在,新建并返回
func FindSubCategories ¶
FindSubCategories 寻找一个category id的所有子category的详细信息
func GenerateEmailToken ¶
func GenerateEmailToken(addAccount model.AddAccount) (account *model.Account, err error)
GenerateEmailToken generate token for email of an account
func GetCollection ¶
func GetCollection(colName string) *mongo.Collection
func GetCommentNum ¶
Ge3tCommentNum 获取该文章下的一级评论数量
func GetComments ¶
GetComments get comments
func GetProject ¶
func InitAccount ¶
already check the validation in controller if add a newAccount success, return account info
func ListAccountsBrief ¶
func ListAccountsBrief(ids []primitive.ObjectID) (bs []dto.UserInfoBrief)
ListAccountsBrief find from a list of id
func ListAllAccountsBrief ¶
func ListAllAccountsBrief() (bs []dto.UserInfoBrief)
ListAllAccountsBrief find from a list of id
func ListProject ¶
func ProcessBlog ¶
ProcessBlog process blog or draft 根据operation对blog/draft的信息进行更新 recycle:新增关于本blog/draft的recycleBin信息,且isDeleted字段置为true状态 restore:将recycleBin中关于本blog/draft的信息进行删除,且isDeleted字段恢复为false状态
func RelateCategories2Blog ¶
func RelateCategories2Blog(s2e dto.RelateEntitySet2Entity)
RelateCategory2Blog 在blog及draft的categories中添加categories的id
func RelateCategories2Blogs ¶
func RelateCategories2Blogs(s2s dto.RelateEntitySet2EntitySet) (result []model.Blog)
RelateCategories2Blogs 在blogs及drafts的categories中添加categories的id Todo find nice way to make of use
func RelateCategories2User ¶
func RelateCategories2User(catIDs []primitive.ObjectID, userIds ...primitive.ObjectID) (mErr mo2errors.Mo2Errors)
RelateCategories2User 在列表category的ownerIds中添加userIDs
Example ¶
ownCatNum := 100 ownCatIDs := InsertCategories4Test(ownCatNum) userID := primitive.NewObjectID() defer func() { DeleteCategoryCompletely(ownCatIDs...) }() RelateCategories2User(ownCatIDs, userID) cs, _ := FindCategoriesByUserId(userID) if len(cs) == ownCatNum { fmt.Println("success") }
Output: success
func RelateCategory2Blog ¶
func RelateCategory2Blog(e2e dto.RelateEntity2Entity)
RelateCategory2Blog 在blog及draft的categories中添加category的id
func RelateCategory2User ¶
func RelateCategory2User(catID primitive.ObjectID, userIds ...primitive.ObjectID) (mErr mo2errors.Mo2Errors)
RelateCategory2User 在category的ownerIds中添加userIDs
func RelateMainCategory2User ¶
func RelateMainCategory2User(e2e dto.RelateEntity2Entity) (mErr mo2errors.Mo2Errors)
RelateMainCategory2User 为用户创建主归档目录
func RelateSubCategories2Category ¶
func RelateSubCategories2Category(s2e dto.RelateEntitySet2Entity)
RelateSubCategories2Category 将子归档subCategories的parent_id设为category
Example ¶
num := 10 ids := InsertCategories4Test(num) parentID := InsertCategories4Test(1)[0] defer func() { DeleteCategory(ids...) DeleteCategory(parentID) }() RelateSubCategories2Category(dto.RelateEntitySet2Entity{RelatedIDs: ids, RelateToID: parentID}) if res, mErr := FindSubCategories(parentID); mErr.IsError() { panic(mErr) } else { fmt.Println("after relate: ", len(res)) }
Output: after relate: 10
func RelateSubCategory2Category ¶
func RelateSubCategory2Category(e2e dto.RelateEntity2Entity)
RelateSubCategory2Category 将子归档subCategory的parent_id设为category
func RemoveCategoriesInAllBlogs ¶
RemoveCategoriesInAllBlogs 删除所有blog中的category存在id
func RightFilter ¶
func RightFilter(userID primitive.ObjectID, requestIDs ...primitive.ObjectID) (allowIDs []primitive.ObjectID, mErr mo2errors.Mo2Errors)
RightFilter 根据用户id,返回请求的ids中有权限进行某种操作的过滤id列表
Example ¶
ownCatNum := 100 notOwnCatNum := 200 ownCatIDs := InsertCategories4Test(ownCatNum) notOwnCatIDs := InsertCategories4Test(notOwnCatNum) userID := primitive.NewObjectID() defer func() { DeleteCategoryCompletely(ownCatIDs...) DeleteCategoryCompletely(notOwnCatIDs...) }() RelateCategories2User(ownCatIDs, userID) allowIDs, _ := RightFilter(userID, append(ownCatIDs, notOwnCatIDs...)...) if len(allowIDs) == ownCatNum { fmt.Println("success") } else { fmt.Printf("allow: %v; own: %v", len(allowIDs), ownCatNum) }
Output: success
func SortCategories ¶
SortCategories 递归建立categories的树形结构
func UpdateSubCategories ¶
func UpdateSubCategories(catID primitive.ObjectID, parentID primitive.ObjectID) (mErr mo2errors.Mo2Errors)
Example ¶
num := 10 ids := InsertCategories4Test(num) parentID := InsertCategories4Test(1)[0] newParentID := primitive.NewObjectID() defer func() { DeleteCategory(ids...) DeleteCategory(parentID) }() RelateSubCategories2Category(dto.RelateEntitySet2Entity{RelatedIDs: ids, RelateToID: parentID}) res, _ := FindSubCategories(parentID) fmt.Println("relate to old: ", len(res)) res, _ = FindSubCategories(newParentID) fmt.Println("relate to new: ", len(res)) mErr := UpdateSubCategories(parentID, newParentID) fmt.Println(mErr) res, _ = FindSubCategories(parentID) fmt.Println("relate to old: ", len(res)) res, _ = FindSubCategories(newParentID) fmt.Println("relate to new: ", len(res))
Output: relate to old: 10 relate to new: 0 200: update 10 subCategories relate to old: 0 relate to new: 10
func UpsertAccountWithF ¶
func UpsertBlog ¶
UpsertBlog upsert blog or draft
func UpsertCategory ¶
UpsertCategory 更新、插入category
func UpsertDirectoryByUser ¶
func UpsertDirectoryByUser(c *model.Directory, userID primitive.ObjectID) (mErr mo2errors.Mo2Errors)
UpsertDirectoryByUser 用户id为userID的用户更新、插入category 若插入的ownerIDs不存在,则将请求方加入
若存在,则检查请求方id是否存在访问权限,若无则直接返回
若插入的parentID不存在,则检查请求用户是否有root
若有root,则使用root的id做为parentID插入 若无root,则创建并使用新建的root的id做为parentID插入
func UpsertProject ¶
func UpsertProject(ctx context.Context, p *model.Project, update bson.M) (*mongo.UpdateResult, error)
UpsertProject 插入或更新project - 如果插入,ManagerIDs和MemberIDs会被设置为空数组
func UpsertRecycleItem ¶
func UpsertRecycleItem(item model.RecycleItem) (mErr mo2errors.Mo2Errors)
UpsertRecycleItem 新增或更新recycleBin中的item
func UpsertSubComment ¶
func UpsertSubComment(id primitive.ObjectID, c *model.Subcomment)
UpsertSubComment as name
func VerifyAccount ¶
func VerifyAccount(info model.LoginAccount) (account model.Account, err error)
verify an account
func VerifyEmail ¶
func VerifyEmail(info model.VerifyEmail) (account model.Account, err error)
verify email of an account
Types ¶
This section is empty.