Connect users controller to db
This commit is contained in:
parent
fef066b449
commit
7d234c5aad
@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.kealoha.me/lks/lenslocked/models"
|
||||
"git.kealoha.me/lks/lenslocked/templates"
|
||||
"git.kealoha.me/lks/lenslocked/views"
|
||||
)
|
||||
@ -13,6 +14,7 @@ type Users struct {
|
||||
Templates struct {
|
||||
New Template
|
||||
}
|
||||
UserService *models.UserService
|
||||
}
|
||||
|
||||
func (u Users) New(w http.ResponseWriter, r *http.Request) {
|
||||
@ -22,12 +24,20 @@ func (u Users) New(w http.ResponseWriter, r *http.Request) {
|
||||
data.Email = r.FormValue("email")
|
||||
u.Templates.New.Execute(w, data)
|
||||
}
|
||||
func (u Users) Create(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, "TODO! ", r.FormValue("email"))
|
||||
|
||||
func (u Users) Create(w http.ResponseWriter, r *http.Request) {
|
||||
email := r.FormValue("email")
|
||||
password := r.FormValue("password")
|
||||
user, err := u.UserService.Create(email, password)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "User created: %+v", user)
|
||||
}
|
||||
|
||||
func FromStaticTemplate(templatePath ...string) Users {
|
||||
func WithStaticTemplate(user_service *models.UserService, templatePath ...string) Users {
|
||||
tpl := views.Must(views.FromFS(templates.FS, templatePath...))
|
||||
|
||||
var testWriter strings.Builder
|
||||
@ -38,5 +48,6 @@ func FromStaticTemplate(templatePath ...string) Users {
|
||||
|
||||
u := Users{}
|
||||
u.Templates.New = tpl
|
||||
u.UserService = user_service
|
||||
return u
|
||||
}
|
||||
|
||||
24
main.go
24
main.go
@ -1,12 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
ctrlrs "git.kealoha.me/lks/lenslocked/controllers"
|
||||
"git.kealoha.me/lks/lenslocked/models"
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
_ "github.com/jackc/pgx/v4/stdlib"
|
||||
)
|
||||
|
||||
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
|
||||
@ -15,8 +20,25 @@ func notFoundHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, "404 page not found")
|
||||
}
|
||||
|
||||
func ConnectDB() *sql.DB {
|
||||
db, err := sql.Open("pgx", os.Getenv("LENSLOCKED_DB_STRING"))
|
||||
if err != nil {
|
||||
panic(fmt.Sprint("Error connecting to database: %w", err))
|
||||
}
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
panic(fmt.Sprint("Error connecting to database: %w", err))
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
func main() {
|
||||
var usersCtrlr ctrlrs.Users = ctrlrs.FromStaticTemplate("signup.gohtml", "tailwind.gohtml")
|
||||
db := ConnectDB()
|
||||
defer db.Close()
|
||||
|
||||
userService := models.UserService{DB: db}
|
||||
var usersCtrlr ctrlrs.Users = ctrlrs.WithStaticTemplate(&userService, "signup.gohtml", "tailwind.gohtml")
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.Logger)
|
||||
r.Get("/", ctrlrs.StaticTemplate("home.gohtml", "tailwind.gohtml"))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user