enabled tab switching

This commit is contained in:
2024-03-03 04:19:57 +00:00
parent ea4bf43e5e
commit 9b2caddeb4
12 changed files with 275 additions and 119 deletions

90
data.go
View File

@@ -1,90 +0,0 @@
package main
import (
"strconv"
)
var data []Company
type Company struct {
ID string
Company string
Contact string
Country string
}
func init() {
data = []Company{
{
ID: "1",
Company: "Amazon",
Contact: "Jeff Bezos",
Country: "United States",
},
{
ID: "2",
Company: "Apple",
Contact: "Tim Cook",
Country: "United States",
},
{
ID: "3",
Company: "Microsoft",
Contact: "Satya Nadella",
Country: "United States",
},
}
}
func getCompanyByID(id string) Company {
var result Company
for _, i := range data {
if i.ID == id {
result = i
break
}
}
return result
}
func updateCompany(company Company) {
result := []Company{}
for _, i := range data {
if i.ID == company.ID {
i.Company = company.Company
i.Contact = company.Contact
i.Country = company.Country
}
result = append(result, i)
}
data = result
}
func addCompany(company Company) {
max := 0
for _, i := range data {
n, _ := strconv.Atoi(i.ID)
if n > max {
max = n
}
}
max++
id := strconv.Itoa(max)
data = append(data, Company{
ID: id,
Company: company.Company,
Contact: company.Contact,
Country: company.Country,
})
}
func deleteCompany(id string) {
result := []Company{}
for _, i := range data {
if i.ID != id {
result = append(result, i)
}
}
data = result
}

View File

@@ -93,6 +93,36 @@ func PostBox(box Box) (int64, error) {
return result.LastInsertId() return result.LastInsertId()
} }
func GetAllBoxes() (result []Box, err error) {
db, err := CreateClient()
if err != nil {
return nil, err
}
defer db.Close()
rows, err := db.Query("SELECT * FROM boxes")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
box := Box{}
err = rows.Scan(&box.ID, &box.Name, &box.Notes, &box.Description, &box.Stage, &box.Category)
if err != nil {
return nil, err
}
result = append(result, box)
}
return
}
// func PostBoxItem(itemid int, boxid int) (int64, error) { // func PostBoxItem(itemid int, boxid int) (int64, error) {
// db, err := CreateClient() // db, err := CreateClient()
// if err != nil { // if err != nil {

12
main.go
View File

