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

View File

@ -77,31 +77,35 @@ export type DocQuery = {
attributes?: Record<string, string> attributes?: Record<string, string>
} }
export function getDocuments(query: DocQuery | undefined = undefined): Promise<Doc[]> { class DocService {
let documents = _documents; getDocuments(query: DocQuery | undefined = undefined): Promise<Doc[]> {
let documents = _documents;
if (query?.id) if (query?.id)
documents = documents.filter(d => d.id === query.id); documents = documents.filter(d => d.id === query.id);
if (query?.name) if (query?.name)
documents = documents.filter(d => d.name === query.name); documents = documents.filter(d => d.name === query.name);
for (const name in query?.attributes) { for (const name in query?.attributes) {
const attr = query.attributes[name]; const attr = query.attributes[name];
documents = documents.filter(d => d.attributes.find(a => a.name === name)?.serilizedValue.toLowerCase().includes(attr.toLowerCase())); documents = documents.filter(d => d.attributes.find(a => a.name === name)?.serilizedValue.toLowerCase().includes(attr.toLowerCase()));
}
return Promise.resolve(documents);
} }
return Promise.resolve(documents); getDocumentById(id: number): Promise<Doc[]> {
return this.getDocuments({ id: id });
}
getDocumentByName(name: string): Promise<Doc[]> {
return this.getDocuments({ name: name });
}
getDocumentByAttributes(attributes: Record<string, string>): Promise<Doc[]> {
return this.getDocuments({ attributes: attributes });
}
} }
export function getDocumentById(id: number): Promise<Doc[]> { export default new DocService();
return getDocuments({ id: id });
}
export function getDocumentByName(name: string): Promise<Doc[]> {
return getDocuments({ name: name });
}
export function getDocumentByAttributes(attributes: Record<string, string>): Promise<Doc[]> {
return getDocuments({ attributes: attributes });
}