mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2024-12-23 03:59:25 +00:00
Adding prober delete action
This commit is contained in:
parent
32ef14d1d5
commit
09490a8250
5 changed files with 76 additions and 2 deletions
|
@ -1,8 +1,9 @@
|
|||
<script>
|
||||
import { DataHandler } from '@vincjo/datatables/remote';
|
||||
import { format, formatDistance } from 'date-fns';
|
||||
import { loadData } from './api-handler';
|
||||
import { loadData, deleteData } from './api-handler';
|
||||
import { onMount, onDestroy } from 'svelte';
|
||||
import { getModalStore, getToastStore } from '@skeletonlabs/skeleton';
|
||||
import {
|
||||
DtSrRowsPerPage,
|
||||
DtSrThSort,
|
||||
|
@ -10,6 +11,37 @@
|
|||
DtSrRowCount,
|
||||
DtSrPagination
|
||||
} from '$lib/components/datatables/server';
|
||||
const modalStore = getModalStore();
|
||||
const toastStore = getToastStore();
|
||||
|
||||
/** @param {number} id */
|
||||
const handleDelete = (id) => {
|
||||
modalStore.trigger({
|
||||
type: 'confirm',
|
||||
title: 'Please Confirm',
|
||||
body: 'Are you sure you wish to proceed?',
|
||||
/** @param {boolean} r */
|
||||
response: async (r) => {
|
||||
if (r) {
|
||||
deleteData(id)
|
||||
.then((res) => {
|
||||
if (res.status !== 'ok') {
|
||||
toastStore.trigger({ message: 'Failed to delete prober' });
|
||||
} else {
|
||||
toastStore.trigger({
|
||||
message: 'Prober deleted',
|
||||
background: 'variant-filled-success'
|
||||
});
|
||||
handler.invalidate();
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
toastStore.trigger({ message: 'Prober could not be deleted' });
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const handler = new DataHandler([], { rowsPerPage: 10, totalRows: 0 });
|
||||
let rows = handler.getRows();
|
||||
|
@ -108,7 +140,16 @@
|
|||
<tbody>
|
||||
{#each $rows as row (row.id)}
|
||||
<tr>
|
||||
<td>{row.id}</td>
|
||||
<td>
|
||||
{row.id}
|
||||
<button
|
||||
class="variant-filled-error btn btn-sm mr-1"
|
||||
name="delete_{row.id}"
|
||||
on:click={() => {
|
||||
handleDelete(row.id);
|
||||
}}>Delete</button
|
||||
>
|
||||
</td>
|
||||
<td>{row.name}</td>
|
||||
<td>{row.api_key}</td>
|
||||
<td>
|
||||
|
|
|
@ -8,6 +8,15 @@ export const loadData = async (state) => {
|
|||
return json.data.items ?? [];
|
||||
};
|
||||
|
||||
export const deleteData = async (id) => {
|
||||
const response = await fetch(apiUri(`/api/v1/prober/${id}`), {
|
||||
method: 'DELETE',
|
||||
credentials: 'include'
|
||||
});
|
||||
const json = await response.json();
|
||||
return json;
|
||||
};
|
||||
|
||||
const getParams = ({ pageNumber, rowsPerPage, sort, filters }) => {
|
||||
let params = `page=${pageNumber}&limit=${rowsPerPage}`;
|
||||
|
||||
|
|
|
@ -88,6 +88,22 @@ func Prober(c *fiber.Ctx) error {
|
|||
"data": nil,
|
||||
})
|
||||
}
|
||||
} else if c.Method() == "DELETE" {
|
||||
id, _ := strconv.Atoi(c.Params("id"))
|
||||
err := proberRepo.Delete(id)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"status": "error",
|
||||
"message": err.Error(),
|
||||
"data": nil,
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"status": "ok",
|
||||
"message": "Success",
|
||||
"data": nil,
|
||||
})
|
||||
}
|
||||
|
||||
query := repo.ProbersQueryParams{
|
||||
|
|
|
@ -14,6 +14,7 @@ func V1Api(app *fiber.App) {
|
|||
|
||||
v1.Get("/prober", Prober)
|
||||
v1.Post("/prober", Prober)
|
||||
v1.Delete("/prober/:id", CookieProtected, Prober)
|
||||
v1.Get("/nodes", MoneroNodes)
|
||||
v1.Post("/nodes", AddNode)
|
||||
v1.Get("/nodes/id/:id", MoneroNode)
|
||||
|
|
|
@ -14,6 +14,7 @@ type ProberRepository interface {
|
|||
AddProber(name string) error
|
||||
Probers(q ProbersQueryParams) (Probers, error)
|
||||
CheckApi(key string) (Prober, error)
|
||||
Delete(id int) error
|
||||
}
|
||||
|
||||
type ProberRepo struct {
|
||||
|
@ -124,3 +125,9 @@ func (repo *ProberRepo) CheckApi(key string) (Prober, error) {
|
|||
err := repo.db.QueryRow(query, key).Scan(&prober.Id, &prober.Name, &prober.ApiKey, &prober.LastSubmitTs)
|
||||
return prober, err
|
||||
}
|
||||
|
||||
func (repo *ProberRepo) Delete(id int) error {
|
||||
query := `DELETE FROM tbl_prober WHERE id = ?`
|
||||
_, err := repo.db.Exec(query, id)
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue