Compare commits

...

2 Commits

Author SHA1 Message Date
2b13e5c03a remove sub things and add view
All checks were successful
build / windows (push) Successful in 2m22s
build / linux (push) Successful in 2m3s
2025-03-07 15:23:33 +01:00
feb248e991 add a route 2025-03-07 15:22:44 +01:00
9 changed files with 48 additions and 94 deletions

View File

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

View File

@ -0,0 +1,26 @@
<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

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

View File

@ -1,25 +1,8 @@
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 {
ID: number;
Name: string;
Subthings: SubThing[];
static createFrom(source: any = {}) {
return new Thing(source);
@ -29,26 +12,7 @@ export namespace model {
if ('string' === typeof source) source = JSON.parse(source);
this.ID = source["ID"];
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,14 +2,8 @@
// This file is automatically generated. DO NOT EDIT
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 GetSubThings(arg1:number):Promise<Array<model.SubThing>>;
export function GetThings():Promise<Array<model.Thing>>;
export function NewThing(arg1:string):Promise<void>;

View File

@ -2,22 +2,10 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// 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) {
return window['go']['things']['Service']['DeleteThing'](arg1);
}
export function GetSubThings(arg1) {
return window['go']['things']['Service']['GetSubThings'](arg1);
}
export function GetThings() {
return window['go']['things']['Service']['GetThings']();
}

View File

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

View File

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

View File

@ -31,24 +31,3 @@ func (s *Service) DeleteThing(id int) {
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)
}
}