Routing, templating, and database handling
This commit is contained in:
28
controllers/getController.go
Normal file
28
controllers/getController.go
Normal file
@ -0,0 +1,28 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"GoWeb/app"
|
||||
"GoWeb/templating"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// GetController is a wrapper struct for the App struct
|
||||
type GetController struct {
|
||||
App *app.App
|
||||
}
|
||||
|
||||
func (getController *GetController) ShowHome(w http.ResponseWriter, r *http.Request) {
|
||||
type dataStruct struct {
|
||||
Test string
|
||||
}
|
||||
|
||||
data := dataStruct{
|
||||
Test: "Hello World!",
|
||||
}
|
||||
|
||||
templating.RenderTemplate(getController.App, w, "templates/pages/home.html", data)
|
||||
}
|
||||
|
||||
func (getController *GetController) ShowRegister(w http.ResponseWriter, r *http.Request) {
|
||||
templating.RenderTemplate(getController.App, w, "templates/pages/register.html", nil)
|
||||
}
|
42
controllers/postController.go
Normal file
42
controllers/postController.go
Normal file
@ -0,0 +1,42 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"GoWeb/app"
|
||||
"GoWeb/database/models"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PostController is a wrapper struct for the App struct
|
||||
type PostController struct {
|
||||
App *app.App
|
||||
}
|
||||
|
||||
func (postController *PostController) Register(w http.ResponseWriter, r *http.Request) {
|
||||
username := r.FormValue("username")
|
||||
password := r.FormValue("password")
|
||||
createdAt := time.Now()
|
||||
updatedAt := time.Now()
|
||||
|
||||
if username == "" || password == "" {
|
||||
log.Println("Tried to create user with empty username or password")
|
||||
http.Redirect(w, r, "/register", http.StatusFound)
|
||||
}
|
||||
|
||||
models.CreateUser(postController.App, username, password, createdAt, updatedAt)
|
||||
|
||||
http.Redirect(w, r, "/login", http.StatusFound)
|
||||
}
|
||||
|
||||
func (postController *PostController) Login(w http.ResponseWriter, r *http.Request) {
|
||||
username := r.FormValue("username")
|
||||
password := r.FormValue("password")
|
||||
|
||||
if username == "" || password == "" {
|
||||
log.Println("Tried to create user with empty username or password")
|
||||
http.Redirect(w, r, "/register", http.StatusFound)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/login", http.StatusFound)
|
||||
}
|
Reference in New Issue
Block a user