adds delete endpoint for the dashboard bookmark widget

This commit is contained in:
SimonGschnell
2024-07-30 15:27:24 +02:00
parent 284cf711fd
commit 39574c1b3a
7 changed files with 300 additions and 14 deletions
@@ -0,0 +1,99 @@
<?php
/**
* Copyright (C) 2024 fhcomplete.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
if (! defined('BASEPATH')) exit('No direct script access allowed');
class Bookmark extends FHCAPI_Controller
{
/**
* Object initialization
*/
public function __construct()
{
parent::__construct([
'getBookmarks' => self::PERM_LOGGED,
'delete' => self::PERM_LOGGED,
]);
$this->load->model('dashboard/Bookmark_model', 'BookmarkModel');
$this->uid = getAuthUID();
$this->pid = getAuthPersonID();
}
//------------------------------------------------------------------------------------------------------------------
// Public methods
/**
* gets the bookmarks associated to a user
* @access public
* @return void
*/
public function getBookmarks()
{
$bookmarks = $this->BookmarkModel->getAll($this->uid);
if(isError($bookmarks)){
$this->terminateWithError(getError($bookmarks));
}
$bookmarks = $this->getDataOrTerminateWithError($bookmarks);
$this->terminateWithSuccess($bookmarks);
}
/**
* deletes bookmark from associated user
* @access public
* @return void
*/
public function delete($bookmark_id)
{
if(!isset($bookmark_id)) $this->terminateWithError("missing required parameters");
$bookmark = $this->BookmarkModel->get($bookmark_id);
if(isError($bookmark)){
$this->terminateWithError(getError($bookmark));
}
$bookmark = current($this->getDataOrTerminateWithError($bookmark));
// only delete bookmark if the user is the owner of the bookmark
$this->load->library('PermissionLib');
if($bookmark->uid == $this->uid || $this->permissionlib->isBerechtigt('admin')){
$delete_result = $this->BookmarkModel->delete($bookmark_id);
if(isError($delete_result)){
$this->terminateWithError(getError($delete_result));
}
$delete_result = $this->getDataOrTerminateWithError($delete_result);
$this->terminateWithSuccess($delete_result);
}else{
$this->terminateWithError("You are not authorized to delete this bookmark");
}
}
}
@@ -0,0 +1,57 @@
<?php
class Bookmark_model extends DB_Model
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->dbTable = 'dashboard.tbl_bookmark';
$this->pk = 'bookmark_id';
}
/**
* Get Bookmarks of UID.
* @param string user uid
* @return array
*/
public function getAll($uid)
{
return $this->execReadOnlyQuery("
SELECT *
FROM dashboard.tbl_bookmark
WHERE uid = ?
",[$uid]);
}
/**
* Gets Bookmark by bookmark_id.
* @param int $bookmark_id
* @return array
*/
public function get($bookmark_id)
{
return $this->execReadOnlyQuery("
SELECT *
FROM dashboard.tbl_bookmark
WHERE bookmark_id = ?
",[$bookmark_id]);
}
/**
* Gets Bookmark by bookmark_id.
* @param int $bookmark_id
* @return array
*/
public function delete($bookmark_id)
{
return $this->execReadOnlyQuery("
DELETE
FROM dashboard.tbl_bookmark
WHERE bookmark_id = ?
",[$bookmark_id]);
}
}
+21
View File
@@ -0,0 +1,21 @@
export default {
getBookmarks: function (uid) {
return this.$fhcApi.get(
FHC_JS_DATA_STORAGE_OBJECT.app_root +
FHC_JS_DATA_STORAGE_OBJECT.ci_router +
`/api/frontend/v1/Bookmark/getBookmarks`
,{}
);
},
deleteBookmark: function (bookmark_id) {
return this.$fhcApi.get(
FHC_JS_DATA_STORAGE_OBJECT.app_root +
FHC_JS_DATA_STORAGE_OBJECT.ci_router +
`/api/frontend/v1/Bookmark/delete/${bookmark_id}`
,{}
);
},
}
+2
View File
@@ -22,6 +22,7 @@ import filter from "./filter.js";
import studstatus from "./studstatus.js";
import profil from "./profil.js";
import profilUpdate from "./profilUpdate.js";
import bookmark from "./bookmark.js";
export default {
search,
@@ -31,4 +32,5 @@ export default {
studstatus,
profil,
profilUpdate,
bookmark,
};
+3
View File
@@ -1,4 +1,6 @@
import FhcDashboard from '../../components/Dashboard/Dashboard.js';
import Phrasen from "../../plugin/Phrasen.js"
import FhcAlert from "../../plugin/FhcAlert.js"
const app = Vue.createApp({
data: () => ({
@@ -9,4 +11,5 @@ const app = Vue.createApp({
}
});
app.config.unwrapInjectedRef = true;
app.use(Phrasen).use(FhcAlert);
app.mount('#content');
+54 -13
View File
@@ -3,7 +3,7 @@ import AbstractWidget from './Abstract';
export default {
name: 'WidgetsUrl',
data: () => ({
links: []
links: null
}),
mixins: [
AbstractWidget
@@ -14,6 +14,14 @@ export default {
}
},
methods: {
async fetchBookmarks(){
await this.$fhcApi.factory.bookmark.getBookmarks()
.then(res => res.data)
.then(result => {
this.links = result;
})
.catch();
},
addLink(){
let linkId = this.links.length;
@@ -24,37 +32,70 @@ export default {
url: this.url
})
},
removeLink(linkId){
let indexToRemove = this.links.findIndex((obj => obj.id === linkId));
this.links.splice(indexToRemove, 1);
removeLink(bookmark_id){
this.$fhcApi.factory.bookmark.deleteBookmark(bookmark_id)
.then(res => res.data)
.then(result => {
this.$fhcAlert.alertInfo(this.$p.t('bookmark','bookmarkDeleted'));
})
.catch();
}
},
created() {
this.links = TEST_LINKS;
//this.links = TEST_LINKS;
// this.links = TEST_KEINE_LINKS;
},
template: `
async mounted(){
await this.fetchBookmarks();
},
template: /*html*/`
<div class="widgets-url w-100 h-100">
<div v-if="configMode">
<div class="mb-3">
<header><b>Neuer Link</b></header><br>
<header><b>{{$p.t('bookmark','newLink')}}</b></header><br>
<div>
<input class="form-control form-control-sm" placeholder="Titel" type="text" v-model="title" name="title" maxlength="50" required>
<input class="form-control form-control-sm mt-2" type="url" placeholder="URL" v-model="url" name="url" required>
<button class="btn btn-outline-secondary btn-sm w-100 mt-2" @click="addLink()" type="button">Link speichern</button>
<button class="btn btn-outline-secondary btn-sm w-100 mt-2" @click="addLink()" type="button">{{$p.t('bookmark','saveLink')}}</button>
</div>
</div>
</div>
<div class="d-flex flex-column justify-content-between">
<header><b>{{ widgetTag }}</b></header>
<div v-for="link in links" :key="link.id" class="d-flex mt-2">
<a target="_blank" :href="link.url"><i class="fa fa-solid fa-arrow-up-right-from-square"></i> {{ link.title }}</a>
<a class="ms-auto" href="#" @click.prevent="removeLink(link.id)" v-show="configMode"><i class="fa fa-regular fa-trash-can"></i></a>
</div>
<!-- todo: widgetTag ?? -->
<template v-if="links">
<header><b>{{ widgetTag }}</b></header>
<div v-for="link in links" :key="link.id" class="d-flex mt-2">
<a target="_blank" :href="link.url"><i class="fa fa-solid fa-arrow-up-right-from-square"></i> {{ link.title }}</a>
<a class="ms-auto" href="#" @click.prevent="removeLink(link.bookmark_id)" v-show="configMode"><i class="fa fa-regular fa-trash-can"></i></a>
</div>
</template>
<template v-else>
<p v-for="i in 4" class="placeholder-glow">
<span class="placeholder" :class="{'col-9' : true}"></span>
</p>
</template>
</div>
</div>`
}
/*
Link JSON structure:
{
"bookmark_id": number,
"uid": string,
"url": string,
"title": string,
"tag": string,
"insertamum": "2024-07-30 14:33:03.699318",
"insertvon": null,
"updateamum": null,
"updatevon": null
}
*/
const TEST_KEINE_LINKS = [];
const TEST_LINKS = [
{
+64 -1
View File
@@ -27489,7 +27489,70 @@ array(
'insertvon' => 'system'
)
)
)
),
// BOOKMARK PHRASEN ----------------------------------------------------------------------
array(
'app' => 'core',
'category' => 'bookmark',
'phrase' => 'newLink',
'insertvon' => 'system',
'phrases' => array(
array(
'sprache' => 'German',
'text' => 'Neuer Link',
'description' => '',
'insertvon' => 'system'
),
array(
'sprache' => 'English',
'text' => 'New Link',
'description' => '',
'insertvon' => 'system'
)
)
),
array(
'app' => 'core',
'category' => 'bookmark',
'phrase' => 'saveLink',
'insertvon' => 'system',
'phrases' => array(
array(
'sprache' => 'German',
'text' => 'Link speichern',
'description' => '',
'insertvon' => 'system'
),
array(
'sprache' => 'English',
'text' => 'Save link',
'description' => '',
'insertvon' => 'system'
)
)
),
array(
'app' => 'core',
'category' => 'bookmark',
'phrase' => 'bookmarkDeleted',
'insertvon' => 'system',
'phrases' => array(
array(
'sprache' => 'German',
'text' => 'Link gelöscht',
'description' => '',
'insertvon' => 'system'
),
array(
'sprache' => 'English',
'text' => 'Link deleted',
'description' => '',
'insertvon' => 'system'
)
)
),
);