Skip to content

Commit

Permalink
Merge pull request #435 from EstrellaXD/webui
Browse files Browse the repository at this point in the history
webui: Fit new api model. Add RSS Manage Page.
  • Loading branch information
EstrellaXD authored Aug 28, 2023
2 parents d101898 + 061def1 commit 0c860c5
Show file tree
Hide file tree
Showing 43 changed files with 698 additions and 223 deletions.
6 changes: 6 additions & 0 deletions backend/src/module/api/bangumi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
router = APIRouter(prefix="/bangumi", tags=["bangumi"])


def str_to_list(data: Bangumi):
data.filter = data.filter.split(",")
data.rss_link = data.rss_link.split(",")
return data


@router.get("/get/all", response_model=list[Bangumi])
async def get_all_data(current_user=Depends(get_current_user)):
if not current_user:
Expand Down
33 changes: 26 additions & 7 deletions backend/src/module/api/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from fastapi.responses import JSONResponse

from module.core import Program
from module.models import APIResponse
from module.conf import VERSION
from module.security.api import get_current_user, UNAUTHORIZED

Expand All @@ -24,20 +25,29 @@ async def shutdown():
program.stop()


@router.get("/restart")
@router.get("/restart", response_model=APIResponse)
async def restart(current_user=Depends(get_current_user)):
if not current_user:
raise UNAUTHORIZED
try:
program.restart()
return {"status": "ok"}
return JSONResponse(
status_code=200,
content={"msg_en": "Restart program successfully.", "msg_zh": "重启程序成功。"},
)
except Exception as e:
logger.debug(e)
logger.warning("Failed to restart program")
raise HTTPException(status_code=500, detail="Failed to restart program")
raise HTTPException(
status_code=500,
detail={
"msg_en": "Failed to restart program.",
"msg_zh": "重启程序失败。",
}
)


@router.get("/start")
@router.get("/start", response_model=APIResponse)
async def start(current_user=Depends(get_current_user)):
if not current_user:
raise UNAUTHORIZED
Expand All @@ -46,7 +56,13 @@ async def start(current_user=Depends(get_current_user)):
except Exception as e:
logger.debug(e)
logger.warning("Failed to start program")
raise HTTPException(status_code=500, detail="Failed to start program")
raise HTTPException(
status_code=500,
detail={
"msg_en": "Failed to start program.",
"msg_zh": "启动程序失败。",
}
)


@router.get("/stop")
Expand Down Expand Up @@ -79,11 +95,14 @@ async def shutdown_program(current_user=Depends(get_current_user)):
program.stop()
logger.info("Shutting down program...")
os.kill(os.getpid(), signal.SIGINT)
return {"status": "ok"}
return JSONResponse(
status_code=200,
content={"msg_en": "Shutdown program successfully.", "msg_zh": "关闭程序成功。"},
)


# Check status
@router.get("/check/downloader", tags=["check"])
@router.get("/check/downloader", tags=["check"], response_model=bool)
async def check_downloader_status(current_user=Depends(get_current_user)):
if not current_user:
raise UNAUTHORIZED
Expand Down
4 changes: 3 additions & 1 deletion backend/src/module/api/response.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fastapi.responses import JSONResponse
from fastapi.exceptions import HTTPException

from module.models.response import ResponseModel

Expand All @@ -7,7 +8,8 @@ def u_response(response_model: ResponseModel):
return JSONResponse(
status_code=response_model.status_code,
content={
"status": response_model.status,
"msg_en": response_model.msg_en,
"msg_zh": response_model.msg_zh,
},
)
)
1 change: 1 addition & 0 deletions backend/src/module/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ class ResponseModel(BaseModel):


