Routing, templating, and database handling

This commit is contained in:
max
2022-10-20 11:59:33 -05:00
parent 38352de02f
commit e73c006550
10 changed files with 204 additions and 0 deletions

View 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)
}

View 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)
}