Skip to content

Commit

Permalink
feat:add gorm
Browse files Browse the repository at this point in the history
  • Loading branch information
sanda0 committed Jun 10, 2024
1 parent 7c853e5 commit dc01a40
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ frontend/dist

*.txt

*.matic.json
*.matic.json

*.db
13 changes: 12 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"log"

"github.com/sanda0/webmatic/webmaticlib"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

type Response struct {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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{
Expand Down
28 changes: 25 additions & 3 deletions frontend/src/pages/Editor.jsx
Original file line number Diff line number Diff line change
@@ -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(){

}
export default function Editor() {
const { name } = useParams();

useEffect(() => {
console.log(name);
});

return (
<div className="h-screen p-0">
<div className="bg-slate-700 h-14">
<div className="w-10">
<Button>
{" "}
<Play></Play>{" "}
</Button>
</div>
</div>
{name}
</div>
);
}
13 changes: 7 additions & 6 deletions frontend/src/pages/NewMatic.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/layouts/MainLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function MainLayout() {


return (
<div className="flex w-full h-screen bg-slate-300">
<div className="flex w-full h-screen p-0 m-0 bg-slate-300">
<div className="h-screen w-[300px] bg-slate-700 text-white">
<ul className="p-4 ">
<li ><div className="text-4xl text-center">WebMatic</div></li>
Expand All @@ -24,7 +24,7 @@ export default function MainLayout() {
</li>
</ul>
</div>
<div className="w-full h-screen">
<div className="w-full h-screen p-0 m-0" style={{paddingTop:10}}>
<Outlet></Outlet>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/routes/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Route path="/" element={<MainLayout></MainLayout>}>
<Route path="new" element={<NewMatic></NewMatic>}></Route>
<Route path="matic/:name" element={<Editor></Editor>}></Route>
</Route>
));

Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
11 changes: 10 additions & 1 deletion webmaticlib/files.go → webmaticlib/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io/ioutil"
"math/rand"
"time"

"gorm.io/gorm"
)

// SaveStructToJSON saves a struct to a JSON file
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
10 changes: 10 additions & 0 deletions webmaticlib/models.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit dc01a40

Please sign in to comment.