generated from schreifuchs/wails-template
export functitionality
This commit is contained in:
parent
fccdcc176e
commit
85248e8c91
37
app.go
37
app.go
@ -2,10 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"encoding/csv"
|
||||
"gegio-ue1/model"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
"gorm.io/gorm"
|
||||
@ -92,9 +93,39 @@ func (a *App) ExportTournament(t model.Tournament) {
|
||||
dirname, _ := os.UserHomeDir()
|
||||
str, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{DefaultDirectory: dirname, DefaultFilename: "tournament.csv"})
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@
|
||||
Modal,
|
||||
} from "flowbite-svelte";
|
||||
import TourCreator from "./TourCreator.svelte";
|
||||
import { Link } from "svelte-routing";
|
||||
import { Link, navigate } from "svelte-routing";
|
||||
|
||||
let thingsList: model.Tournament[] = $state([]);
|
||||
let newThing: boolean = $state(false);
|
||||
@ -46,9 +46,7 @@
|
||||
<TableBodyCell>{t.WinnerParticipant.Name}</TableBodyCell>
|
||||
|
||||
<TableBodyCell>
|
||||
<Link class="text-primary-500 underline" to={`/tournament/${t.ID}`}
|
||||
>Edit</Link
|
||||
>
|
||||
<Button onclick={() => navigate(`/tournament/${t.ID}`)}>View</Button>
|
||||
</TableBodyCell>
|
||||
</TableBodyRow>
|
||||
{/each}
|
||||
|
@ -28,7 +28,7 @@
|
||||
|
||||
function saveMatch(m: model.Match): Promise<void> {
|
||||
if (matches[matches.length - 1].length == 1) {
|
||||
tournament.WinnerParticipantID = m.WinnerParticipantID;
|
||||
tournament.WinnerParticipant = m.WinnerParticipant;
|
||||
SaveTournament(tournament).then(update);
|
||||
}
|
||||
return SaveMatch(m);
|
||||
@ -71,17 +71,17 @@
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{#each ms as m}
|
||||
<TableBodyRow>
|
||||
<TableBodyRow class="h-20">
|
||||
<TableBodyCell>
|
||||
{m.Participant1.Name}
|
||||
</TableBodyCell>
|
||||
<TableBodyCell>
|
||||
{m.Participant2.Name}
|
||||
</TableBodyCell>
|
||||
<TableBodyCell>
|
||||
<TableBodyCell class="w-96">
|
||||
{#if m.WinnerParticipant.Name == ""}
|
||||
<div class="grid grid-cols-2 gap-5">
|
||||
<Button
|
||||
class="m-1"
|
||||
onclick={() => {
|
||||
m.WinnerParticipant = m.Participant1;
|
||||
saveMatch(m).then(update);
|
||||
@ -91,12 +91,13 @@
|
||||
</Button>
|
||||
<Button
|
||||
onclick={() => {
|
||||
m.WinnerParticipantID = m.Participant2ID;
|
||||
m.WinnerParticipant = m.Participant2;
|
||||
saveMatch(m).then(update);
|
||||
}}
|
||||
>
|
||||
{m.Participant2.Name}
|
||||
</Button>
|
||||
</div>
|
||||
{:else}
|
||||
{m.WinnerParticipant.Name}
|
||||
{/if}
|
||||
|
@ -67,7 +67,6 @@
|
||||
<TableHeadCell>Name</TableHeadCell>
|
||||
<TableHeadCell />
|
||||
<TableHeadCell />
|
||||
<TableHeadCell />
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{#each participants as p}
|
||||
@ -75,7 +74,7 @@
|
||||
<TableBodyCell>
|
||||
{p.Name}
|
||||
</TableBodyCell>
|
||||
<TableBodyCell>
|
||||
<TableBodyCell class="w-6">
|
||||
{#if tournament.Participants.find((pa) => pa.ID == p.ID)}
|
||||
<Button
|
||||
onclick={() => {
|
||||
@ -92,7 +91,7 @@
|
||||
>
|
||||
{/if}
|
||||
</TableBodyCell>
|
||||
<TableBodyCell>
|
||||
<TableBodyCell class="w-6">
|
||||
<Button onclick={() => DeleteParticipat(p).then(update)}
|
||||
>Delete</Button
|
||||
>
|
||||
|
BIN
tournament.db
BIN
tournament.db
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user