enabled tab switching
This commit is contained in:
90
data.go
90
data.go
@@ -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
|
||||
}
|
||||
30
db/sql.go
30
db/sql.go
@@ -93,6 +93,36 @@ func PostBox(box Box) (int64, error) {
|
||||
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) {
|
||||
// db, err := CreateClient()
|
||||
// if err != nil {
|
||||
|
||||
12
main.go
12
main.go
@@ -12,6 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/innocuous-symmetry/moving-mgmt/db"
|
||||
"github.com/innocuous-symmetry/moving-mgmt/routes"
|
||||
|
||||
"github.com/jritsema/gotoolbox"
|
||||
"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/edit", web.Action(companyEdit))
|
||||
// router.Handle("/company/edit/", web.Action(companyEdit))
|
||||
|
||||
// router.Handle("/company", web.Action(companies))
|
||||
// router.Handle("/company/", web.Action(companies))
|
||||
|
||||
router.Handle("/", web.Action(index))
|
||||
router.Handle("/index.html", web.Action(index))
|
||||
router.Handle("/", web.Action(routes.HomePage))
|
||||
router.Handle("/items", web.Action(routes.Items(html).GetAll))
|
||||
router.Handle("/boxes", web.Action(routes.Boxes(html).GetAll))
|
||||
|
||||
//logging/tracing
|
||||
nextRequestID := func() string {
|
||||
|
||||
17
routes.go
17
routes.go
@@ -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
45
routes/base.go
Normal 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
40
routes/boxes.go
Normal 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
26
routes/index.go
Normal 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
63
routes/items.go
Normal 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,
|
||||
)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<div id="companies">
|
||||
<div id="box-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">
|
||||
@@ -29,7 +29,7 @@
|
||||
</thead>
|
||||
<tbody id="table-body">
|
||||
{{range .}}
|
||||
{{ template "item-row.html". }}
|
||||
{{ template "entity-row.html". }}
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
40
templates/entity-list.html
Normal file
40
templates/entity-list.html
Normal 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>
|
||||
@@ -12,9 +12,32 @@
|
||||
<main>
|
||||
<div class="md:container md:mx-auto">
|
||||
<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/>
|
||||
<span class="text-xl">Companies</span>
|
||||
<div>{{ template "items.html" . }}</div>
|
||||
<span class="text-xl">Items</span>
|
||||
<div id="home-page-container">
|
||||
{{ template "entity-list.html" . }}
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user