Compare commits

..

No commits in common. "2b13e5c03a231f99a2a4bc83a7a74b7401f14191" and "42e8605945775212bd5434520043cecdd210b3cf" have entirely different histories.

9 changed files with 94 additions and 48 deletions

View File

@ -1,15 +1,11 @@
<script lang="ts"> <script lang="ts">
import "./app.css"; import "./app.css";
import { Router, Route, Link, navigate } from "svelte-routing"; import { Router, Route, Link, navigate } from "svelte-routing";
import Things from "./routes/Things.svelte"; import Home from "./routes/Home.svelte";
import "./app.css"; import "./app.css";
import { Navbar, DarkMode } from "flowbite-svelte"; import { Navbar, DarkMode, Heading } from "flowbite-svelte";
import { HomeOutline } from "flowbite-svelte-icons"; import { HomeOutline } from "flowbite-svelte-icons";
import Thing from "./routes/Thing.svelte";
let url: string = $state("/"); let url: string = $state("/");
$effect(() => {
console.log(url);
});
</script> </script>
<div <div
@ -27,10 +23,7 @@
<DarkMode /> <DarkMode />
</Navbar> </Navbar>
<main class="size-full max-h-full max-w-full overflow-scroll"> <main class="size-full max-h-full max-w-full overflow-scroll">
<Route path="/"><Things /></Route> <Route path="/"><Home /></Route>
<Route path="/things/:id" let:params>
<Thing thingID={parseInt(params.id)} />
</Route>
</main> </main>
</Router> </Router>
</div> </div>

View File

@ -17,7 +17,6 @@
TableBodyRow, TableBodyRow,
TableBodyCell, TableBodyCell,
} from "flowbite-svelte"; } from "flowbite-svelte";
import { navigate } from "svelte-routing";
let name: string = $state(); let name: string = $state();
let thingsList: model.Thing[] = $state([]); let thingsList: model.Thing[] = $state([]);
@ -34,6 +33,10 @@
name = ""; name = "";
} }
function deleteEvent(id: number) {
DeleteThing(id).then(update);
}
onMount(update); onMount(update);
</script> </script>
@ -50,7 +53,6 @@
<TableHead> <TableHead>
<TableHeadCell>ID</TableHeadCell> <TableHeadCell>ID</TableHeadCell>
<TableHeadCell>Name</TableHeadCell> <TableHeadCell>Name</TableHeadCell>
<TableHeadCell>View</TableHeadCell>
<TableHeadCell>Delete</TableHeadCell> <TableHeadCell>Delete</TableHeadCell>
</TableHead> </TableHead>
<TableBody> <TableBody>
@ -63,13 +65,8 @@
<TableBodyCell> <TableBodyCell>
{t.Name} {t.Name}
</TableBodyCell> </TableBodyCell>
<TableBodyCell> <TableBodyCell>
<Button onclick={() => navigate(`/things/${t.ID}`)}>View</Button> <Button on:click={(_) => deleteEvent(t.ID)}>Delete</Button>
</TableBodyCell>
<TableBodyCell>
<Button onclick={() => DeleteThing(t.ID).then(update)}>Delete</Button>
</TableBodyCell> </TableBodyCell>
</TableBodyRow> </TableBodyRow>
{/each} {/each}

View File

@ -1,26 +0,0 @@
<script lang="ts">
import { GetThings } from "../../wailsjs/go/things/Service";
import { model } from "../../wailsjs/go/models";
import { onMount } from "svelte";
import { Heading } from "flowbite-svelte";
let { thingID }: { thingID: number } = $props();
let thing: model.Thing = $state(new model.Thing());
function update() {
GetThings().then((ts) => {
ts.forEach((t) => {
if (t.ID === thingID) {
thing = t;
}
});
});
}
onMount(update);
</script>
<div class="m-5">
<Heading>
{thing.Name}
</Heading>
</div>

View File

