Skip to content
Permalink
Browse files

07-Profile-Page add avatar

  • Loading branch information...
bonfy committed Sep 28, 2018
1 parent db97f90 commit b9549261e9d4f0e55ea9612879101d36f32ee388
Showing with 44 additions and 12 deletions.
  1. +4 −0 cmd/db_init/main.go
  2. +1 −1 config/g.go
  3. +1 −1 model/post.go
  4. +17 −3 model/user.go
  5. +7 −3 model/utils.go
  6. +14 −4 templates/content/profile.html
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"

"github.com/bonfy/go-mega-code/model"
@@ -20,6 +21,8 @@ func main() {
{
Username: "bonfy",
PasswordHash: model.GeneratePasswordHash("abc123"),
Email: "i@bonfy.im",
Avatar: fmt.Sprintf("https://www.gravatar.com/avatar/%s?d=identicon", model.Md5("i@bonfy.im")),
Posts: []model.Post{
{Body: "Beautiful day in Portland!"},
},
@@ -28,6 +31,7 @@ func main() {
Username: "rene",
PasswordHash: model.GeneratePasswordHash("abc123"),
Email: "rene@test.com",
Avatar: fmt.Sprintf("https://www.gravatar.com/avatar/%s?d=identicon", model.Md5("rene@test.com")),
Posts: []model.Post{
{Body: "The Avengers movie was so cool!"},
{Body: "Sun shine is beautiful"},
@@ -32,5 +32,5 @@ func GetMysqlConnectingString() string {
db := viper.GetString("mysql.db")
charset := viper.GetString("mysql.charset")

return fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=%s&parseTime=true", usr, pwd, host, db, charset)
return fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=%s&parseTime=true&loc=Local", usr, pwd, host, db, charset)
}
@@ -9,7 +9,7 @@ type Post struct {
ID int `gorm:"primary_key"`
UserID int
User User
Body string `gorm:"varchar(180)"`
Body string `gorm:"type:varchar(180)"`
Timestamp *time.Time `sql:"DEFAULT:current_timestamp"`
}

@@ -1,11 +1,19 @@
package model

import (
"fmt"
"time"
)

// User struct
type User struct {
ID int `gorm:"primary_key"`
Username string `gorm:"varchar(64)"`
Email string `gorm:"varchar(12)"`
PasswordHash string `gorm:"varchar(128)"`
Username string `gorm:"type:varchar(64)"`
Email string `gorm:"type:varchar(120)"`
PasswordHash string `gorm:"type:varchar(128)"`
LastSeen *time.Time
AboutMe string `gorm:"type:varchar(140)"`
Avatar string `gorm:"type:varchar(200)"`
Posts []Post
Followers []*User `gorm:"many2many:follower;association_jointable_foreignkey:follower_id"`
}
@@ -15,6 +23,11 @@ func (u *User) SetPassword(password string) {
u.PasswordHash = GeneratePasswordHash(password)
}

// SetAvatar func: Set PasswordHash
func (u *User) SetAvatar(email string) {
u.Avatar = fmt.Sprintf("https://www.gravatar.com/avatar/%s?d=identicon", Md5(email))
}

// CheckPassword func
func (u *User) CheckPassword(password string) bool {
return GeneratePasswordHash(password) == u.PasswordHash
@@ -33,5 +46,6 @@ func GetUserByUsername(username string) (*User, error) {
func AddUser(username, password, email string) error {
user := User{Username: username, Email: email}
user.SetPassword(password)
user.SetAvatar(email)
return db.Create(&user).Error
}
@@ -7,8 +7,12 @@ import (

// GeneratePasswordHash : Use MD5
func GeneratePasswordHash(pwd string) string {
return Md5(pwd)
}

// Md5 func
func Md5(origin string) string {
hasher := md5.New()
hasher.Write([]byte(pwd))
pwdHash := hex.EncodeToString(hasher.Sum(nil))
return pwdHash
hasher.Write([]byte(origin))
return hex.EncodeToString(hasher.Sum(nil))
}
@@ -1,11 +1,21 @@
{{define "content"}}
<h1>User: {{.ProfileUser.Username}}</h1>

<table>
<tr valign="top">
<td><img src="{{.ProfileUser.Avatar}}&s=128"></td>
<td><h1>User: {{.ProfileUser.Username}}</h1></td>
</tr>
</table>

<hr/>

{{range .Posts}}
<p>
{{ .User.Username }} says: <b>{{ .Body }}</b>
</p>
<table>
<tr valign="top">
<td><img src="{{.User.Avatar}}&s=36"></td>
<td>{{ .User.Username }} says:<br>{{ .Body }}</td>
</tr>
</table>
{{end}}

{{end}}

0 comments on commit b954926

Please sign in to comment.
You can’t perform that action at this time.