refactor(doc): encapsulate document query functions into DocService class

This commit is contained in:
tekh 2025-07-16 13:45:46 +02:00
parent c022b96a12
commit a34270cfc3
2 changed files with 28 additions and 24 deletions

View File

@ -7,8 +7,8 @@ import Typography from '@mui/material/Typography';
import Pagination from '@mui/material/Pagination';
import { DashboardContent } from 'src/layouts/dashboard';
import docService, { Doc } from 'src/services/document-service';
import { Attribute, getAttributes } from 'src/services/attribute-service';
import { Doc, getDocumentByAttributes, getDocuments } from 'src/services/document-service';
import { Iconify } from 'src/components/iconify';
@ -40,7 +40,7 @@ export function DocSearchView() {
const [docs, setDocs] = useState<Doc[]>([]);
useEffect(() => {
getDocuments({}).then((res) => {
docService.getDocuments({}).then((res) => {
setDocs(res);
});
}, []);
@ -49,7 +49,7 @@ export function DocSearchView() {
const [attributes, setAttributes] = useState<Record<string, string>>({});
useEffect(() => {
getDocumentByAttributes(attributes).then(setDocs);
docService.getDocumentByAttributes(attributes).then(setDocs);
}, [attributes]);
function setAttribute(name: string, serilizedValue: string) {

View File

@ -77,7 +77,8 @@ export type DocQuery = {
attributes?: Record<string, string>
}
export function getDocuments(query: DocQuery | undefined = undefined): Promise<Doc[]> {
class DocService {
getDocuments(query: DocQuery | undefined = undefined): Promise<Doc[]> {
let documents = _documents;
if (query?.id)
@ -94,14 +95,17 @@ export function getDocuments(query: DocQuery | undefined = undefined): Promise<D
return Promise.resolve(documents);
}
export function getDocumentById(id: number): Promise<Doc[]> {
return getDocuments({ id: id });
getDocumentById(id: number): Promise<Doc[]> {
return this.getDocuments({ id: id });
}
export function getDocumentByName(name: string): Promise<Doc[]> {
return getDocuments({ name: name });
getDocumentByName(name: string): Promise<Doc[]> {
return this.getDocuments({ name: name });
}
export function getDocumentByAttributes(attributes: Record<string, string>): Promise<Doc[]> {
return getDocuments({ attributes: attributes });
getDocumentByAttributes(attributes: Record<string, string>): Promise<Doc[]> {
return this.getDocuments({ attributes: attributes });
}
}
export default new DocService();