Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
6064d9847c | |||
235a90b0a7 | |||
4abddac94f | |||
f12c353905 | |||
2586f0749f | |||
1387eecc96 |
5
app.go
5
app.go
@@ -2,7 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
// App struct
|
||||
@@ -139,5 +140,5 @@ func (a *App) SetSetting(key string, value int64) WailsGenericAck {
|
||||
|
||||
//region other
|
||||
func (a *App) Close() {
|
||||
os.Exit(0)
|
||||
runtime.Quit(a.ctx)
|
||||
}
|
22
db.go
22
db.go
@@ -4,6 +4,7 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/mattn/go-sqlite3"
|
||||
@@ -21,6 +22,22 @@ func (db *DB) Open() error {
|
||||
return fmt.Errorf("database path not set")
|
||||
}
|
||||
|
||||
file, err := os.Open(db.path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Printf("Database file does not exist at %s, creating", db.path)
|
||||
file, err := os.Create(db.path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create database file: %v", err)
|
||||
}
|
||||
log.Printf("Database created at %s", db.path)
|
||||
file.Close()
|
||||
} else {
|
||||
return fmt.Errorf("failed to open database file: %v", err)
|
||||
}
|
||||
}
|
||||
file.Close()
|
||||
|
||||
sql.Register("spellfixlite", &sqlite3.SQLiteDriver{
|
||||
Extensions: []string{"spellfix"},
|
||||
})
|
||||
@@ -95,6 +112,11 @@ func (db *DB) Init(ddl string) error {
|
||||
_, err = db.writeConn.Exec(ddl)
|
||||
if err != nil {
|
||||
Error.Printf("%++v", err)
|
||||
log.Printf("%#v", "Rolling back")
|
||||
_, err2 := db.writeConn.Exec("ROLLBACK;")
|
||||
if err2 != nil {
|
||||
Error.Printf("Error rollingback! %++v", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
5
food.ddl
5
food.ddl
@@ -1,4 +1,4 @@
|
||||
begin transaction;
|
||||
begin;
|
||||
|
||||
create table weight (
|
||||
date datetime default (datetime('now', '+2 hours')),
|
||||
@@ -124,7 +124,6 @@ where not exists (
|
||||
);
|
||||
end;
|
||||
|
||||
-- Spellfix search example
|
||||
with search_results as (
|
||||
select word, score, f.rowid, f.*
|
||||
from foodfix
|
||||
@@ -134,7 +133,7 @@ with search_results as (
|
||||
select rowid, food, score, date, description, amount, per100, energy
|
||||
from search_results
|
||||
group by food
|
||||
order by score asc, date desc
|
||||
order by score asc, date desc;
|
||||
|
||||
create index dailyIdx on food(strftime('%Y-%m-%d', date));
|
||||
create index weeklyIdx on food(strftime('%Y-%W', date));
|
||||
|
@@ -30,9 +30,24 @@
|
||||
name = name.trim();
|
||||
if (!name) {
|
||||
foodSearch = [];
|
||||
} else {
|
||||
updateAutocomplete();
|
||||
}
|
||||
}
|
||||
|
||||
function updateAutocomplete() {
|
||||
if (!per100Edited)
|
||||
GetLastPer100(name.trim()).then((res) => {
|
||||
// Prevent search when there's nothing to search
|
||||
// Sometimes we get search results after deleting name
|
||||
if (res.success && res.data && name) {
|
||||
foodSearch = res.data;
|
||||
} else {
|
||||
foodSearch = [];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function update(event: KeyboardEvent & { currentTarget: EventTarget & HTMLTableCellElement }) {
|
||||
name = name.trim();
|
||||
amount = amount.trim();
|
||||
@@ -69,17 +84,6 @@
|
||||
nameElement.focus();
|
||||
foodSearch = [];
|
||||
}
|
||||
|
||||
if (!per100Edited)
|
||||
GetLastPer100(name.trim()).then((res) => {
|
||||
// Prevent search when there's nothing to search
|
||||
// Sometimes we get search results after deleting name
|
||||
if (res.success && res.data && name) {
|
||||
foodSearch = res.data;
|
||||
} else {
|
||||
foodSearch = [];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let hiLiteIndex: number | null = null;
|
||||
@@ -99,8 +103,9 @@
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
function setInputVal(val: string) {
|
||||
name = val;
|
||||
function setInputVal(food: main.Food) {
|
||||
name = food.food;
|
||||
per100 = String(food.per100);
|
||||
hiLiteIndex = null;
|
||||
foodSearch = [];
|
||||
}
|
||||
@@ -131,6 +136,8 @@
|
||||
contenteditable="true"
|
||||
autofocus
|
||||
on:keydown={update}
|
||||
on:focusin={updateAutocomplete}
|
||||
on:focusout={() => foodSearch = []}
|
||||
bind:this={nameElement}
|
||||
/>
|
||||
<td
|
||||
@@ -160,11 +167,7 @@
|
||||
{#if foodSearch.length > 0}
|
||||
<ul bind:this={autocompleteList} class="z-50 fixed top-0 left-0 w-3/12 border border-x-gray-800">
|
||||
{#each foodSearch as f, i}
|
||||
<FoodSearchEntry
|
||||
itemLabel={f.food}
|
||||
highlighted={i == hiLiteIndex}
|
||||
on:click={() => setInputVal(f.food)}
|
||||
/>
|
||||
<FoodSearchEntry itemLabel={f.food} highlighted={i == hiLiteIndex} on:click={() => setInputVal(f)} />
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
|
6
main.go
6
main.go
@@ -56,7 +56,11 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
defer db.Close()
|
||||
db.Init(foodDDL)
|
||||
err = db.Init(foodDDL)
|
||||
if err != nil {
|
||||
Error.Printf("Error initializing database: %++v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
settingsService = &SettingsService{db: &db}
|
||||
err = settingsService.LoadSettings()
|
||||
|
BIN
spellfix.dll
Normal file
BIN
spellfix.dll
Normal file
Binary file not shown.
Reference in New Issue
Block a user