Skip to content
Permalink
Browse files

05-Database change view data from database

  • Loading branch information...
bonfy committed Sep 11, 2018
1 parent 8342427 commit d966fd83cd177679322c20386f4788d500ba4026
Showing with 29 additions and 9 deletions.
  1. +9 −0 main.go
  2. +3 −9 vm/index.go
  3. +17 −0 vm/login.go
@@ -4,9 +4,18 @@ import (
"net/http"

"github.com/bonfy/go-mega-code/controller"
"github.com/bonfy/go-mega-code/model"
_ "github.com/jinzhu/gorm/dialects/mysql"
)

func main() {
// Setup DB
db := model.ConnectToDB()
defer db.Close()
model.SetDB(db)

// Setup Controller
controller.Startup()

http.ListenAndServe(":8888", nil)
}
@@ -14,14 +14,8 @@ type IndexViewModelOp struct{}

// GetVM func
func (IndexViewModelOp) GetVM() IndexViewModel {
u1 := model.User{Username: "bonfy"}
u2 := model.User{Username: "rene"}

posts := []model.Post{
model.Post{User: u1, Body: "Beautiful day in Portland!"},
model.Post{User: u2, Body: "The Avengers movie was so cool!"},
}

v := IndexViewModel{BaseViewModel{Title: "Homepage"}, u1, posts}
u1, _ := model.GetUserByUsername("rene")
posts, _ := model.GetPostsByUserID(u1.ID)
v := IndexViewModel{BaseViewModel{Title: "Homepage"}, *u1, *posts}
return v
}
@@ -1,5 +1,11 @@
package vm

import (
"log"

"github.com/bonfy/go-mega-code/model"
)

// LoginViewModel struct
type LoginViewModel struct {
BaseViewModel
@@ -20,3 +26,14 @@ func (LoginViewModelOp) GetVM() LoginViewModel {
v.SetTitle("Login")
return v
}

// CheckLogin func
func CheckLogin(username, password string) bool {
user, err := model.GetUserByUsername(username)
if err != nil {
log.Println("Can not find username: ", username)
log.Println("Error:", err)
return false
}
return user.CheckPassword(password)
}

0 comments on commit d966fd8

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