Create and delete card

This commit is contained in:
Brieuc Dubois 2023-12-30 02:13:30 +01:00
parent 888ef75dd8
commit daee67d6c3
4 changed files with 77 additions and 10 deletions

View File

@ -12,15 +12,15 @@ import (
func CreateCard(c *fiber.Ctx) error { func CreateCard(c *fiber.Ctx) error {
card := types.Card{} card := types.Card{}
if err := c.BodyParser(&card); err != nil { if err := c.BodyParser(&card); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse request"}) return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"status": "error", "error": "Cannot parse request", "trace": fmt.Sprint(err)})
} }
id, err := db.CreateCard(card) id, err := db.CreateCard(card)
if err != nil { if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot create card", "trace": fmt.Sprint(err)}) return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"status": "error", "error": "Cannot create card", "trace": fmt.Sprint(err)})
} }
return c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": id}) return c.Status(fiber.StatusCreated).JSON(fiber.Map{"status": "ok", "id": id})
} }
func GetAllCardsOf(c *fiber.Ctx) error { func GetAllCardsOf(c *fiber.Ctx) error {
@ -57,12 +57,12 @@ func GetCard(c *fiber.Ctx) error {
func DeleteCard(c *fiber.Ctx) error { func DeleteCard(c *fiber.Ctx) error {
id, err := strconv.Atoi(c.Params("id")) id, err := strconv.Atoi(c.Params("id"))
if err != nil { if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid card ID"}) return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"status": "error", "error": "Invalid card ID", "trace": fmt.Sprint(err)})
} }
err = db.DeleteCard(id) err = db.DeleteCard(id)
if err != nil { if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot delete card"}) return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"status": "error", "error": "Cannot delete card", "trace": fmt.Sprint(err)})
} }
return c.SendStatus(fiber.StatusNoContent) return c.SendStatus(fiber.StatusNoContent)

View File

@ -12,7 +12,8 @@
tags: [] tags: []
}; };
let showModal = false; export let showModal = false;
export let onDelete: () => void;
function editCard() { function editCard() {
showModal = true; showModal = true;
@ -49,4 +50,4 @@
{/if} {/if}
</div> </div>
<ModalCard bind:show={showModal} bind:card onCancel={cancelEdit} /> <ModalCard bind:show={showModal} bind:card onCancel={cancelEdit} {onDelete} />

View File

@ -7,6 +7,7 @@
export let show: boolean; export let show: boolean;
export let card: Card; export let card: Card;
export let onCancel: () => void; export let onCancel: () => void;
export let onDelete: () => void;
let tempCard: Card = { ...card }; let tempCard: Card = { ...card };
@ -32,6 +33,26 @@
<div class="header"> <div class="header">
<input class="title" bind:value={tempCard.title} on:blur={() => save(false)} /> <input class="title" bind:value={tempCard.title} on:blur={() => save(false)} />
<div class="buttons"> <div class="buttons">
<button on:click={onDelete}>
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="white"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<path d="M3 6h18"></path>
<path
d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m2-2h10a2 2 0 0 1 2 2v2H5V6a2 2 0 0 1 2-2z"
></path>
<line x1="10" y1="11" x2="10" y2="17"></line>
<line x1="14" y1="11" x2="14" y2="17"></line>
</svg>
</button>
<button on:click={onCancel}> <button on:click={onCancel}>
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"

View File

@ -23,6 +23,45 @@
console.error(response.data); console.error(response.data);
} }
}); });
let modalID = -1;
async function newCard() {
const response = await axios.post(`${backend}/api/card`, {
project_id: projectId,
title: 'Untitled',
content: ''
});
if (response.data.status !== 'ok') {
console.error(response.data);
return;
}
const id: number = response.data.id;
let card: Card = {
id: id,
project_id: projectId,
title: 'Untitled',
content: '',
tags: []
};
cards = [...cards, card];
modalID = id;
}
async function deleteCard(cardID: number) {
const response = await axios.delete(`${backend}/api/card/${cardID}`);
if (response.status !== 204) {
console.error(response.data);
return;
}
cards = cards.filter((card) => card.id !== cardID);
}
</script> </script>
<svelte:head> <svelte:head>
@ -33,12 +72,18 @@
{#if project} {#if project}
<div id="project"> <div id="project">
<header>
<h2>{project.title}</h2> <h2>{project.title}</h2>
<button on:click={newCard}>New card</button>
</header>
<ul> <ul>
{#if cards} {#if cards}
{#each cards as card} {#each cards as card}
<CardC {card} /> <CardC
{card}
showModal={modalID === card.id}
onDelete={async () => await deleteCard(card.id)}
/>
{/each} {/each}
{/if} {/if}
</ul> </ul>