Adding prober delete action

This commit is contained in:
ditatompel 2024-05-07 22:37:36 +07:00
parent 32ef14d1d5
commit 09490a8250
No known key found for this signature in database
GPG key ID: 31D3D06D77950979
5 changed files with 76 additions and 2 deletions

View file

@ -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>

View file

@ -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}`;

View file

@ -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{

View file

@ -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)

View file

@ -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
}