@@ -12,6 +12,7 @@ import (
"time" "time"
"github.com/innocuous-symmetry/moving-mgmt/db" "github.com/innocuous-symmetry/moving-mgmt/db"
"github.com/innocuous-symmetry/moving-mgmt/routes"
"github.com/jritsema/gotoolbox" "github.com/jritsema/gotoolbox"
"github.com/jritsema/gotoolbox/web" "github.com/jritsema/gotoolbox/web"
@@ -49,14 +50,9 @@ func main() {
// router.Handle("/company/add", web.Action(companyAdd)) // router.Handle("/company/add", web.Action(companyAdd))
// router.Handle("/company/add/", web.Action(companyAdd)) // router.Handle("/company/add/", web.Action(companyAdd))
// router.Handle("/company/edit", web.Action(companyEdit)) router.Handle("/", web.Action(routes.HomePage))
// router.Handle("/company/edit/", web.Action(companyEdit)) router.Handle("/items", web.Action(routes.Items(html).GetAll))
router.Handle("/boxes", web.Action(routes.Boxes(html).GetAll))
// router.Handle("/company", web.Action(companies))
// router.Handle("/company/", web.Action(companies))
router.Handle("/", web.Action(index))
router.Handle("/index.html", web.Action(index))
//logging/tracing //logging/tracing
nextRequestID := func() string { nextRequestID := func() string {

View File

@@ -1,17 +0,0 @@
package main
import (
"net/http"
db "github.com/innocuous-symmetry/moving-mgmt/db"
"github.com/jritsema/gotoolbox/web"
)
func index(r *http.Request) *web.Response {
result, err := db.GetAllItems()
if err != nil {
panic(err)
}
return web.HTML(http.StatusOK, html, "index.html", result, nil)
}

45
routes/base.go Normal file
View File

@@ -0,0 +1,45 @@
package routes
import (
"net/http"
"github.com/jritsema/gotoolbox/web"
)
type Entity int
const (
Item Entity = iota
Box
BoxItem
)
type RouterActions struct {
GetAll func(r *http.Request) *web.Response
GetByID func(r *http.Request) *web.Response
Post func(r *http.Request) *web.Response
Put func(r *http.Request) *web.Response
Delete func(r *http.Request) *web.Response
}
type Router struct {
Entity Entity
Path string
GetAll func(r *http.Request) *web.Response
GetByID func(r *http.Request) *web.Response
Post func(r *http.Request) *web.Response
Put func(r *http.Request) *web.Response
Delete func(r *http.Request) *web.Response
}
func NewRouter(entity Entity, path string, actions RouterActions) *Router {
return &Router{
Entity: entity,
Path: path,
GetAll: actions.GetAll,
GetByID: actions.GetByID,
Post: actions.Post,
Put: actions.Put,
Delete: actions.Delete,
}
}

40
routes/boxes.go Normal file
View File

@@ -0,0 +1,40 @@
package routes
import (
"html/template"
"net/http"
"github.com/innocuous-symmetry/moving-mgmt/db"
"github.com/jritsema/gotoolbox/web"
)
func Boxes(_html *template.Template) *Router {
html = _html
return NewRouter(
Box,
"/boxes",
RouterActions{
GetAll: GetAllBoxes,
GetByID: nil,
Post: nil,
Put: nil,
Delete: nil,
},
)
}
func GetAllBoxes(_ *http.Request) *web.Response {
result, err := db.GetAllBoxes()
if err != nil {
return web.Error(http.StatusBadRequest, err, nil)
}
return web.HTML(
http.StatusOK,
html,
"entity-list.html",
result,
nil,
)
}

26
routes/index.go Normal file
View File

@@ -0,0 +1,26 @@
package routes
import (
"html/template"
"net/http"
// "github.com/innocuous-symmetry/moving-mgmt/"
db "github.com/innocuous-symmetry/moving-mgmt/db"
"github.com/jritsema/gotoolbox/web"
)
var html *template.Template
func HomePage(r *http.Request) *web.Response {
result, err := db.GetAllItems()
if err != nil {
panic(err)
}
return web.HTML(
http.StatusOK,
html,
"index.html",
result,
nil,
)
}

63
routes/items.go Normal file
View File

@@ -0,0 +1,63 @@
package routes
import (
"html/template"
"net/http"
"strconv"
db "github.com/innocuous-symmetry/moving-mgmt/db"
"github.com/jritsema/gotoolbox/web"
)
func Items(_html *template.Template) *Router {
html = _html
return NewRouter(
Item,
"/items",
RouterActions{
GetAll: GetAllItems,
GetByID: nil,
Post: nil,
Put: nil,
Delete: nil,
},
)
}
func GetAllItems(_ *http.Request) *web.Response {
result, err := db.GetAllItems()
if err != nil {
panic(err)
}
return web.HTML(
http.StatusOK,
html,
"entity-list.html",
result,
nil,
)
}
func GetItemByID(r *http.Request) *web.Response {
var id int
id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
return web.Error(http.StatusBadRequest, err, nil)
}
result, err := db.GetItemByID(id)
if err != nil {
return web.Error(http.StatusInternalServerError, err, nil)
}
return web.HTML(
http.StatusOK,
html,
"item-by-id.html",
result,
nil,
)
}

View File

@@ -1,4 +1,4 @@
<div id="companies"> <div id="box-list">
<div id="processing" class="htmx-indicator">Processing...</div> <div id="processing" class="htmx-indicator">Processing...</div>
<div class="flex flex-col"> <div class="flex flex-col">
<div class="overflow-x-auto sm:-mx-6 lg:-mx-8"> <div class="overflow-x-auto sm:-mx-6 lg:-mx-8">
@@ -29,7 +29,7 @@
</thead> </thead>
<tbody id="table-body"> <tbody id="table-body">
{{range .}} {{range .}}
{{ template "item-row.html". }} {{ template "entity-row.html". }}
{{end}} {{end}}
</tbody> </tbody>
</table> </table>

View File

@@ -0,0 +1,40 @@
<div id="entity-list">
<div id="processing" class="htmx-indicator">Processing...</div>
<div class="flex flex-col">
<div class="overflow-x-auto sm:-mx-6 lg:-mx-8">
<div class="inline-block min-w-full py-2 sm:px-6 lg:px-8">
<div class="overflow-hidden">
<!-- <div class="flex justify-end">
<button
hx-get="/company/add"
hx-target="#table-body"
hx-swap="outerHTML"
hx-indicator="#processing"
class="inline-flex items-center h-8 px-4 m-2 text-sm text-blue-100 transition-colors duration-150 bg-blue-700 rounded-lg focus:shadow-outline hover:bg-blue-800"
href=""
>
Add
</button>
</div> -->
<table class="min-w-full text-left text-sm font-light">
<thead class="border-b font-medium dark:border-neutral-500">
<tr>
<th scope="col" class="px-6 py-4">#</th>
<th scope="col" class="px-6 py-4">Name</th>
<th scope="col" class="px-6 py-4">Stage</th>
<th scope="col" class="px-6 py-4">Category</th>
<th scope="col" class="px-6 py-4">Description</th>
<th scope="col" class="px-6 py-4">Notes</th>
</tr>
</thead>
<tbody id="table-body">
{{range .}}
{{ template "entity-row.html". }}
{{end}}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>

View File

@@ -12,9 +12,32 @@
<main> <main>
<div class="md:container md:mx-auto"> <div class="md:container md:mx-auto">
<h1 class="text-3xl">Mikayla's Move Manager</h1> <h1 class="text-3xl">Mikayla's Move Manager</h1>
<nav>
<button
hx-get="/items"
hx-target="#home-page-container"
hx-swap="innerHTML"
class="inline-flex items-center h-8 px-4 m-2 text-sm text-blue-100 transition-colors duration-150 bg-blue-700 rounded-lg focus:shadow-outline hover:bg-blue-800"
>
Items
</button>
<button
hx-get="/boxes"
hx-target="#home-page-container"
hx-swap="innerHTML"
class="inline-flex items-center h-8 px-4 m-2 text-sm text-blue-100 transition-colors duration-150 bg-blue-700 rounded-lg focus:shadow-outline hover:bg-blue-800"
>
Boxes
</button>
<button>Search</button>
</nav>
<br/> <br/>
<span class="text-xl">Companies</span> <span class="text-xl">Items</span>
<div>{{ template "items.html" . }}</div> <div id="home-page-container">
{{ template "entity-list.html" . }}
</div>
</div> </div>
</main> </main>
</body> </body>