From 39071c1550779e7102fe7e58e7f89a2cde35eff9 Mon Sep 17 00:00:00 2001 From: giaogiao Date: Mon, 4 Sep 2023 18:00:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=80=E4=B9=88=E4=B9=9F=E6=B2=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/wol_item.go | 26 +++++++++++--------------- database/database.go | 23 ++++++++++++++++++++++- main_test.go | 13 +++++++++++++ wol.db | Bin 12288 -> 12288 bytes 4 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 main_test.go diff --git a/controller/wol_item.go b/controller/wol_item.go index cd50d78..ecd0027 100644 --- a/controller/wol_item.go +++ b/controller/wol_item.go @@ -3,6 +3,7 @@ package controller import ( "encoding/json" "fmt" + "goweb/database" "goweb/res" "goweb/wol" "net" @@ -36,21 +37,16 @@ func Send_start(mac string, ws **websocket.Conn) { } func Getall(ws **websocket.Conn) { - type wol struct { - ID int - Name string - Address string - } - var wolarray []wol - wol_item := new(wol) - wol_item.ID = 1 - wol_item.Name = "电脑" - wol_item.Address = "10:7B:44:80:F4:6A" - wol_item2 := new(wol) - wol_item2.ID = 2 - wol_item2.Name = "aa" - wol_item2.Address = "10:7B:44:80:F4:6A" - wolarray = append(wolarray, *wol_item, *wol_item2) + wolarray := make([]database.Wol, 0) + // wol_item := new(wol) + // wol_item.ID = 1 + // wol_item.Name = "电脑" + // wol_item.Address = "10:7B:44:80:F4:6A" + // wol_item2 := new(wol) + // wol_item2.ID = 2 + // wol_item2.Name = "aa" + // wol_item2.Address = "10:7B:44:80:F4:6A" + // wolarray = append(wolarray, *wol_item, *wol_item2) v, err := json.Marshal(wolarray) if err != nil { fmt.Println("marshal failed!", err) diff --git a/database/database.go b/database/database.go index 54f1de6..123e91b 100644 --- a/database/database.go +++ b/database/database.go @@ -1,12 +1,18 @@ package database import ( - _ "modernc.org/sqlite" + _ "github.com/mattn/go-sqlite3" "xorm.io/xorm" ) var engine *xorm.Engine +type Wol struct { + ID int + Name string `xorm:"varchar(64)"` + Mac string `xorm:"varchar(64)"` +} + func init() { var err error file := "./wol.db" @@ -15,4 +21,19 @@ func init() { panic(err) } + +} +func CreateTable() int { + err := engine.Sync2(new(Wol)) + if err != nil { + panic(err) + } + return 0 +} +func Getallwol(everyone *[]Wol) { + // everyone := make([]wol, 0) + err := engine.Find(&everyone) + if err != nil { + panic(err) + } } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..3df7dae --- /dev/null +++ b/main_test.go @@ -0,0 +1,13 @@ +package main + +import ( + "goweb/database" + "testing" +) + +func TestAdd(t *testing.T) { + if ans := database.CreateTable(); ans == 0 { + t.Errorf("1 + 2 expected be 3, but %d got", ans) + } + +} diff --git a/wol.db b/wol.db index 5ae2447aeb4fe2cff5606b48359ce415c4747b4d..c5184374d891ad66eeb3f1960eadd0a6443cd8f2 100644 GIT binary patch delta 203 zcmZojXh@hKEy%*az`zW|Fu*)f#~3K6mvw=cKZb#cpNoN?i+|x}L4jF(lezeVK+0Vw z7CNTJGP8>d3o|wet3rsQlaH%H0#GDDK_ekEJ|#iH(=Wu; z-8D$TFVx3JM(hx`*8B)K451_u655TO7r KZa$cc85jV}oh^v~ delta 295 zcmZojXh@hKEy%>cz`zW|Fu*ub#~3K6XZ?{ED8$6i#lRoK&$U@lU>4tGF8-kUMny(; zad~;hCc%=#q@2{$!qUuw+|;}hID^SK$kj2#RUyRD$;VYe2`;Xrpux+h2n(5D?_)>lhTN;O!czprGIw8shKi2h{BA>KCE|RI23X=`y zqTr_=>f-~JbaZhEat#hfmj_Doa%qB`&dC3mf&Vek;Wzo!r5RZmB=w(9+4`boqM?D6 lxs#QNiIs(cm79r`nIlB;CkFmcK*cZk`FI$a