Item sharing

This commit is contained in:
roberto 2025-01-06 17:07:18 +01:00
parent b4d3d1cb7e
commit bf34ee71b3
6 changed files with 49 additions and 25 deletions

View File

@ -2,8 +2,7 @@ CREATE TABLE "bm_item_shares" (
"id" INTEGER, "id" INTEGER,
"token" TEXT NOT NULL UNIQUE, "token" TEXT NOT NULL UNIQUE,
"item_id" INTEGER DEFAULT '', "item_id" INTEGER DEFAULT '',
"read" INTEGER DEFAULT '', "permission_edit" INTEGER DEFAULT '',
"update" INTEGER DEFAULT '',
"start_datetime" TEXT DEFAULT '', "start_datetime" TEXT DEFAULT '',
"end_datetime" TEXT DEFAULT '', "end_datetime" TEXT DEFAULT '',
"password" TEXT DEFAULT '', "password" TEXT DEFAULT '',

View File

@ -261,6 +261,11 @@ document.addEventListener("DOMContentLoaded", function(event){
document.getElementById("dialog").showModal(); document.getElementById("dialog").showModal();
}); });
document.body.addEventListener("closeModalDialog", function(evt){
document.getElementById('dialog').close();
document.getElementById('dialog').remove();
});
document.body.addEventListener("quickboxNoteClear", function(evt){ document.body.addEventListener("quickboxNoteClear", function(evt){
document.getElementById("quickbox-notetext").value = '' document.getElementById("quickbox-notetext").value = ''
}); });

View File

@ -11,13 +11,13 @@
<div class="col l6"> <div class="col l6">
<p style="margin-right: 6px"> <p style="margin-right: 6px">
<label for="share-start-datetime">Start date time</label> <label for="share-start-datetime">Start date time</label>
<input name="start-datetime" id="share-start-datetime" type="datetime-local" value=""/> <input name="Start_datetime" id="share-start-datetime" type="datetime-local" value=""/>
</p> </p>
</div> </div>
<div class="col l6"> <div class="col l6">
<p> <p>
<label for="share-end-datetime">End date time</label> <label for="share-end-datetime">End date time</label>
<input name="end-datetime" id="share-end-datetime" type="datetime-local" value=""/> <input name="End_datetime" id="share-end-datetime" type="datetime-local" value=""/>
</p> </p>
</div> </div>
</div> </div>
@ -27,7 +27,7 @@
<label for="share-token">Share Url</label> <label for="share-token">Share Url</label>
<div class="input-container"> <div class="input-container">
<span class="prefix">{{ .baseUrl }}/</span> <span class="prefix">{{ .baseUrl }}/</span>
<input name="token" id="share-token" type="url" readonly="readonly" value="{{ .shareToken }}" /> <input name="Token" id="share-token" type="url" readonly="readonly" value="{{ .shareToken }}" />
</div> </div>
</p> </p>
</div> </div>
@ -36,15 +36,14 @@
<div class="col l12"> <div class="col l12">
<p style="margin-right: 6px"> <p style="margin-right: 6px">
<label for="share-password">Password</label> <label for="share-password">Password</label>
<input name="password" id="share-password" type="password" value=""/> <input name="Password" id="share-password" type="password" value=""/>
</p> </p>
</div> </div>
</div> </div>
<fieldset> <fieldset>
<legend>Permissions</legend> <legend>Permissions</legend>
<div class="row"> <div class="row">
<div class="col l6">{{ widget_checkbox "read" "Read" "1" "" }}</div> <div class="col l6">{{ widget_checkbox "Permission_edit" "Edit" "1" "" }}</div>
<div class="col l6">{{ widget_checkbox "edit" "Edit" "1" "" }}</div>
</div> </div>
</fieldset> </fieldset>
<div id="footer-navbar"> <div id="footer-navbar">

4
before-compile.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
PATH="/home/roberto/.nvm/versions/node/v20.18.0/bin:$PATH"
DIR=$(dirname "$0")
handlebars "$DIR"/assets/jstemplates/items/ -f "$DIR"/assets/static/js/templates.js

View File

