diff --git a/.gitignore b/.gitignore index 549d13f..48954fa 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,13 @@ -.air \ No newline at end of file +# Air (hot reload) generated +.air + +# temp folder +tmp + +# Frontend asset +web/dist + +# build folder +build + +.DS_Store \ No newline at end of file diff --git a/server/workspace.go b/server/workspace.go index 50d2131..b905491 100644 --- a/server/workspace.go +++ b/server/workspace.go @@ -31,6 +31,15 @@ func (s *Server) registerWorkspaceRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create workspace").SetInternal(err) } + _, err = s.Store.UpsertWorkspaceUser(ctx, &api.WorkspaceUserUpsert{ + WorkspaceID: workspace.ID, + UserID: userID, + Role: api.RoleAdmin, + }) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create workspace user").SetInternal(err) + } + c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(workspace)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode workspace response").SetInternal(err) diff --git a/store/db/migration/dev/LATEST__SCHEMA.sql b/store/db/migration/dev/LATEST__SCHEMA.sql index d073576..6915dc9 100644 --- a/store/db/migration/dev/LATEST__SCHEMA.sql +++ b/store/db/migration/dev/LATEST__SCHEMA.sql @@ -108,7 +108,7 @@ CREATE TABLE shortcut ( workspace_id INTEGER NOT NULL, name TEXT NOT NULL, link TEXT NOT NULL DEFAULT '', - visibility TEXT NOT NULL CHECK (row_status IN ('PRIVATE', 'WORKSPACE')) DEFAULT 'PRIVATE', + visibility TEXT NOT NULL CHECK (visibility IN ('PRIVATE', 'WORKSPACE')) DEFAULT 'PRIVATE', FOREIGN KEY(creator_id) REFERENCES user(id) ON DELETE CASCADE, FOREIGN KEY(workspace_id) REFERENCES workspace(id) ON DELETE CASCADE ); diff --git a/store/db/migration/prod/LATEST__SCHEMA.sql b/store/db/migration/prod/LATEST__SCHEMA.sql index f3f9aba..6915dc9 100644 --- a/store/db/migration/prod/LATEST__SCHEMA.sql +++ b/store/db/migration/prod/LATEST__SCHEMA.sql @@ -11,12 +11,14 @@ DROP TABLE IF EXISTS `workspace`; -- workspace CREATE TABLE workspace ( id INTEGER PRIMARY KEY AUTOINCREMENT, + creator_id INTEGER NOT NULL, created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL', name TEXT NOT NULL UNIQUE, - description TEXT NOT NULL DEFAULT '' -) + description TEXT NOT NULL DEFAULT '', + FOREIGN KEY(creator_id) REFERENCES user(id) ON DELETE CASCADE +); INSERT INTO sqlite_sequence (name, seq) @@ -106,7 +108,7 @@ CREATE TABLE shortcut ( workspace_id INTEGER NOT NULL, name TEXT NOT NULL, link TEXT NOT NULL DEFAULT '', - visibility TEXT NOT NULL CHECK (row_status IN ('PRIVATE', 'WORKSPACE')) DEFAULT 'PRIVATE', + visibility TEXT NOT NULL CHECK (visibility IN ('PRIVATE', 'WORKSPACE')) DEFAULT 'PRIVATE', FOREIGN KEY(creator_id) REFERENCES user(id) ON DELETE CASCADE, FOREIGN KEY(workspace_id) REFERENCES workspace(id) ON DELETE CASCADE ); diff --git a/store/db/seed/10001__user.sql b/store/db/seed/10001__user.sql new file mode 100644 index 0000000..297559b --- /dev/null +++ b/store/db/seed/10001__user.sql @@ -0,0 +1,49 @@ +INSERT INTO + user ( + `id`, + `email`, + `name`, + `password_hash` + ) +VALUES + ( + 101, + 'demo@iamcorgi.com', + 'Demo Host', + -- raw password: secret + '$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK' + ); + +INSERT INTO + user ( + `id`, + `email`, + `name`, + `password_hash` + ) +VALUES + ( + 102, + 'jack@iamcorgi.com', + 'Jack', + -- raw password: secret + '$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK' + ); + +INSERT INTO + user ( + `id`, + `row_status`, + `email`, + `name`, + `password_hash` + ) +VALUES + ( + 103, + 'ARCHIVED', + 'bob@iamcorgi.com', + 'Bob', + -- raw password: secret + '$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK' + ); \ No newline at end of file diff --git a/store/db/seed/10002__workspace.sql b/store/db/seed/10002__workspace.sql new file mode 100644 index 0000000..0ca0b8a --- /dev/null +++ b/store/db/seed/10002__workspace.sql @@ -0,0 +1,14 @@ +INSERT INTO + workspace ( + `id`, + `creator_id`, + `name`, + `description` + ) +VALUES + ( + 11, + 101, + 'Demo', + '' + ); diff --git a/store/db/seed/10003__workspace_user.sql b/store/db/seed/10003__workspace_user.sql new file mode 100644 index 0000000..599c5b2 --- /dev/null +++ b/store/db/seed/10003__workspace_user.sql @@ -0,0 +1,12 @@ +INSERT INTO + workspace_user ( + `workspace_id`, + `user_id`, + `role` + ) +VALUES + ( + 11, + 101, + 'ADMIN' + ); diff --git a/store/db/seed/10004__shortcut.sql b/store/db/seed/10004__shortcut.sql new file mode 100644 index 0000000..7a16449 --- /dev/null +++ b/store/db/seed/10004__shortcut.sql @@ -0,0 +1,16 @@ +INSERT INTO + shortcut ( + `creator_id`, + `workspace_id`, + `name`, + `link`, + `visibility` + ) +VALUES + ( + 101, + 11, + 'baidu', + 'https://baidu.com', + 'WORKSPACE' + );