Fixed the delete button on the shares table of the expenses page
This commit is contained in:
+1
-1
@@ -11,7 +11,7 @@ used
|
||||
- [X] Reduce the size of the font file
|
||||
- [X] Add some migration tool
|
||||
- [X] Ownership on the image for the expenses webapp dir need to be changed
|
||||
- [ ] The button for deleting share rows isn't working at all unlike the payments
|
||||
- [X] The button for deleting share rows isn't working at all unlike the payments
|
||||
- [X] Use shopspring/decimal for money handling
|
||||
- [ ] Rename this file
|
||||
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
type="button"
|
||||
class="delete-button"
|
||||
hx-delete="/payments/{{ $payment.ExpPaymID }}"
|
||||
hx-on="htmx:afterRequest: this.closest('tr').remove()"
|
||||
hx-target="closest tr">X</button>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -58,7 +58,12 @@
|
||||
value="{{ $share.Share }}" class="tui-input">
|
||||
</td>
|
||||
<td class="row-delete-button">
|
||||
<button type="button" onclick="deleteShareRow(this)">X</button>
|
||||
<button
|
||||
type="button"
|
||||
class="delete-button"
|
||||
hx-delete="/shares/{{ $share.ExpShareID }}"
|
||||
hx-on="htmx:afterRequest: this.closest('tr').remove()"
|
||||
hx-target="closest tr">X</button>
|
||||
</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
|
||||
@@ -54,6 +54,7 @@ func setRoutes(router *gin.Engine) {
|
||||
pages.RouteStores(router)
|
||||
pages.RouteTypes(router)
|
||||
pages.RoutePayments(router)
|
||||
pages.RouteShares(router)
|
||||
pages.RouteOverview(router)
|
||||
pages.RouteNotFound(router)
|
||||
pages.RouteServerError(router)
|
||||
|
||||
@@ -17,3 +17,6 @@ VALUES(?, ?, ?);
|
||||
UPDATE expensesShares
|
||||
SET "UserID" = ?, "Share" = ?
|
||||
WHERE "ExpShareID" = ?;
|
||||
|
||||
-- name: DeleteShare :execresult
|
||||
DELETE FROM "expensesShares" where "ExpShareID" = ?;
|
||||
|
||||
@@ -10,6 +10,14 @@ import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const deleteShare = `-- name: DeleteShare :execresult
|
||||
DELETE FROM "expensesShares" where "ExpShareID" = ?
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteShare(ctx context.Context, expshareid int64) (sql.Result, error) {
|
||||
return q.db.ExecContext(ctx, deleteShare, expshareid)
|
||||
}
|
||||
|
||||
const getShares = `-- name: GetShares :many
|
||||
SELECT
|
||||
shares.ExpShareID, shares.ExpID, shares.UserID, shares.Share, users.UserID, users.UserName, users.UserPass
|
||||
|
||||
@@ -45,3 +45,22 @@ func ParseFormShares(userIDs []string, shares []string, sharesIDs []string,
|
||||
|
||||
return shareList, nil
|
||||
}
|
||||
|
||||
func (s *Service) DeleteShare(id int64) error {
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
share := mod.ExpenseShare{
|
||||
ExpShareID: id,
|
||||
}
|
||||
|
||||
err = share.Delete(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
@@ -76,3 +76,22 @@ func (sh *ExpenseShare) Update(tx *sql.Tx) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sh *ExpenseShare) Delete(tx *sql.Tx) error {
|
||||
ctx := context.Background()
|
||||
|
||||
queries := repo.New(tx)
|
||||
res, err := queries.DeleteShare(ctx, sh.ExpShareID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
} else if rowsAffected == 0 {
|
||||
return fmt.Errorf("no rows were created")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
exp "expenses/expenses"
|
||||
experr "expenses/expenses/errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func deleteShare(c *gin.Context) {
|
||||
shareID, err := exp.ParseID(c.Param("id"))
|
||||
if err != nil {
|
||||
NotFoundView(c, "failed to fetch payment")
|
||||
return
|
||||
}
|
||||
|
||||
err = exp.Serv.DeleteShare(shareID)
|
||||
if err == experr.ErrNotFound {
|
||||
errMsg := fmt.Sprintf("share %d not found", shareID)
|
||||
c.String(http.StatusNotFound, errMsg)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
errMsg := fmt.Sprintf("error deleting share %d", shareID)
|
||||
c.String(http.StatusInternalServerError, errMsg)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func RouteShares(router *gin.Engine) {
|
||||
router.DELETE("/shares/:id", deleteShare)
|
||||
}
|
||||
Reference in New Issue
Block a user