Skip to content

Commit

Permalink
07-Profile-Page add avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfy committed Sep 28, 2018
1 parent db97f90 commit b954926
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
4 changes: 4 additions & 0 deletions cmd/db_init/main.go
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"

"github.com/bonfy/go-mega-code/model"
Expand All @@ -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!"},
},
Expand All @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion config/g.go
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion model/post.go
Expand Up @@ -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"`
}

Expand Down
20 changes: 17 additions & 3 deletions model/user.go
@@ -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"`
}
Expand All @@ -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
Expand All @@ -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
}
10 changes: 7 additions & 3 deletions model/utils.go
Expand Up @@ -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))
}
18 changes: 14 additions & 4 deletions templates/content/profile.html
@@ -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.