class APIResponse(BaseModel):
status: bool = Field(..., example=True)
msg_en: str = Field(..., example="Success")
msg_zh: str = Field(..., example="成功")
2 changes: 1 addition & 1 deletion backend/src/module/rss/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def add_rss(self, rss_link: str, name: str | None = None, aggregate: bool = True
else:
return ResponseModel(
status=False,
status_code=400,
status_code=406,
msg_en="RSS added failed.",
msg_zh="RSS 添加失败。",
)
Expand Down
1 change: 1 addition & 0 deletions webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"pinia": "^2.1.3",
"vue": "^3.3.4",
"vue-i18n": "^9.2.2",
"vue-inline-svg": "^3.1.2",
"vue-router": "^4.2.1"
},
"devDependencies": {
Expand Down
11 changes: 11 additions & 0 deletions webui/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions webui/public/images/RSS.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 47 additions & 26 deletions webui/src/api/bangumi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { BangumiRule, BangumiUpdate } from '#/bangumi';
import type { ApiSuccess } from '#/api';
import {forEach} from "lodash";


export const apiBangumi = {
/**
Expand All @@ -8,7 +10,10 @@ export const apiBangumi = {
*/
async getAll() {
const { data } = await axios.get<BangumiRule[]>('api/v1/bangumi/get/all');

forEach(data, (item) => {
item.rss_link = item.rss_link.split(',');
item.filter = item.filter.split(',');
});
return data;
},

Expand All @@ -21,7 +26,8 @@ export const apiBangumi = {
const { data } = await axios.get<BangumiRule>(
`api/v1/bangumi/get/${bangumiId}`
);

data.rss_link = data.rss_link.split(',');
data.filter = data.filter.split(',');
return data;
},

Expand All @@ -33,8 +39,9 @@ export const apiBangumi = {
*/
async updateRule(bangumiId: number, bangumiRule: BangumiRule) {
const rule = omit(bangumiRule, ['id']);

const { data } = await axios.patch<ApiSuccess>(
rule.rss_link = rule.rss_link.join(',');
rule.filter = rule.filter.join(',');
const { data } = await axios.patch< ApiSuccess >(
`api/v1/bangumi/update/${bangumiId}`,
rule
);
Expand All @@ -47,15 +54,23 @@ export const apiBangumi = {
* @param file - 是否同时删除关联文件。
* @returns axios 请求返回的数据
*/
async deleteRule(bangumiId: number, file: boolean) {
const { data } = await axios.delete<ApiSuccess>(
`api/v1/bangumi/delete/${bangumiId}`,
{
params: {
file,
},
}
);
async deleteRule(bangumiId: number | number[], file: boolean) {
let url = 'api/v1/bangumi/delete';
let ids: undefined | number[];

if (typeof bangumiId === 'number') {
url = `${url}/${bangumiId}`;
} else {
url = `${url}/many`;
ids = bangumiId;
}

const { data } = await axios.delete< ApiSuccess >(url, {
data: ids,
params: {
file,
},
});
return data;
},

Expand All @@ -65,15 +80,23 @@ export const apiBangumi = {
* @param file - 是否同时删除关联文件。
* @returns axios 请求返回的数据
*/
async disableRule(bangumiId: number, file: boolean) {
const { data } = await axios.delete<ApiSuccess>(
`api/v1/bangumi/disable/${bangumiId}`,
{
params: {
file,
},
}
);
async disableRule(bangumiId: number | number[], file: boolean) {
let url = 'api/v1/bangumi/disable';
let ids: undefined | number[];

if (typeof bangumiId === 'number') {
url = `${url}/${bangumiId}`;
} else {
url = `${url}/many`;
ids = bangumiId;
}

const { data } = await axios.delete< ApiSuccess >(url, {
data: ids,
params: {
file,
},
});
return data;
},

Expand All @@ -82,7 +105,7 @@ export const apiBangumi = {
* @param bangumiId - 需要启用的 bangumi 的 id
*/
async enableRule(bangumiId: number) {
const { data } = await axios.get<ApiSuccess>(
const { data } = await axios.get< ApiSuccess >(
`api/v1/bangumi/enable/${bangumiId}`
);
return data;
Expand All @@ -92,9 +115,7 @@ export const apiBangumi = {
* 重置所有 bangumi 数据
*/
async resetAll() {
const { data } = await axios.get<{
message: 'OK';
}>('api/v1/bangumi/resetAll');
const { data } = await axios.get< ApiSuccess >('api/v1/bangumi/resetAll');
return data;
},
};
19 changes: 2 additions & 17 deletions webui/src/api/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,8 @@ export const apiCheck = {
* 检测下载器
*/
async downloader() {
const { data } = await axios.get('api/v1/check/downloader');
const { data } = await axios.get<Boolean>('api/v1/check/downloader');
return data;
},

/**
* 检测 RSS
*/
async rss() {
const { data } = await axios.get('api/v1/check/rss');
return data;
},

/**
* 检测所有
*/
async all() {
const { data } = await axios.get('api/v1/check');
return data;
},
};
}
13 changes: 7 additions & 6 deletions webui/src/api/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Config } from '#/config';
import type { ApiSuccess } from '#/api';

export const apiConfig = {
/**
* 获取 config 数据
*/
async getConfig() {
const { data } = await axios.get<Config>('api/v1/getConfig');
const { data } = await axios.get<Config>('api/v1/config/get');
return data;
},

Expand All @@ -14,10 +15,10 @@ export const apiConfig = {
* @param newConfig - 需要更新的 config
*/
async updateConfig(newConfig: Config) {
const { data } = await axios.post<{
message: 'Success' | 'Failed to update config';
}>('api/v1/updateConfig', newConfig);

return data.message === 'Success';
const { data } = await axios.patch<ApiSuccess>(
'api/v1/config/update',
newConfig
);
return data;
},
};
13 changes: 5 additions & 8 deletions webui/src/api/download.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type { BangumiRule } from '#/bangumi';

interface Status {
status: 'Success';
}
import type { ApiSuccess } from '#/api';

export const apiDownload = {
/**
Expand Down Expand Up @@ -30,22 +27,22 @@ export const apiDownload = {
* @param bangumiData - Bangumi 数据
*/
async collection(bangumiData: BangumiRule) {
const { data } = await axios.post<Status>(
const { data } = await axios.post<ApiSuccess>(
'api/v1/download/collection',
bangumiData
);
return data.status === 'Success';
return data;
},

/**
* 新番
* @param bangumiData - Bangumi 数据
*/
async subscribe(bangumiData: BangumiRule) {
const { data } = await axios.post<Status>(
const { data } = await axios.post<ApiSuccess>(
'api/v1/download/subscribe',
bangumiData
);
return data.status === 'Success';
return data;
},
};
6 changes: 4 additions & 2 deletions webui/src/api/log.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { ApiSuccess } from "#/api";

export const apiLog = {
async getLog() {
const { data } = await axios.get<string>('api/v1/log');
return data;
},

async clearLog() {
const { data } = await axios.get<{ status: 'ok' }>('api/v1/log/clear');
return data.status === 'ok';
const { data } = await axios.get<ApiSuccess>('api/v1/log/clear');
return data;
},
};
Loading

0 comments on commit 0c860c5

Please sign in to comment.