export functitionality

This commit is contained in:
schreifuchs 2025-02-19 14:59:12 +01:00
parent fccdcc176e
commit 85248e8c91
5 changed files with 59 additions and 30 deletions

37
app.go
View File

@ -2,10 +2,11 @@ package main
import ( import (
"context" "context"
"fmt" "encoding/csv"
"gegio-ue1/model" "gegio-ue1/model"
"os" "os"
"slices" "slices"
"strconv"
"github.com/wailsapp/wails/v2/pkg/runtime" "github.com/wailsapp/wails/v2/pkg/runtime"
"gorm.io/gorm" "gorm.io/gorm"
@ -92,9 +93,39 @@ func (a *App) ExportTournament(t model.Tournament) {
dirname, _ := os.UserHomeDir() dirname, _ := os.UserHomeDir()
str, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{DefaultDirectory: dirname, DefaultFilename: "tournament.csv"}) str, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{DefaultDirectory: dirname, DefaultFilename: "tournament.csv"})
if err != nil { if err != nil {
fmt.Println(err) panic(err)
}
file, err := os.Create(str)
if err != nil {
panic(err)
}
defer file.Close()
// Create a CSV writer
writer := csv.NewWriter(file)
defer writer.Flush()
var matches []model.Match
a.db.Where("tournament_id = ?", t.ID).Order("stage ASC").Order("`order` ASC").Preload("Participant1").Preload("Participant2").Preload("WinnerParticipant").Find(&matches)
// Write header
header := []string{"Stage", "Order", "Winner", "Looser"}
if err := writer.Write(header); err != nil {
panic(err)
}
for _, m := range matches {
record := []string{strconv.Itoa(m.Stage), strconv.Itoa(m.Order), m.WinnerParticipant.Name}
if m.WinnerParticipantID == m.Participant1ID {
record = append(record, m.Participant2.Name)
} else {
record = append(record, m.Participant1.Name)
}
if err := writer.Write(record); err != nil {
panic(err)
}
} }
println(str)
} }

View File

@ -13,7 +13,7 @@
Modal, Modal,
} from "flowbite-svelte"; } from "flowbite-svelte";
import TourCreator from "./TourCreator.svelte"; import TourCreator from "./TourCreator.svelte";
import { Link } from "svelte-routing"; import { Link, navigate } from "svelte-routing";
let thingsList: model.Tournament[] = $state([]); let thingsList: model.Tournament[] = $state([]);
let newThing: boolean = $state(false); let newThing: boolean = $state(false);
@ -46,9 +46,7 @@
<TableBodyCell>{t.WinnerParticipant.Name}</TableBodyCell> <TableBodyCell>{t.WinnerParticipant.Name}</TableBodyCell>
<TableBodyCell> <TableBodyCell>
<Link class="text-primary-500 underline" to={`/tournament/${t.ID}`} <Button onclick={() => navigate(`/tournament/${t.ID}`)}>View</Button>
>Edit</Link
>
</TableBodyCell> </TableBodyCell>
</TableBodyRow> </TableBodyRow>
{/each} {/each}

View File

@ -28,7 +28,7 @@
function saveMatch(m: model.Match): Promise<void> { function saveMatch(m: model.Match): Promise<void> {
if (matches[matches.length - 1].length == 1) { if (matches[matches.length - 1].length == 1) {
tournament.WinnerParticipantID = m.WinnerParticipantID; tournament.WinnerParticipant = m.WinnerParticipant;
SaveTournament(tournament).then(update); SaveTournament(tournament).then(update);
} }
return SaveMatch(m); return SaveMatch(m);
@ -71,32 +71,33 @@
</TableHead> </TableHead>
<TableBody> <TableBody>
{#each ms as m} {#each ms as m}
<TableBodyRow> <TableBodyRow class="h-20">
<TableBodyCell> <TableBodyCell>
{m.Participant1.Name} {m.Participant1.Name}
</TableBodyCell> </TableBodyCell>
<TableBodyCell> <TableBodyCell>
{m.Participant2.Name} {m.Participant2.Name}
</TableBodyCell> </TableBodyCell>
<TableBodyCell> <TableBodyCell class="w-96">
{#if m.WinnerParticipant.Name == ""} {#if m.WinnerParticipant.Name == ""}
<Button <div class="grid grid-cols-2 gap-5">
class="m-1" <Button
onclick={() => { onclick={() => {
m.WinnerParticipant = m.Participant1; m.WinnerParticipant = m.Participant1;
saveMatch(m).then(update); saveMatch(m).then(update);
}} }}
> >
{m.Participant1.Name} {m.Participant1.Name}
</Button> </Button>
<Button <Button
onclick={() => { onclick={() => {
m.WinnerParticipantID = m.Participant2ID; m.WinnerParticipant = m.Participant2;
saveMatch(m).then(update); saveMatch(m).then(update);
}} }}
> >
{m.Participant2.Name} {m.Participant2.Name}
</Button> </Button>
</div>
{:else} {:else}
{m.WinnerParticipant.Name} {m.WinnerParticipant.Name}
{/if} {/if}

View File

@ -67,7 +67,6 @@
<TableHeadCell>Name</TableHeadCell> <TableHeadCell>Name</TableHeadCell>
<TableHeadCell /> <TableHeadCell />
<TableHeadCell /> <TableHeadCell />
<TableHeadCell />
</TableHead> </TableHead>
<TableBody> <TableBody>
{#each participants as p} {#each participants as p}
@ -75,7 +74,7 @@
<TableBodyCell> <TableBodyCell>
{p.Name} {p.Name}
</TableBodyCell> </TableBodyCell>
<TableBodyCell> <TableBodyCell class="w-6">
{#if tournament.Participants.find((pa) => pa.ID == p.ID)} {#if tournament.Participants.find((pa) => pa.ID == p.ID)}
<Button <Button
onclick={() => { onclick={() => {
@ -92,7 +91,7 @@
> >
{/if} {/if}
</TableBodyCell> </TableBodyCell>
<TableBodyCell> <TableBodyCell class="w-6">
<Button onclick={() => DeleteParticipat(p).then(update)} <Button onclick={() => DeleteParticipat(p).then(update)}
>Delete</Button >Delete</Button
> >

Binary file not shown.