diff --git a/cmd/server/main.go b/cmd/server/main.go index 88ff6fa..1592ac1 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,51 +1,50 @@ package main import ( - "fmt" + "log/slog" "net/http" - "git.proximination.com/pcpelatihan/wstempl/component" + view "git.proximination.com/pclatihan/wstempl/component" + "git.proximination.com/pclatihan/wstempl/mock" + "git.proximination.com/pclatihan/wstempl/model" "github.com/go-chi/chi/v5" ) +type SumberData interface { + GetBarang(nama string) (model.Barang, error) + GetDaftarBarang() (model.DaftarBarang, error) +} + func main() { - port := ":8123" + router := chi.NewRouter() + db := mock.NewMockV2() - router.Get("/", HandleHome) - router.Get("/satu", HandleFormSatu) - router.Get("/dua", HandleFormDua) - router.Get("/tiga", HandleFormTiga) - router.Get("/coba", HandleCoba) + router.Get("/", HandleDaftarBarang(db)) + router.Get("/barang/{nama}", HandleNamaBarang(db)) - server := http.Server{ - Addr: port, - Handler: router, - } + // fmt.Println("server running at http://localhost:8080") + slog.Info("server running at http://localhost:8080") - fmt.Printf("server jalan di http://localhost%s", port) - - if err := server.ListenAndServe(); err != nil { + if err := http.ListenAndServe(":8080", router); err != nil { panic(err) } + } -func HandleHome(w http.ResponseWriter, r *http.Request) { - component.HomePage().Render(r.Context(), w) +func HandleDaftarBarang(db SumberData) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + daftar, _ := db.GetDaftarBarang() + + view.DaftarBarangView(daftar).Render(r.Context(), w) + } } -func HandleFormSatu(w http.ResponseWriter, r *http.Request) { - component.FormSatu().Render(r.Context(), w) -} - -func HandleFormDua(w http.ResponseWriter, r *http.Request) { - component.FormDua().Render(r.Context(), w) -} - -func HandleFormTiga(w http.ResponseWriter, r *http.Request) { - component.FormTiga().Render(r.Context(), w) -} - -func HandleCoba(w http.ResponseWriter, r *http.Request) { - component.Try().Render(r.Context(), w) +func HandleNamaBarang(db SumberData) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + path := chi.URLParam(r, "nama") + slog.Info(path) + model, _ := db.GetBarang(path) + view.BarangView(model).Render(r.Context(), w) + } } diff --git a/component/barang.templ b/component/barang.templ new file mode 100644 index 0000000..8204467 --- /dev/null +++ b/component/barang.templ @@ -0,0 +1,45 @@ +package component + +import "git.proximination.com/pclatihan/wstempl/model" + +templ BarangView(mdl model.Barang) { +
+

{ mdl.Nama }

+
{ mdl.Merk }
+ + + + + + + + + + + + + + + + + + + + + +
PANJANG{ mdl.Panjang }
LEBAR{ mdl.Lebar }
TINGGI{ mdl.Tinggi }
SATUAN{ mdl.Unit }
STATUS{ mdl.Status }
+ BACK +
+} + +templ DaftarBarangView(mdl model.DaftarBarang) { + @Common("DAFTAR") { + + } +} diff --git a/component/common.templ b/component/common.templ new file mode 100644 index 0000000..e7bbed2 --- /dev/null +++ b/component/common.templ @@ -0,0 +1,24 @@ +package component + +templ Common(pagename string) { + + + + + + { pagename } + + + + { children... } + + +} diff --git a/go.mod b/go.mod index 91c4cd7..20bce1e 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,20 @@ -module git.proximination.com/pcpelatihan/wstempl +module git.proximination.com/pclatihan/wstempl go 1.24.1 +require ( + github.com/a-h/templ v0.3.865 + github.com/brianvoe/gofakeit/v7 v7.2.1 + github.com/go-chi/chi/v5 v5.2.1 +) + require ( github.com/a-h/parse v0.0.0-20250122154542-74294addb73e // indirect - github.com/a-h/templ v0.3.865 // indirect github.com/andybalholm/brotli v1.1.0 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cli/browser v1.3.0 // indirect github.com/fatih/color v1.16.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect - github.com/go-chi/chi/v5 v5.2.1 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/natefinch/atomic v1.0.1 // indirect diff --git a/go.sum b/go.sum index f8bd014..cbaf8f7 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/a-h/templ v0.3.865 h1:nYn5EWm9EiXaDgWcMQaKiKvrydqgxDUtT1+4zU2C43A= github.com/a-h/templ v0.3.865/go.mod h1:oLBbZVQ6//Q6zpvSMPTuBK0F3qOtBdFBcGRspcT+VNQ= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= +github.com/brianvoe/gofakeit/v7 v7.2.1 h1:AGojgaaCdgq4Adzrd2uWdbGNDyX6MWNhHdQBraNfOHI= +github.com/brianvoe/gofakeit/v7 v7.2.1/go.mod h1:QXuPeBw164PJCzCUZVmgpgHJ3Llj49jSLVkKPMtxtxA= github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/cli/browser v1.3.0 h1:LejqCrpWr+1pRqmEPDGnTZOjsMe7sehifLynZJuqJpo= @@ -14,6 +16,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8= github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= diff --git a/mock/mock.go b/mock/mock.go new file mode 100644 index 0000000..1617726 --- /dev/null +++ b/mock/mock.go @@ -0,0 +1,33 @@ +package mock + +import "git.proximination.com/pclatihan/wstempl/model" + +type Mock struct{} + +func NewMock() *Mock { + return &Mock{} +} + +func (m Mock) GetBarang(nama string) (model.Barang, error) { + model := model.Barang{ + Nama: nama, + Merk: "Ini dari DB pura-pura mau ngarep apalagi", + Panjang: "12", + Lebar: "10", + Tinggi: "12", + Unit: "Buah", + } + return model, nil +} + +func (m Mock) GetDaftarBarang() (model.DaftarBarang, error) { + daftar := model.DaftarBarang{ + Items: []model.BarangDalamList{ + {Nama: "barang1", Merk: "abalabal", Url: "barang/barang1"}, + {Nama: "barang2", Merk: "abalabal", Url: "barang/barang2"}, + {Nama: "barang3", Merk: "abalabal", Url: "barang/barang3"}, + {Nama: "barang4", Merk: "abalabal", Url: "barang/barang4"}, + }, + } + return daftar, nil +} diff --git a/mock/mockv2.go b/mock/mockv2.go new file mode 100644 index 0000000..5d6b7b8 --- /dev/null +++ b/mock/mockv2.go @@ -0,0 +1,49 @@ +package mock + +import ( + "fmt" + + "git.proximination.com/pclatihan/wstempl/model" + "github.com/brianvoe/gofakeit/v7" +) + +type MockV2 struct{} + +func NewMockV2() *MockV2 { + return &MockV2{} +} + +func (m MockV2) GetBarang(nama string) (model.Barang, error) { + model := model.Barang{ + Nama: nama, + Merk: gofakeit.Name(), + Panjang: gofakeit.Numerify("#"), + Lebar: gofakeit.Numerify("#"), + Tinggi: gofakeit.Numerify("#"), + Unit: gofakeit.AppName(), + Status: gofakeit.RandomString([]string{"Sehat", "Rusak", "Dicuri Orang"}), + } + return model, nil +} + +func (m MockV2) GetDaftarBarang() (model.DaftarBarang, error) { + daftar := model.DaftarBarang{ + Items: DaftarBoongan(25), + } + return daftar, nil +} + +func DaftarBoongan(num int) []model.BarangDalamList { + result := []model.BarangDalamList{} + + for i := 0; i < num; i++ { + nama := gofakeit.Word() + data := model.BarangDalamList{ + Nama: nama, + Merk: gofakeit.ProductCategory(), + Url: fmt.Sprintf("barang/%s", nama), + } + result = append(result, data) + } + return result +} diff --git a/model/barang.go b/model/barang.go new file mode 100644 index 0000000..55b8225 --- /dev/null +++ b/model/barang.go @@ -0,0 +1,11 @@ +package model + +type Barang struct { + Nama string + Merk string + Panjang string + Lebar string + Tinggi string + Unit string + Status string +} diff --git a/model/listbarang.go b/model/listbarang.go new file mode 100644 index 0000000..d9503dd --- /dev/null +++ b/model/listbarang.go @@ -0,0 +1,11 @@ +package model + +type BarangDalamList struct { + Nama string + Merk string + Url string +} + +type DaftarBarang struct { + Items []BarangDalamList +}