@ -1,8 +1,25 @@
export namespace model { export namespace model {
export class SubThing {
ID: number;
ThingID: number;
Name: string;
static createFrom(source: any = {}) {
return new SubThing(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.ID = source["ID"];
this.ThingID = source["ThingID"];
this.Name = source["Name"];
}
}
export class Thing { export class Thing {
ID: number; ID: number;
Name: string; Name: string;
Subthings: SubThing[];
static createFrom(source: any = {}) { static createFrom(source: any = {}) {
return new Thing(source); return new Thing(source);
@ -12,6 +29,25 @@ export namespace model {
if ('string' === typeof source) source = JSON.parse(source); if ('string' === typeof source) source = JSON.parse(source);
this.ID = source["ID"]; this.ID = source["ID"];
this.Name = source["Name"]; this.Name = source["Name"];
this.Subthings = this.convertValues(source["Subthings"], SubThing);
}
convertValues(a: any, classs: any, asMap: boolean = false): any {
if (!a) {
return a;
}
if (a.slice && a.map) {
return (a as any[]).map(elem => this.convertValues(elem, classs));
} else if ("object" === typeof a) {
if (asMap) {
for (const key of Object.keys(a)) {
a[key] = new classs(a[key]);
}
return a;
}
return new classs(a);
}
return a;
} }
} }

View File

@ -2,8 +2,14 @@
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
import {model} from '../models'; import {model} from '../models';
export function AddSubThing(arg1:number,arg2:string):Promise<void>;
export function DeleteSubThing(arg1:number):Promise<void>;
export function DeleteThing(arg1:number):Promise<void>; export function DeleteThing(arg1:number):Promise<void>;
export function GetSubThings(arg1:number):Promise<Array<model.SubThing>>;
export function GetThings():Promise<Array<model.Thing>>; export function GetThings():Promise<Array<model.Thing>>;
export function NewThing(arg1:string):Promise<void>; export function NewThing(arg1:string):Promise<void>;

View File

@ -2,10 +2,22 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
export function AddSubThing(arg1, arg2) {
return window['go']['things']['Service']['AddSubThing'](arg1, arg2);
}
export function DeleteSubThing(arg1) {
return window['go']['things']['Service']['DeleteSubThing'](arg1);
}
export function DeleteThing(arg1) { export function DeleteThing(arg1) {
return window['go']['things']['Service']['DeleteThing'](arg1); return window['go']['things']['Service']['DeleteThing'](arg1);
} }
export function GetSubThings(arg1) {
return window['go']['things']['Service']['GetSubThings'](arg1);
}
export function GetThings() { export function GetThings() {
return window['go']['things']['Service']['GetThings'](); return window['go']['things']['Service']['GetThings']();
} }

View File

@ -12,6 +12,13 @@ import (
type Thing struct { type Thing struct {
ID int ID int
Name string Name string
Subthings []SubThing
}
type SubThing struct {
ID int
ThingID int
Name string
} }
func InitDB() *gorm.DB { func InitDB() *gorm.DB {
@ -23,6 +30,6 @@ func InitDB() *gorm.DB {
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
db.AutoMigrate(&Thing{}) db.AutoMigrate(&Thing{}, &SubThing{})
return db return db
} }

View File

@ -4,7 +4,7 @@
CURRENT_DIR_NAME=$(basename "$PWD") CURRENT_DIR_NAME=$(basename "$PWD")
# Define the files to be updated # Define the files to be updated
FILES=("main.go" "go.mod" "wails.json" "things/resource.go") FILES=("main.go" "go.mod" "wails.json")
# String to be replaced # String to be replaced
OLD_STRING="wails-template" OLD_STRING="wails-template"

View File

@ -31,3 +31,24 @@ func (s *Service) DeleteThing(id int) {
log.Fatal(err) log.Fatal(err)
} }
} }
func (s *Service) GetSubThings(thingID int) (subthings []model.SubThing) {
if err := s.DB.Where("thing_id = ?", thingID).Find(&subthings).Error; err != nil {
log.Fatal(err)
}
return
}
func (s *Service) AddSubThing(thingID int, name string) {
if err := s.DB.Save(&model.SubThing{
ThingID: thingID,
Name: name,
}).Error; err != nil {
log.Fatal(err)
}
}
func (s *Service) DeleteSubThing(id int) {
if err := s.DB.Delete(model.SubThing{}, id).Error; err != nil {
log.Fatal(err)
}
}