@ -35,15 +35,14 @@ type itemForm struct {
} }
type itemShareForm struct { type itemShareForm struct {
Id int64 `form:"Id"` Id int64 `form:"Id"`
Item_id int64 `form:"Item_id"` ItemId int64 `form:"Item_id"`
Token string `form:"Token"` Token string `form:"Token"`
Start_datetime string `form:"start_datetime"` StartDatetime string `form:"Start_datetime"`
End_datetime string `form:"end_datetime"` EndDatetime string `form:"End_datetime"`
Password string `form:"password"` Password string `form:"Password"`
Permission_read int `form:"read"` PermissionEdit int `form:"Permission_edit"`
Permission_edit int `form:"edit"` Validator validator.Validator `form:"-"`
Validator validator.Validator `form:"-"`
} }
func (form *itemForm) Validate(w http.ResponseWriter, r *http.Request, app *application, data map[string]any) bool { func (form *itemForm) Validate(w http.ResponseWriter, r *http.Request, app *application, data map[string]any) bool {
@ -937,8 +936,12 @@ func (app *application) itemShare(w http.ResponseWriter, r *http.Request) {
} }
itemShare := &models.ItemShare{ itemShare := &models.ItemShare{
Item_id: item_id, ItemId: item_id,
Token: itemShareFromForm.Token, Token: itemShareFromForm.Token,
Password: itemShareFromForm.Password,
PermissionEdit: itemShareFromForm.PermissionEdit,
StartDatetime: itemShareFromForm.StartDatetime,
EndDatetime: itemShareFromForm.EndDatetime,
} }
itemshareModel := &models.ItemShareModel{DB: app.db} itemshareModel := &models.ItemShareModel{DB: app.db}
@ -947,6 +950,20 @@ func (app *application) itemShare(w http.ResponseWriter, r *http.Request) {
app.badRequest(w, err) app.badRequest(w, err)
return return
} }
var fullBuf = new(bytes.Buffer)
dataMessage := make(map[string]string)
dataMessage["messageType"] = "success"
dataMessage["messageContent"] = "Item shared successfully"
err = response.HXFragment(fullBuf, []string{"partials/message.tmpl"}, "message", dataMessage)
if err != nil {
app.serverError(w, r, err)
}
w.Header().Add("HX-Reswap", `beforeend`)
w.Header().Add("HX-Trigger-After-Settle", `{"showMessage": "", "closeModalDialog": ""}`)
fullBuf.WriteTo(w)
} }
} }

View File

@ -13,10 +13,10 @@ type ItemShareModel struct {
type ItemShare struct { type ItemShare struct {
Id int64 `db:"id"` Id int64 `db:"id"`
Token string `db:"token"` Token string `db:"token"`
Item_id int64 `db:"item_id"` ItemId int64 `db:"item_id"`
Update int `db:"hidden"` PermissionEdit int `db:"permission_edit"`
Start_datetime string `db:"start_datetime"` StartDatetime string `db:"start_datetime"`
End_datetime string `db:"end_datetime"` EndDatetime string `db:"end_datetime"`
Password string `db:"password"` Password string `db:"password"`
} }
@ -24,7 +24,7 @@ func (model *ItemShareModel) Create(ItemShare *ItemShare) (int64, error) {
ctx, cancel := database.GetContext() ctx, cancel := database.GetContext()
defer cancel() defer cancel()
query := `INSERT INTO bm_items_share (token, item_id, update, start_datetime, end_datetime, password) VALUES (:token, :item_id, :update, :start_datetime, :end_datetime, :password)` query := `INSERT INTO bm_item_shares (token, item_id, permission_edit, start_datetime, end_datetime, password) VALUES (:token, :item_id, :permission_edit, :start_datetime, :end_datetime, :password)`
result, err := model.DB.NamedExecContext(ctx, query, ItemShare) result, err := model.DB.NamedExecContext(ctx, query, ItemShare)
if err != nil { if err != nil {
@ -43,7 +43,7 @@ func (model *ItemShareModel) Delete(id int) (bool, error) {
ctx, cancel := database.GetContext() ctx, cancel := database.GetContext()
defer cancel() defer cancel()
_, err := model.DB.ExecContext(ctx, `DELETE FROM bm_item_share WHERE id = $1`, id) _, err := model.DB.ExecContext(ctx, `DELETE FROM bm_item_shares WHERE id = $1`, id)
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
return false, nil return false, nil
} }