basic homepage based on example
This commit is contained in:
parent
77a7bb4bf4
commit
b85ab840c0
@ -2,13 +2,13 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
handler2 "portfolio/api/handler"
|
"portfolio/api/handler"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ApiRoutes(mux **http.ServeMux) {
|
func ApiRoutes(mux **http.ServeMux) {
|
||||||
m := *mux
|
m := *mux
|
||||||
// Register the routes and webHandler
|
// Register the routes and webHandler
|
||||||
m.HandleFunc("/api/", handler2.CatchAllHandler)
|
m.HandleFunc("/api/", handler.CatchAllHandler)
|
||||||
m.HandleFunc("POST /api/user", handler2.CreateUser)
|
m.HandleFunc("POST /api/user", handler.CreateUser)
|
||||||
m.HandleFunc("GET /api/user/{id}", handler2.GetUser)
|
m.HandleFunc("GET /api/user/{id}", handler.GetUser)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ package handler
|
|||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
func InternalServerErrorHandler(w http.ResponseWriter, r *http.Request) {
|
func InternalServerErrorHandler(w http.ResponseWriter) {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
_, err := w.Write([]byte("500 Internal Server Error"))
|
_, err := w.Write([]byte("500 Internal Server Error"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -10,7 +10,7 @@ func InternalServerErrorHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
|
func NotFoundHandler(w http.ResponseWriter) {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
_, err := w.Write([]byte("404 Not Found"))
|
_, err := w.Write([]byte("404 Not Found"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -18,7 +18,7 @@ func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BadRequestHandler(w http.ResponseWriter, r *http.Request) {
|
func BadRequestHandler(w http.ResponseWriter) {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
_, err := w.Write([]byte("400 Bad Request"))
|
_, err := w.Write([]byte("400 Bad Request"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -1,18 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"html/template"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func InitHomepage(w http.ResponseWriter, r *http.Request) {
|
|
||||||
tmpl := template.Must(template.ParseFiles("./frontend/templates/index.html"))
|
|
||||||
err := tmpl.Execute(w, nil)
|
|
||||||
if err != nil {
|
|
||||||
_, err := w.Write([]byte("failed to load"))
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -6,6 +6,6 @@ func CatchAllHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusGone)
|
w.WriteHeader(http.StatusGone)
|
||||||
_, err := w.Write([]byte("Bad endpoint"))
|
_, err := w.Write([]byte("Bad endpoint"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalServerErrorHandler(w, r)
|
InternalServerErrorHandler(w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"portfolio/database/ent"
|
"portfolio/database/ent"
|
||||||
|
"portfolio/database/ent/user"
|
||||||
"portfolio/database/query"
|
"portfolio/database/query"
|
||||||
|
"portfolio/service/validate"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,12 +15,26 @@ func CreateUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
var u *ent.User
|
var u *ent.User
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&u)
|
isHtmx := r.Header.Get("HX-Request")
|
||||||
if err != nil {
|
|
||||||
InternalServerErrorHandler(w, r)
|
if isHtmx == "true" {
|
||||||
|
u = &ent.User{
|
||||||
|
Name: r.PostFormValue("name"),
|
||||||
|
Role: user.Role(r.PostFormValue("role")),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&u)
|
||||||
|
if err != nil {
|
||||||
|
InternalServerErrorHandler(w)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = query.CreateUser(context.Background(), *u)
|
if !validate.UserIsValid(u) {
|
||||||
|
BadRequestHandler(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := query.CreateUser(context.Background(), *u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -31,7 +47,7 @@ func GetUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
userID, err := strconv.Atoi(r.PathValue("id"))
|
userID, err := strconv.Atoi(r.PathValue("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
BadRequestHandler(w, r)
|
BadRequestHandler(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
User, err := query.GetUser(context.Background(), userID)
|
User, err := query.GetUser(context.Background(), userID)
|
||||||
|
|||||||
29
api/webHandler/homepageHandler.go
Normal file
29
api/webHandler/homepageHandler.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package webHandler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
"portfolio/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitHomepage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
tmpl := template.Must(template.ParseFiles("./templates/index.html"))
|
||||||
|
|
||||||
|
userNames := map[string][]types.Username{
|
||||||
|
"Names": {
|
||||||
|
{Name: "The Godfather"},
|
||||||
|
{Name: "Blade Runner"},
|
||||||
|
{Name: "The Thing"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := tmpl.Execute(w, userNames)
|
||||||
|
if err != nil {
|
||||||
|
_, err := w.Write([]byte("failed to load"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,11 +2,11 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"portfolio/api/handler"
|
"portfolio/api/webHandler"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WebRoutes(mux **http.ServeMux) {
|
func WebRoutes(mux **http.ServeMux) {
|
||||||
m := *mux
|
m := *mux
|
||||||
// Register the routes and webHandler
|
// Register the routes and webHandler
|
||||||
m.HandleFunc("GET /{$}", handler.InitHomepage)
|
m.HandleFunc("GET /{$}", webHandler.InitHomepage)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,14 +4,14 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"portfolio/backend/database"
|
"portfolio/database"
|
||||||
"portfolio/backend/database/ent"
|
"portfolio/database/ent"
|
||||||
"portfolio/backend/database/ent/user"
|
"portfolio/database/ent/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetUser(ctx context.Context, id int) (*ent.User, error) {
|
func GetUser(ctx context.Context, id int) (*ent.User, error) {
|
||||||
|
|
||||||
u, err := database.DBclient.User.
|
u, err := database.Client.User.
|
||||||
Query().
|
Query().
|
||||||
Where(user.ID(id)).
|
Where(user.ID(id)).
|
||||||
Only(ctx)
|
Only(ctx)
|
||||||
@ -24,7 +24,7 @@ func GetUser(ctx context.Context, id int) (*ent.User, error) {
|
|||||||
|
|
||||||
func CreateUser(ctx context.Context, User ent.User) error {
|
func CreateUser(ctx context.Context, User ent.User) error {
|
||||||
|
|
||||||
_, err := database.DBclient.User.
|
_, err := database.Client.User.
|
||||||
Create().
|
Create().
|
||||||
SetName(User.Name).
|
SetName(User.Name).
|
||||||
SetRole(User.Role).
|
SetRole(User.Role).
|
||||||
|
|||||||
13
service/validate/validateUser.go
Normal file
13
service/validate/validateUser.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package validate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"portfolio/database/ent"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UserIsValid(u *ent.User) bool {
|
||||||
|
if len(u.Name) > 0 &&
|
||||||
|
len(u.Role) > 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@ -13,30 +13,29 @@
|
|||||||
|
|
||||||
<div class="row mt-4 g-4">
|
<div class="row mt-4 g-4">
|
||||||
<div class="col-8">
|
<div class="col-8">
|
||||||
<h1 class="mb-4">Film List</h1>
|
<h1 class="mb-4">User List</h1>
|
||||||
|
|
||||||
<ul class="list-group fs-5 me-5" id="film-list">
|
<ul class="list-group fs-5 me-5" id="user-list">
|
||||||
{{ range .Films }}
|
{{ range .Names }}
|
||||||
{{ block "film-list-element" .}}
|
{{ block "user-list" .}}
|
||||||
<li class="list-group-item bg-primary text-white">{{ .Title }} - {{ .Director }}</li>
|
<li class="list-group-item bg-primary text-white">{{ .Name }}</li>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-4">
|
<div class="col-4">
|
||||||
<h1 class="mb-4">Add Film</h1>
|
<h1 class="mb-4">Add User</h1>
|
||||||
|
|
||||||
<form hx-post="/add-film/" hx-target="#film-list" hx-swap="beforeend" hx-indicator="#spinner">
|
<form hx-post="/api/user" hx-target="#user-form" hx-swap="beforeend" hx-indicator="#spinner" id="user-form">
|
||||||
<div class="mb-2">
|
<div class="mb-2">
|
||||||
<label for="film-title">Title</label>
|
<label for="user-name">Title</label>
|
||||||
<input type="text" name="title" id="film-title" class="form-control" />
|
<input type="text" name="name" id="user-name" class="form-control" />
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-2">
|
||||||
<label for="film-director">Director</label>
|
<label for="user-name">Title</label>
|
||||||
<input type="text" name="director" id="film-director" class="form-control" />
|
<input type="text" name="role" id="user-role" class="form-control" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button type="submit" class="btn btn-primary">
|
<button type="submit" class="btn btn-primary">
|
||||||
<span class="spinner-border spinner-border-sm htmx-indicator" id="spinner" role="status" aria-hidden="true"></span>
|
<span class="spinner-border spinner-border-sm htmx-indicator" id="spinner" role="status" aria-hidden="true"></span>
|
||||||
Submit
|
Submit
|
||||||
|
|||||||
5
types/userTypes.go
Normal file
5
types/userTypes.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
type Username struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user