示例#1
0
文件: file.go 项目: VincentYu/server
func (f *File) Find(c *gin.Context) {
	if !isGranted(c, "file.list") {
		c.JSON(403, utils.NewNoAccessPermissionError(""))
		return
	}

	page := queryInt(c, "page", 1)
	pagesize := queryInt(c, "pagesize", 30)
	userid := queryInt64(c, "user_id", 0)
	ext := c.Query("ext")
	name := c.Query("name")

	engine := models.Engine()
	session := func() *xorm.Session {
		where := engine.Where("")
		if userid > 0 {
			where = where.Where("user_id = ?", userid)
		}

		if ext != "" {
			exts := strings.Split(ext, ",")
			where = where.In("ext", exts)
		}

		if name != "" {
			where = where.Where("name like ?", "%"+name+"%")
		}

		return where
	}

	total, _ := session().Count(&models.File{})
	files := make([]models.File, 0)

	if err := session().Limit(pagesize, pagesize*(page-1)).Desc("created_at").Find(&files); err != nil {
		c.JSON(400, utils.NewError("find files failed - %s", err.Error()))
		return
	}

	result := models.NewQueryResult(page, pagesize, total, files)

	c.JSON(200, result)

}
示例#2
0
文件: user.go 项目: VincentYu/server
// liist all user
// is_active 1/0
func (u *User) List(c *gin.Context) {
	if !isGranted(c, "user.list") {
		c.JSON(403, utils.NewNoAccessPermissionError(""))
		return
	}

	page := queryInt(c, "page", 1)
	pagesize := queryInt(c, "pagesize", 30)
	isActive := strings.ToLower(c.Query("is_active"))

	engine := models.Engine()
	session := func() *xorm.Session {
		where := engine.Where("nickname like ? and username like ? and email like ? ",
			"%"+c.Query("nickname")+"%", "%"+c.Query("username")+"%", "%"+c.Query("email")+"%")

		if isActive != "" {
			if isActive == "1" || isActive == "true" {
				where = where.Where("is_active = ?", true)
			} else if isActive == "0" || isActive == "false" {
				where = where.Where("is_active = ?", false)
			}
		}

		return where
	}

	total, _ := session().Count(&models.User{})
	users := make([]models.User, 0)

	if err := session().Limit(pagesize, pagesize*(page-1)).Asc("created_at").Find(&users); err != nil {
		c.JSON(400, utils.NewError("find users failed - %s", err.Error()))
		return
	}

	result := models.NewQueryResult(page, pagesize, total, users)

	c.JSON(200, result)

}