item put functionality
This commit is contained in:
@@ -22,21 +22,21 @@ const (
|
||||
|
||||
// entities
|
||||
type Item struct {
|
||||
ID int
|
||||
Name string
|
||||
Notes *string
|
||||
Description *string
|
||||
Stage PackingStage
|
||||
Category Category
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Notes *string `json:"notes"`
|
||||
Description *string `json:"description"`
|
||||
Stage PackingStage `json:"stage"`
|
||||
Category Category `json:"category"`
|
||||
}
|
||||
|
||||
type Box struct {
|
||||
ID int
|
||||
Name string
|
||||
Notes *string
|
||||
Description *string
|
||||
Stage PackingStage
|
||||
Category Category
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Notes *string `json:"notes"`
|
||||
Description *string `json:"description"`
|
||||
Stage PackingStage `json:"stage"`
|
||||
Category Category `json:"category"`
|
||||
}
|
||||
|
||||
// joins
|
||||
|
||||
199
db/sql.go
199
db/sql.go
@@ -38,61 +38,6 @@ func GetAllItems() (result []Item, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func GetItemByID(id int) (item Item, err error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return Item{}, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
row := db.QueryRow("SELECT * FROM items WHERE id = ?", id)
|
||||
err = row.Scan(&item.ID, &item.Name, &item.Notes, &item.Description, &item.Stage, &item.Category)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func PostItem(item Item) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `INSERT INTO items (name, notes, description, stage, category)
|
||||
VALUES (?, ?, ?, ?, ?)`
|
||||
|
||||
result, err := db.Exec(query, item.Name, item.Notes, item.Description, item.Stage, item.Category)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
|
||||
|
||||
func PostBox(box Box) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `INSERT INTO boxes (name, notes, description, stage, category) VALUES (?, ?, ?, ?, ?)`
|
||||
|
||||
result, err := db.Exec(query, box.Name, box.Notes, box.Description, box.Stage, box.Category)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
func GetAllBoxes() (result []Box, err error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
@@ -123,6 +68,150 @@ func GetAllBoxes() (result []Box, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func GetItemByID(id int) (item Item, err error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return Item{}, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
row := db.QueryRow("SELECT * FROM items WHERE id = ?", id)
|
||||
err = row.Scan(&item.ID, &item.Name, &item.Notes, &item.Description, &item.Stage, &item.Category)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func GetBoxByID(id int) (box Box, err error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return Box{}, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
row := db.QueryRow("SELECT * FROM boxes WHERE id = ?", id)
|
||||
err = row.Scan(&box.ID, &box.Name, &box.Notes, &box.Description, &box.Stage, &box.Category)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func PostItem(item Item) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `INSERT INTO items (name, notes, description, stage, category)
|
||||
VALUES (?, ?, ?, ?, ?)`
|
||||
|
||||
result, err := db.Exec(query, item.Name, item.Notes, item.Description, item.Stage, item.Category)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
func PostBox(box Box) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `INSERT INTO boxes (name, notes, description, stage, category) VALUES (?, ?, ?, ?, ?)`
|
||||
|
||||
result, err := db.Exec(query, box.Name, box.Notes, box.Description, box.Stage, box.Category)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
func PutItem(item Item) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `UPDATE items SET name = ?, notes = ?, description = ?, stage = ?, category = ? WHERE id = ?`
|
||||
|
||||
result, err := db.Exec(query, item.Name, item.Notes, item.Description, item.Stage, item.Category, item.ID)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
func PutBox(box Box) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `UPDATE boxes SET name = ?, notes = ?, description = ?, stage = ?, category = ? WHERE id = ?`
|
||||
|
||||
result, err := db.Exec(query, box.Name, box.Notes, box.Description, box.Stage, box.Category, box.ID)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
func DeleteItem(id int) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `DELETE FROM items WHERE id = ?`
|
||||
|
||||
result, err := db.Exec(query, id)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
func DeleteBox(id int) (int64, error) {
|
||||
db, err := CreateClient()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
query := `DELETE FROM boxes WHERE id = ?`
|
||||
|
||||
result, err := db.Exec(query, id)
|
||||
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
|
||||
// func PostBoxItem(itemid int, boxid int) (int64, error) {
|
||||
// db, err := CreateClient()
|
||||
// if err != nil {
|
||||
|
||||
5
main.go
5
main.go
@@ -51,13 +51,16 @@ func main() {
|
||||
router.Handle("/items/save", web.Action(itemActions.Save))
|
||||
router.Handle("/items/edit/", web.Action(itemActions.Edit))
|
||||
router.Handle("/items/delete/", web.Action(itemActions.Delete))
|
||||
router.Handle("/items/delete/:id", web.Action(itemActions.Delete))
|
||||
router.Handle("/items/save/", web.Action(itemActions.Save))
|
||||
router.Handle("/items/save/:id", web.Action(itemActions.Save))
|
||||
|
||||
router.Handle("/items", web.Action(itemActions.Get))
|
||||
router.Handle("/items/:id", web.Action(itemActions.Get))
|
||||
router.Handle("/boxes", web.Action(boxActions.GetAll))
|
||||
router.Handle("/items/", web.Action(itemActions.Get))
|
||||
router.Handle("/items/:id", web.Action(itemActions.Get))
|
||||
router.Handle("/boxes/", web.Action(boxActions.GetAll))
|
||||
router.Handle("/boxes/:id", web.Action(boxActions.GetAll))
|
||||
|
||||
router.Handle("/", web.Action(routes.HomePage))
|
||||
router.Handle("/index.html", web.Action(routes.HomePage))
|
||||
|
||||
@@ -25,7 +25,7 @@ func Items(_html *template.Template) *ItemActions {
|
||||
GetAll: GetAllItems,
|
||||
Edit: EditItem,
|
||||
Delete: nil,
|
||||
Save: nil,
|
||||
Save: Put,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,3 +99,82 @@ func GetItemByID(r *http.Request) *web.Response {
|
||||
nil,
|
||||
)
|
||||
}
|
||||
|
||||
func Put(r *http.Request) *web.Response {
|
||||
id, _ := web.PathLast(r)
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return web.Error(http.StatusBadRequest, err, nil)
|
||||
}
|
||||
|
||||
name := r.Form.Get("name")
|
||||
stage := r.Form.Get("stage")
|
||||
category := r.Form.Get("category")
|
||||
description := r.Form.Get("description")
|
||||
notes := r.Form.Get("notes")
|
||||
// id := r.Form.Get("id")
|
||||
|
||||
item := db.Item{
|
||||
ID: func() int {
|
||||
idInt, err := strconv.ParseInt(id, 10, 64)
|
||||
|
||||
if err != nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
return int(idInt)
|
||||
}(),
|
||||
|
||||
Name: name,
|
||||
Description: &description,
|
||||
Notes: ¬es,
|
||||
|
||||
Stage: func() db.PackingStage {
|
||||
stageInt, _ := strconv.Atoi(stage)
|
||||
return db.PackingStage(stageInt)
|
||||
}(),
|
||||
|
||||
Category: func() db.Category {
|
||||
categoryInt, _ := strconv.Atoi(category)
|
||||
return db.Category(categoryInt)
|
||||
}(),
|
||||
}
|
||||
|
||||
_, err = db.PutItem(item)
|
||||
|
||||
if err != nil {
|
||||
return web.Error(http.StatusInternalServerError, err, nil)
|
||||
}
|
||||
|
||||
return web.HTML(
|
||||
http.StatusOK,
|
||||
html,
|
||||
"entity-row.html",
|
||||
item,
|
||||
nil,
|
||||
)
|
||||
}
|
||||
|
||||
func Delete(r *http.Request) *web.Response {
|
||||
idFromPath, _ := web.PathLast(r)
|
||||
id, err := strconv.ParseInt(idFromPath, 10, 64)
|
||||
|
||||
if err != nil {
|
||||
return web.Error(http.StatusBadRequest, err, nil)
|
||||
}
|
||||
|
||||
_, err = db.DeleteItem(int(id))
|
||||
|
||||
if err != nil {
|
||||
return web.Error(http.StatusInternalServerError, err, nil)
|
||||
}
|
||||
|
||||
return web.HTML(
|
||||
http.StatusOK,
|
||||
html,
|
||||
"entity-row.html",
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
<tr id="datarow-{{.ID}}" class="border-b dark:border-neutral-500">
|
||||
<td class="whitespace-nowrap px-6 py-4">{{.ID}}</td>
|
||||
<tr id="datarow-{{.ID}}" class="datarow border-b dark:border-neutral-500">
|
||||
<td class="whitespace-nowrap px-6 py-4">
|
||||
<input hidden disabled type="text" name="id" value="{{.ID}}" data-include-edit="{{.ID}}" />
|
||||
<span>{{.ID}}</span>
|
||||
</td>
|
||||
<td class="whitespace-nowrap px-6 py-4">
|
||||
<input
|
||||
type="text"
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<td class="whitespace-nowrap px-6 py-4">{{.ID}}</td>
|
||||
<td class="whitespace-nowrap px-6 py-4">{{.Name}}</td>
|
||||
|
||||
<td class="whitespace-nowrap px-6 py-4">{{.Stage}}</td>
|
||||
{{ template "parsed-packing-stage.html" .Stage }}
|
||||
|
||||
{{ template "parsed-category.html" .Category }}
|
||||
|
||||
|
||||
23
templates/parsed-packing-stage.html
Normal file
23
templates/parsed-packing-stage.html
Normal file
@@ -0,0 +1,23 @@
|
||||
<td class="whitespace-nowrap px-6 py-4">
|
||||
{{ if eq . 0 }}
|
||||
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800 bg-">
|
||||
Essentials
|
||||
</span>
|
||||
{{ else if eq . 1 }}
|
||||
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-cyan-100 text-cyan-800">
|
||||
Stage One
|
||||
</span>
|
||||
{{ else if eq . 2 }}
|
||||
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-purple-100 text-purple-800">
|
||||
Stage Two
|
||||
</span>
|
||||
{{ else if eq . 3 }}
|
||||
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-stone-100 text-stone-800">
|
||||
Stage Three
|
||||
</span>
|
||||
{{ else }}
|
||||
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-amber-100 text-amber-800">
|
||||
Determine Later
|
||||
</span>
|
||||
{{ end }}
|
||||
</td>
|
||||
Reference in New Issue
Block a user