basic homepage based on example
This commit is contained in:
parent
77a7bb4bf4
commit
b85ab840c0
@ -2,13 +2,13 @@ package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
handler2 "portfolio/api/handler"
|
||||
"portfolio/api/handler"
|
||||
)
|
||||
|
||||
func ApiRoutes(mux **http.ServeMux) {
|
||||
m := *mux
|
||||
// Register the routes and webHandler
|
||||
m.HandleFunc("/api/", handler2.CatchAllHandler)
|
||||
m.HandleFunc("POST /api/user", handler2.CreateUser)
|
||||
m.HandleFunc("GET /api/user/{id}", handler2.GetUser)
|
||||
m.HandleFunc("/api/", handler.CatchAllHandler)
|
||||
m.HandleFunc("POST /api/user", handler.CreateUser)
|
||||
m.HandleFunc("GET /api/user/{id}", handler.GetUser)
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ package handler
|
||||
|
||||
import "net/http"
|
||||
|
||||
func InternalServerErrorHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func InternalServerErrorHandler(w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_, err := w.Write([]byte("500 Internal Server Error"))
|
||||
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)
|
||||
_, err := w.Write([]byte("404 Not Found"))
|
||||
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)
|
||||
_, err := w.Write([]byte("400 Bad Request"))
|
||||
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)
|
||||
_, err := w.Write([]byte("Bad endpoint"))
|
||||
if err != nil {
|
||||
InternalServerErrorHandler(w, r)
|
||||
InternalServerErrorHandler(w)
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,9 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"portfolio/database/ent"
|
||||
"portfolio/database/ent/user"
|
||||
"portfolio/database/query"
|
||||
"portfolio/service/validate"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@ -13,12 +15,26 @@ func CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var u *ent.User
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&u)
|
||||
if err != nil {
|
||||
InternalServerErrorHandler(w, r)
|
||||
isHtmx := r.Header.Get("HX-Request")
|
||||
|
||||
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 {
|
||||
return
|
||||
}
|
||||
@ -31,7 +47,7 @@ func GetUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
userID, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
BadRequestHandler(w, r)
|
||||
BadRequestHandler(w)
|
||||
}
|
||||
|
||||
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 (
|
||||
"net/http"
|
||||
"portfolio/api/handler"
|
||||
"portfolio/api/webHandler"
|
||||
)
|
||||
|
||||
func WebRoutes(mux **http.ServeMux) {
|
||||
m := *mux
|
||||
// Register the routes and webHandler
|
||||
m.HandleFunc("GET /{$}", handler.InitHomepage)
|
||||
m.HandleFunc("GET /{$}", webHandler.InitHomepage)
|
||||
}
|
||||
|
||||
@ -4,14 +4,14 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"portfolio/backend/database"
|
||||
"portfolio/backend/database/ent"
|
||||
"portfolio/backend/database/ent/user"
|
||||
"portfolio/database"
|
||||
"portfolio/database/ent"
|
||||
"portfolio/database/ent/user"
|
||||
)
|
||||
|
||||
func GetUser(ctx context.Context, id int) (*ent.User, error) {
|
||||
|
||||
u, err := database.DBclient.User.
|
||||
u, err := database.Client.User.
|
||||
Query().
|
||||
Where(user.ID(id)).
|
||||
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 {
|
||||
|
||||
_, err := database.DBclient.User.
|
||||
_, err := database.Client.User.
|
||||
Create().
|
||||
SetName(User.Name).
|
||||
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="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">
|
||||
{{ range .Films }}
|
||||
{{ block "film-list-element" .}}
|
||||
<li class="list-group-item bg-primary text-white">{{ .Title }} - {{ .Director }}</li>
|
||||
<ul class="list-group fs-5 me-5" id="user-list">
|
||||
{{ range .Names }}
|
||||
{{ block "user-list" .}}
|
||||
<li class="list-group-item bg-primary text-white">{{ .Name }}</li>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<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">
|
||||
<label for="film-title">Title</label>
|
||||
<input type="text" name="title" id="film-title" class="form-control" />
|
||||
<label for="user-name">Title</label>
|
||||
<input type="text" name="name" id="user-name" class="form-control" />
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="film-director">Director</label>
|
||||
<input type="text" name="director" id="film-director" class="form-control" />
|
||||
<div class="mb-2">
|
||||
<label for="user-name">Title</label>
|
||||
<input type="text" name="role" id="user-role" class="form-control" />
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<span class="spinner-border spinner-border-sm htmx-indicator" id="spinner" role="status" aria-hidden="true"></span>
|
||||
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