From 1ecd81371c45e55ad932f9284ea48c840dad83c8 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Tue, 12 Dec 2023 21:33:49 +0800 Subject: [PATCH] feat(database):imeplement database create --- .../business/database/blocks/postgre.tsx | 4 +- .../pages/business/database/blocks/redis.tsx | 4 +- .../database/create-database-editor.tsx | 34 ++++++++---- .../src/pages/business/database/overview.tsx | 53 ++++++++----------- .../business/workplace/business-item.tsx | 6 +-- .../src/pages/business/workplace/overview.tsx | 1 - dashboard/src/types/index.ts | 2 +- .../adapter/in/restful/route/admin_server.go | 7 ++- .../adapter/out/admin/database_adapter.go | 3 +- 9 files changed, 64 insertions(+), 50 deletions(-) diff --git a/dashboard/src/pages/business/database/blocks/postgre.tsx b/dashboard/src/pages/business/database/blocks/postgre.tsx index aa1521a..38ccc1d 100644 --- a/dashboard/src/pages/business/database/blocks/postgre.tsx +++ b/dashboard/src/pages/business/database/blocks/postgre.tsx @@ -1,11 +1,11 @@ import React from 'react' -import { DatabaseConnectionConfig } from '../../../../types' +import { DatabaseConnectConfig } from "@/api/openapi"; import styles from '../style/blocks.module.less'; import { Button, Card,Typography } from '@arco-design/web-react'; const { Text, Title } = Typography; interface PostgresProps { - config: DatabaseConnectionConfig + config: DatabaseConnectConfig } const Postgres = ({config}:PostgresProps)=>{ return ( diff --git a/dashboard/src/pages/business/database/blocks/redis.tsx b/dashboard/src/pages/business/database/blocks/redis.tsx index 0e6addb..e9a3a06 100644 --- a/dashboard/src/pages/business/database/blocks/redis.tsx +++ b/dashboard/src/pages/business/database/blocks/redis.tsx @@ -1,11 +1,11 @@ import React from 'react' -import { DatabaseConnectionConfig } from '../../../../types' import styles from '../style/blocks.module.less'; import { Button, Card,Typography } from '@arco-design/web-react'; const { Text, Title } = Typography; +import { DatabaseConnectConfig } from "@/api/openapi"; interface RedisProps { - config: DatabaseConnectionConfig + config: DatabaseConnectConfig } const Redis = ({config}:RedisProps)=>{ return ( diff --git a/dashboard/src/pages/business/database/create-database-editor.tsx b/dashboard/src/pages/business/database/create-database-editor.tsx index dd8b0d8..5585218 100644 --- a/dashboard/src/pages/business/database/create-database-editor.tsx +++ b/dashboard/src/pages/business/database/create-database-editor.tsx @@ -42,13 +42,13 @@ const CreateDatabase = () => { form.validate().then((res)=>{ const config:DatabaseConnectConfig = { title: res.title, - "id": uuid(), - "databaseType":"redis", - "host": res.host, - "port": Number(res.port), - "username":"", + id: uuid(), + databaseType:"redis", + host: res.host, + port: Number(res.port), + username:"", password: res?.password || "", - "database":"" + database:"" } console.log(config) axios.post("/admin/v1/database/test", config).then((res) => { @@ -77,11 +77,27 @@ const CreateDatabase = () => { onOk={()=>{ form.validate().then((res)=>{ setConfirmLoading(true); - console.log(res) - createDatabase(res).then((res)=>{ + + const config:DatabaseConnectConfig = { + title: res?.title || "", + id: uuid(), + databaseType: res.type, + host: res.host, + port: Number(res.port), + username: res?.username || "", + password: res?.password || "", + database: res?.database || "" + } + + console.log(config) + createDatabase(config).then(()=>{ toast.success("create success"); + setConfirmLoading(false); + setVisible(false); + }).catch((err)=>{ + toast.error(err.response.data.msg); + setConfirmLoading(false); }) - setConfirmLoading(false); }) }} diff --git a/dashboard/src/pages/business/database/overview.tsx b/dashboard/src/pages/business/database/overview.tsx index 1670ac6..27c77f6 100644 --- a/dashboard/src/pages/business/database/overview.tsx +++ b/dashboard/src/pages/business/database/overview.tsx @@ -1,10 +1,9 @@ -import React, { useEffect, useState } from "react"; -import { Card, Divider, Typography,Grid } from "@arco-design/web-react"; -import { DatabaseConnectionConfig } from "../../../types"; -import axios from "axios"; +import React from "react"; +import { Card, Divider, Typography,Grid, Skeleton } from "@arco-design/web-react"; import Redis from "./blocks/redis"; import Postgres from "./blocks/postgre"; import CreateDatabase from "./create-database-editor"; +import { useDatabase } from "@/api"; const { Row, Col } = Grid; @@ -12,18 +11,8 @@ const { Title } = Typography; const Overview = () => { - // TODO extract to a hook - const [data, setData] = useState([]); - - const fetchData = () => { - axios.get('/admin/v1/database').then((res) => { - setData(res.data.dataSourceConfig); - }); - } + const {database,isLoading} = useDatabase(); - useEffect(() => { - fetchData() - }, []); return ( @@ -34,9 +23,10 @@ const Overview = () => { Redis - + + { - data + database .filter((item) => item.databaseType === 'redis') .map((item) => ( @@ -44,6 +34,7 @@ const Overview = () => { )) } + @@ -51,19 +42,21 @@ const Overview = () => {
Postgres - - { - data - .filter((item) => item.databaseType === 'postgres') - .map((item) => ( - <> - - - - - )) - } - + + + { + database + .filter((item) => item.databaseType === 'postgres') + .map((item) => ( + <> + + + + + )) + } + +
diff --git a/dashboard/src/pages/business/workplace/business-item.tsx b/dashboard/src/pages/business/workplace/business-item.tsx index 6e5a421..bc967db 100644 --- a/dashboard/src/pages/business/workplace/business-item.tsx +++ b/dashboard/src/pages/business/workplace/business-item.tsx @@ -1,14 +1,14 @@ import { Button, Card, Drawer, Grid, Skeleton, Typography } from '@arco-design/web-react'; -import React, { useState, useEffect } from 'react'; +import React, { useState } from 'react'; import styles from './style/overview.module.less'; import BusinessEditor from './business-editor'; -import { BusinessType } from '../../../types/type'; +import { Business } from '@/api/openapi'; const { Row, Col } = Grid; const { Title,Text } = Typography; interface BusinessItemProps { - business: BusinessType; + business: Business; } const BusinessItem = ({business}:BusinessItemProps) => { diff --git a/dashboard/src/pages/business/workplace/overview.tsx b/dashboard/src/pages/business/workplace/overview.tsx index efc8b87..171ce4b 100644 --- a/dashboard/src/pages/business/workplace/overview.tsx +++ b/dashboard/src/pages/business/workplace/overview.tsx @@ -8,7 +8,6 @@ import { } from '@arco-design/web-react'; import styles from './style/overview.module.less'; import BusinessItem from './business-item'; -import { BusinessType } from '@/types'; import CreateBusinessEditor from './create-business-editor'; import { useBusiness } from '@/api'; diff --git a/dashboard/src/types/index.ts b/dashboard/src/types/index.ts index f5c7f9c..b2e9ce4 100644 --- a/dashboard/src/types/index.ts +++ b/dashboard/src/types/index.ts @@ -9,7 +9,7 @@ interface DatabaseConnectionConfig{ database: string; } type BusinessType = { - title: string; + title?: string; id: string; type: string; }; diff --git a/internal/adapter/in/restful/route/admin_server.go b/internal/adapter/in/restful/route/admin_server.go index b16f03c..23d5ebd 100644 --- a/internal/adapter/in/restful/route/admin_server.go +++ b/internal/adapter/in/restful/route/admin_server.go @@ -222,7 +222,12 @@ func (a *adminApiService) GetDatabaseConfigureList(ctx echo.Context) error { } func (a *adminApiService) CreateDatabase(ctx echo.Context) error { - err := a.databaseUseCase.CreateDatabase(ctx.Request().Context(), domain.DatabaseConnectConfig{}) + var databaseConnectConfig codegen.DatabaseConnectConfig + if err := ctx.Bind(&databaseConnectConfig); err != nil { + return ctx.JSON(http.StatusBadRequest, err) + } + + err := a.databaseUseCase.CreateDatabase(ctx.Request().Context(), convertDatabaseConnectConfig(databaseConnectConfig)) if err != nil { return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{ Status: utils.Ptr("error"), diff --git a/internal/adapter/out/admin/database_adapter.go b/internal/adapter/out/admin/database_adapter.go index d8fb5d2..87cbb23 100644 --- a/internal/adapter/out/admin/database_adapter.go +++ b/internal/adapter/out/admin/database_adapter.go @@ -30,7 +30,8 @@ func NewDatabaseAdapter(config domain.PostgresConfig) out.DatabasePort { } func (d *DatabaseAdapter) CreateDatabaseConnectConfig(config domain.DatabaseConnectConfig) error { - return d.db.Create(&config).Error + fmt.Println(config) + return d.db.Create(config).Error } func (d *DatabaseAdapter) DatabaseConnectConfig(configId string) (domain.DatabaseConnectConfig, error) {