Exemple #1
0
func (m model) GetPostById(id uint64) *Post {
	post := Post{}
	if err := m.db.Where("id = ?", id).First(&post).Error; err != nil {
		log.DebugPrint("Get post error: %v", err)
		return nil
	}

	if err := m.db.Model(&post).Related(&post.User).Error; err != nil {
		log.DebugPrint("Post user related error: %v", err)
		return &post
	}

	return &post
}
Exemple #2
0
func (m model) GetUserPostsByUserId(userId uint64, page int, size int) *[]Post {
	posts := []Post{}
	if err := m.db.Where("user_id = ?", userId).Offset((page - 1) * size).Limit(size).Find(&posts).Error; err != nil {
		log.DebugPrint("Get user posts error: %v", err)
		return nil
	}

	for key, post := range posts {
		if err := m.db.Model(&post).Related(&post.User).Error; err != nil {
			log.DebugPrint("Post user related error: %v", err)
		}
		posts[key] = post
	}

	return &posts
}
Exemple #3
0
func loadTemplatesDefault(templatesDir string) multitemplate.Render {
	r := multitemplate.New()

	layoutDir := templatesDir + "/layouts/"
	layouts, err := filepath.Glob(layoutDir + "*/*" + conf.TMPL_SUFFIX)
	if err != nil {
		panic(err.Error())
	}

	includeDir := templatesDir + "/includes/"
	includes, err := filepath.Glob(includeDir + "*" + conf.TMPL_SUFFIX)
	if err != nil {
		panic(err.Error())
	}

	// Generate our templates map from our layouts/ and includes/ directories
	for _, layout := range layouts {
		files := append(includes, layout)
		tmpl := template.Must(template.ParseFiles(files...))
		tmplName := strings.TrimPrefix(layout, layoutDir)
		tmplName = strings.TrimSuffix(tmplName, conf.TMPL_SUFFIX)
		log.DebugPrint("Tmpl add " + tmplName)
		r.Add(tmplName, tmpl)
	}
	return r
}
Exemple #4
0
func (m model) GetUserByNicknamePwd(nickname string, pwd string) *User {
	user := User{}
	if err := m.db.Where("nickname = ? AND password = ?", nickname, pwd).First(&user).Error; err != nil {
		log.DebugPrint("GetUserByNicknamePwd error: %v", err)
		return nil
	}
	return &user
}
Exemple #5
0
func (m model) GetUserById(id uint64) *User {
	user := User{}
	if err := m.db.Where("id = ?", id).First(&user).Error; err != nil {
		log.DebugPrint("GetUserById error: %v", err)
		return nil
	}

	return &user
}
Exemple #6
0
func render() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Next()

		tmpl, context, err := getContext(c)
		if err == nil {
			c.HTML(http.StatusOK, tmpl, gin.H(context))
		} else {
			log.DebugPrint("Render Error: %v", err)
		}
	}
}
Exemple #7
0
func pongo2() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Next()

		tmpl, context, err := getContext(c)
		if err == nil {
			c.HTML(http.StatusOK, tmpl+conf.TMPL_SUFFIX, p.Context(context))
		} else {
			log.DebugPrint("Render Error: %v", err)
		}
	}
}
Exemple #8
0
func loadTemplatesBindata(templatesDir string) multitemplate.Render {
	r := multitemplate.New()

	layoutDir := templatesDir + "/layouts"
	layoutDirs, err := templates.AssetDir(layoutDir)
	if err != nil {
		panic(err.Error())
	}

	var layouts []string
	for _, dir := range layoutDirs {
		files, err := templates.AssetDir(layoutDir + "/" + dir)
		if err != nil {
			panic(err.Error())
		}

		// 过滤非.tmpl后缀模板
		layoutFiels, err := tmplsFilter(files, layoutDir+"/"+dir)
		if err != nil {
			panic(err.Error())
		}

		layouts = append(layouts, layoutFiels...)
	}

	includeDir := templatesDir + "/includes"
	includeFiels, err := templates.AssetDir(includeDir)
	if err != nil {
		panic(err.Error())
	}
	// 过滤非.tmpl后缀模板
	includes, err := tmplsFilter(includeFiels, includeDir)
	if err != nil {
		panic(err.Error())
	}

	// Generate our templates map from our layouts/ and includes/ directories
	for _, layout := range layouts {
		files := append(includes, layout)
		tmpl := template.Must(parseBindataFiles(files...))
		tmplName := strings.TrimPrefix(layout, layoutDir+"/")
		tmplName = strings.TrimSuffix(tmplName, conf.TMPL_SUFFIX)
		log.DebugPrint("Tmpl add " + tmplName)
		r.Add(tmplName, tmpl)
	}
	return r
}