diff --git a/.gitignore b/.gitignore index a6dc935..f880cb6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ frontend/dist *.txt -*.matic.json \ No newline at end of file +*.matic.json + +*.db \ No newline at end of file diff --git a/app.go b/app.go index 0084730..8a5645d 100644 --- a/app.go +++ b/app.go @@ -6,6 +6,8 @@ import ( "log" "github.com/sanda0/webmatic/webmaticlib" + "gorm.io/driver/sqlite" + "gorm.io/gorm" ) type Response struct { @@ -16,6 +18,7 @@ type Response struct { // App struct type App struct { ctx context.Context + DB *gorm.DB } // NewApp creates a new App application struct @@ -27,6 +30,14 @@ func NewApp() *App { // so we can call the runtime methods func (a *App) startup(ctx context.Context) { a.ctx = ctx + + var err error + a.DB, err = gorm.Open(sqlite.Open("data.db"), &gorm.Config{}) + if err != nil { + log.Fatalf("failed to connect database: %v", err) + } + + a.DB.AutoMigrate(&webmaticlib.ProjectMap{}) } // Greet returns a greeting for the given name @@ -39,7 +50,7 @@ func (a *App) FileUpload(fileName string) int { } func (a *App) SaveMatic(name string, autor string) Response { - fileName, err := webmaticlib.SaveMatic(name, autor) + fileName, err := webmaticlib.SaveMatic(a.DB, name, autor) if err != nil { log.Println(err.Error()) return Response{ diff --git a/frontend/src/pages/Editor.jsx b/frontend/src/pages/Editor.jsx index 5d7a584..e1a4b63 100644 --- a/frontend/src/pages/Editor.jsx +++ b/frontend/src/pages/Editor.jsx @@ -1,4 +1,26 @@ +import { useEffect } from "react"; +import { useParams } from "react-router-dom"; +import Button from "../components/Button"; +import { Play } from "lucide-react"; -export default function Editor(){ - -} \ No newline at end of file +export default function Editor() { + const { name } = useParams(); + + useEffect(() => { + console.log(name); + }); + + return ( +
+
+
+ +
+
+ {name} +
+ ); +} diff --git a/frontend/src/pages/NewMatic.jsx b/frontend/src/pages/NewMatic.jsx index 93eb2d9..5e6c9bf 100644 --- a/frontend/src/pages/NewMatic.jsx +++ b/frontend/src/pages/NewMatic.jsx @@ -3,21 +3,22 @@ import Input from "../components/Input"; import Button2 from "../components/Button2"; import { Save } from "lucide-react"; import { SaveMatic } from "../../wailsjs/go/main/App"; +import { useNavigate } from "react-router-dom"; export default function NewMatic() { - const [name, setname] = useState(""); const [author, setAuthor] = useState(""); + const navigate = useNavigate(); - function save(e) { + function save(e) { console.log(name); console.log(author); - SaveMatic(name, author).then((r)=>{ + SaveMatic(name, author).then((r) => { console.log(r); - if(r.status == 200){ - + if (r.status == 200) { + navigate("/matic/" + r.data); } - }) + }); } return ( diff --git a/frontend/src/pages/layouts/MainLayout.jsx b/frontend/src/pages/layouts/MainLayout.jsx index 2720c3c..9a16bd6 100644 --- a/frontend/src/pages/layouts/MainLayout.jsx +++ b/frontend/src/pages/layouts/MainLayout.jsx @@ -9,7 +9,7 @@ export default function MainLayout() { return ( -
+
  • WebMatic
  • @@ -24,7 +24,7 @@ export default function MainLayout() {
-
+
diff --git a/frontend/src/routes/index.jsx b/frontend/src/routes/index.jsx index 2cfb2fe..aeb9de1 100644 --- a/frontend/src/routes/index.jsx +++ b/frontend/src/routes/index.jsx @@ -5,10 +5,12 @@ import { } from "react-router-dom"; import MainLayout from "../pages/layouts/MainLayout"; import NewMatic from "../pages/NewMatic"; +import Editor from "../pages/Editor"; const router = createBrowserRouter(createRoutesFromElements( }> }> + }> )); diff --git a/go.mod b/go.mod index bfeb502..11e667c 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,8 @@ toolchain go1.22.0 require ( github.com/go-rod/rod v0.116.0 github.com/wailsapp/wails/v2 v2.8.2 + gorm.io/driver/sqlite v1.5.5 + gorm.io/gorm v1.25.10 ) require ( @@ -15,6 +17,8 @@ require ( github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/google/uuid v1.3.0 // indirect github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect github.com/labstack/echo/v4 v4.10.2 // indirect github.com/labstack/gommon v0.4.0 // indirect github.com/leaanthony/go-ansi-parser v1.6.0 // indirect @@ -23,6 +27,7 @@ require ( github.com/leaanthony/u v1.1.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-sqlite3 v1.14.22 // indirect github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/rivo/uniseg v0.4.4 // indirect diff --git a/go.sum b/go.sum index 491397d..0bc83c1 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,10 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e h1:Q3+PugElBCf4PFpxhErSzU3/PY5sFL5Z6rfv4AbGAck= github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e/go.mod h1:alcuEEnZsY1WQsagKhZDsoPCRoOijYqhZvPwLG0kzVs= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/labstack/echo/v4 v4.10.2 h1:n1jAhnq/elIFTHr1EYpiYtyKgx4RW9ccVgkqByZaN2M= github.com/labstack/echo/v4 v4.10.2/go.mod h1:OEyqf2//K1DFdE57vw2DRgWY0M7s65IVQO2FzvI4J5k= github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8= @@ -37,6 +41,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -108,3 +114,7 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E= +gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE= +gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s= +gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= diff --git a/webmaticlib/files.go b/webmaticlib/handler.go similarity index 89% rename from webmaticlib/files.go rename to webmaticlib/handler.go index 843df38..e404165 100644 --- a/webmaticlib/files.go +++ b/webmaticlib/handler.go @@ -6,6 +6,8 @@ import ( "io/ioutil" "math/rand" "time" + + "gorm.io/gorm" ) // SaveStructToJSON saves a struct to a JSON file @@ -54,7 +56,7 @@ func GenerateRandomString(length int) string { return string(b) } -func SaveMatic(name string, author string) (string, error) { +func SaveMatic(db *gorm.DB, name string, author string) (string, error) { matic := Project{ Name: name, Author: author, @@ -68,5 +70,12 @@ func SaveMatic(name string, author string) (string, error) { if err != nil { return "", err } + + projectMap := ProjectMap{ + Name: name, + Author: author, + FileName: fileName, + } + db.Create(&projectMap) return fileName, nil } diff --git a/webmaticlib/models.go b/webmaticlib/models.go new file mode 100644 index 0000000..9a91b2e --- /dev/null +++ b/webmaticlib/models.go @@ -0,0 +1,10 @@ +package webmaticlib + +import "gorm.io/gorm" + +type ProjectMap struct { + gorm.Model + Name string `json:"name"` + Author string `json:"author"` + FileName string `json:"file_name"` +}