Browse Source

User object instead user id for pages and blog

Vova Tkach 6 years ago
parent
commit
66f466a12b

+ 49 - 15
engine/fetdata/blog.go

@@ -32,7 +32,7 @@ type Blog struct {
 	bufferCats map[string][]*BlogCategory
 }
 
-func (this *Blog) init() {
+func (this *Blog) load() {
 	if this == nil {
 		return
 	}
@@ -47,20 +47,27 @@ func (this *Blog) init() {
 	`
 	sql_rows := `
 		SELECT
-			id,
-			user,
-			name,
-			alias,
-			briefly,
-			content,
-			UNIX_TIMESTAMP(datetime) as datetime,
-			active
+			blog_posts.id,
+			blog_posts.user,
+			blog_posts.name,
+			blog_posts.alias,
+			blog_posts.briefly,
+			blog_posts.content,
+			UNIX_TIMESTAMP(blog_posts.datetime) as datetime,
+			blog_posts.active,
+			users.id,
+			users.first_name,
+			users.last_name,
+			users.email,
+			users.admin,
+			users.active
 		FROM
 			blog_posts
+			LEFT JOIN users ON users.id = blog_posts.user
 		WHERE
-			active = 1
+			blog_posts.active = 1
 		ORDER BY
-			id DESC
+			blog_posts.id DESC
 		LIMIT ?, ?;
 	`
 
@@ -119,10 +126,17 @@ func (this *Blog) init() {
 				blog_posts.briefly,
 				blog_posts.content,
 				UNIX_TIMESTAMP(blog_posts.datetime) AS datetime,
-				blog_posts.active
+				blog_posts.active,
+				users.id,
+				users.first_name,
+				users.last_name,
+				users.email,
+				users.admin,
+				users.active
 			FROM
 				blog_posts
 				LEFT JOIN blog_cat_post_rel ON blog_cat_post_rel.post_id = blog_posts.id
+				LEFT JOIN users ON users.id = blog_posts.user
 			WHERE
 				blog_posts.active = 1 AND
 				blog_cat_post_rel.category_id IN (` + strings.Join(cat_ids, ", ") + `)
@@ -146,9 +160,29 @@ func (this *Blog) init() {
 		if rows, err := this.wrap.DB.Query(sql_rows, offset, this.postsPerPage); err == nil {
 			defer rows.Close()
 			for rows.Next() {
-				row := utils.MySql_blog_post{}
-				if err := rows.Scan(&row.A_id, &row.A_user, &row.A_name, &row.A_alias, &row.A_briefly, &row.A_content, &row.A_datetime, &row.A_active); err == nil {
-					this.posts = append(this.posts, &BlogPost{object: &row})
+				rp := utils.MySql_blog_post{}
+				ru := utils.MySql_user{}
+				if err := rows.Scan(
+					&rp.A_id,
+					&rp.A_user,
+					&rp.A_name,
+					&rp.A_alias,
+					&rp.A_briefly,
+					&rp.A_content,
+					&rp.A_datetime,
+					&rp.A_active,
+					&ru.A_id,
+					&ru.A_first_name,
+					&ru.A_last_name,
+					&ru.A_email,
+					&ru.A_admin,
+					&ru.A_active,
+				); err == nil {
+					this.posts = append(this.posts, &BlogPost{
+						wrap:   this.wrap,
+						object: &rp,
+						user:   &User{wrap: this.wrap, object: &ru},
+					})
 				}
 			}
 		}

+ 12 - 3
engine/fetdata/blog_category.go

@@ -1,12 +1,16 @@
 package fetdata
 
 import (
+	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
 )
 
 type BlogCategory struct {
+	wrap   *wrapper.Wrapper
 	object *utils.MySql_blog_category
 	depth  int
+
+	user *User
 }
 
 func (this *BlogCategory) Id() int {
@@ -16,11 +20,16 @@ func (this *BlogCategory) Id() int {
 	return this.object.A_id
 }
 
-func (this *BlogCategory) User() int {
+func (this *BlogCategory) User() *User {
 	if this == nil {
-		return 0
+		return nil
+	}
+	if this.user != nil {
+		return this.user
 	}
-	return this.object.A_user
+	this.user = &User{wrap: this.wrap}
+	this.user.load(this.object.A_user)
+	return this.user
 }
 
 func (this *BlogCategory) Name() string {

+ 12 - 3
engine/fetdata/blog_post.go

@@ -4,11 +4,15 @@ import (
 	"html/template"
 	"time"
 
+	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
 )
 
 type BlogPost struct {
+	wrap   *wrapper.Wrapper
 	object *utils.MySql_blog_post
+
+	user *User
 }
 
 func (this *BlogPost) Id() int {
@@ -18,11 +22,16 @@ func (this *BlogPost) Id() int {
 	return this.object.A_id
 }
 
-func (this *BlogPost) User() int {
+func (this *BlogPost) User() *User {
 	if this == nil {
-		return 0
+		return nil
+	}
+	if this.user != nil {
+		return this.user
 	}
-	return this.object.A_user
+	this.user = &User{wrap: this.wrap}
+	this.user.load(this.object.A_user)
+	return this.user
 }
 
 func (this *BlogPost) Name() string {

+ 5 - 6
engine/fetdata/fetdata.go

@@ -21,22 +21,21 @@ func New(wrap *wrapper.Wrapper, drow interface{}, is404 bool) *FERData {
 
 	if wrap.CurrModule == "index" {
 		if o, ok := drow.(*utils.MySql_page); ok {
-			d_Page = &Page{object: o}
+			d_Page = &Page{wrap: wrap, object: o}
 		}
 	} else if wrap.CurrModule == "blog" {
 		if len(wrap.UrlArgs) == 3 && wrap.UrlArgs[0] == "blog" && wrap.UrlArgs[1] == "category" && wrap.UrlArgs[2] != "" {
 			if o, ok := drow.(*utils.MySql_blog_category); ok {
-				d_Blog = &Blog{wrap: wrap, category: &BlogCategory{object: o}}
-				d_Blog.init()
+				d_Blog = &Blog{wrap: wrap, category: &BlogCategory{wrap: wrap, object: o}}
+				d_Blog.load()
 			}
 		} else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "blog" && wrap.UrlArgs[1] != "" {
 			if o, ok := drow.(*utils.MySql_blog_post); ok {
-				d_Blog = &Blog{wrap: wrap, post: &BlogPost{object: o}}
-				d_Blog.init()
+				d_Blog = &Blog{wrap: wrap, post: &BlogPost{wrap: wrap, object: o}}
 			}
 		} else {
 			d_Blog = &Blog{wrap: wrap}
-			d_Blog.init()
+			d_Blog.load()
 		}
 	}
 

+ 12 - 3
engine/fetdata/index.go

@@ -4,11 +4,15 @@ import (
 	"html/template"
 	"time"
 
+	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
 )
 
 type Page struct {
+	wrap   *wrapper.Wrapper
 	object *utils.MySql_page
+
+	user *User
 }
 
 func (this *Page) Id() int {
@@ -18,11 +22,16 @@ func (this *Page) Id() int {
 	return this.object.A_id
 }
 
-func (this *Page) User() int {
+func (this *Page) User() *User {
 	if this == nil {
-		return 0
+		return nil
+	}
+	if this.user != nil {
+		return this.user
 	}
-	return this.object.A_user
+	this.user = &User{wrap: this.wrap}
+	this.user.load(this.object.A_user)
+	return this.user
 }
 
 func (this *Page) Name() string {

+ 87 - 0
engine/fetdata/user.go

@@ -0,0 +1,87 @@
+package fetdata
+
+import (
+	"golang-fave/engine/wrapper"
+	"golang-fave/utils"
+)
+
+type User struct {
+	wrap   *wrapper.Wrapper
+	object *utils.MySql_user
+}
+
+func (this *User) load(id int) {
+	if this == nil {
+		return
+	}
+	if this.object != nil {
+		return
+	}
+	this.object = &utils.MySql_user{}
+	if err := this.wrap.DB.QueryRow(`
+		SELECT
+			id,
+			first_name,
+			last_name,
+			email,
+			admin,
+			active
+		FROM
+			users
+		WHERE
+			id = ?
+		LIMIT 1;`,
+		id,
+	).Scan(
+		&this.object.A_id,
+		&this.object.A_first_name,
+		&this.object.A_last_name,
+		&this.object.A_email,
+		&this.object.A_admin,
+		&this.object.A_active,
+	); err != nil {
+		return
+	}
+}
+
+func (this *User) Id() int {
+	if this == nil {
+		return 0
+	}
+	return this.object.A_id
+}
+
+func (this *User) FirstName() string {
+	if this == nil {
+		return ""
+	}
+	return this.object.A_first_name
+}
+
+func (this *User) LastName() string {
+	if this == nil {
+		return ""
+	}
+	return this.object.A_last_name
+}
+
+func (this *User) Email() string {
+	if this == nil {
+		return ""
+	}
+	return this.object.A_email
+}
+
+func (this *User) IsAdmin() bool {
+	if this == nil {
+		return false
+	}
+	return this.object.A_admin == 1
+}
+
+func (this *User) IsActive() bool {
+	if this == nil {
+		return false
+	}
+	return this.object.A_active == 1
+}

+ 9 - 1
hosts/localhost/template/blog-category.html

@@ -1,4 +1,11 @@
 {{template "header.html" .}}
+<div class="card mb-4">
+	<div class="post">
+		<div class="card-body">
+			<b>Category author:</b> {{$.Data.Blog.Category.User.FirstName}} {{$.Data.Blog.Category.User.LastName}}
+		</div>
+	</div>
+</div>
 <div class="card mb-4">
 	{{if $.Data.Blog.HavePosts}}
 		{{range $.Data.Blog.Posts}}
@@ -13,7 +20,8 @@
 						{{.Briefly}}
 					</div>
 					<div class="post-date">
-						<small>Published on {{.DateTimeFormat "02/01/2006, 15:04:05"}}</small>
+						<div><small>Published on {{.DateTimeFormat "02/01/2006, 15:04:05"}}</small></div>
+						<div>Author: {{.User.FirstName}} {{.User.LastName}}</div>
 					</div>
 				</div>
 			</div>

+ 2 - 1
hosts/localhost/template/blog-post.html

@@ -7,7 +7,8 @@
 		</div>
 	</div>
 	<div class="card-footer text-muted">
-		Published on {{$.Data.Blog.Post.DateTimeFormat "02/01/2006, 15:04:05"}}
+		<div>Published on {{$.Data.Blog.Post.DateTimeFormat "02/01/2006, 15:04:05"}}</div>
+		<div>Author: {{$.Data.Blog.Post.User.FirstName}} {{$.Data.Blog.Post.User.LastName}}</div>
 	</div>
 </div>
 {{template "footer.html" .}}

+ 2 - 1
hosts/localhost/template/blog.html

@@ -13,7 +13,8 @@
 						{{.Briefly}}
 					</div>
 					<div class="post-date">
-						<small>Published on {{.DateTimeFormat "02/01/2006, 15:04:05"}}</small>
+						<div><small>Published on {{.DateTimeFormat "02/01/2006, 15:04:05"}}</small></div>
+						<div>Author: {{.User.FirstName}} {{.User.LastName}}</div>
 					</div>
 				</div>
 			</div>

+ 2 - 1
hosts/localhost/template/index.html

@@ -7,7 +7,8 @@
 		</div>
 	</div>
 	<div class="card-footer text-muted">
-		Published on {{$.Data.DateTimeFormat "02/01/2006, 15:04:05"}}
+		<div>Published on {{$.Data.Page.DateTimeFormat "02/01/2006, 15:04:05"}}</div>
+		<div>Author: {{$.Data.Page.User.FirstName}} {{$.Data.Page.User.LastName}}</div>
 	</div>
 </div>
 {{template "footer.html" .}}

+ 2 - 1
hosts/localhost/template/page.html

@@ -7,7 +7,8 @@
 		</div>
 	</div>
 	<div class="card-footer text-muted">
-		Published on {{$.Data.Page.DateTimeFormat "02/01/2006, 15:04:05"}}
+		<div>Published on {{$.Data.Page.DateTimeFormat "02/01/2006, 15:04:05"}}</div>
+		<div>Author: {{$.Data.Page.User.FirstName}} {{$.Data.Page.User.LastName}}</div>
 	</div>
 </div>
 {{template "footer.html" .}}