improvements are done

This commit is contained in:
sdarbinyan
2026-01-22 00:41:13 +04:00
parent a1a2a69fd0
commit 0f3d0ae3ef
27 changed files with 2115 additions and 107 deletions

View File

@@ -248,52 +248,105 @@ export class MockDataService {
}
getSubcategory(subcategoryId: string): Observable<Subcategory> {
let sub: Subcategory | undefined;
for (const cat of this.categories) {
sub = cat.subcategories?.find(s => s.id === subcategoryId);
if (sub) break;
}
const sub = this.findSubcategoryById(subcategoryId);
return of(sub!).pipe(delay(200));
}
updateSubcategory(subcategoryId: string, data: Partial<Subcategory>): Observable<Subcategory> {
let sub: Subcategory | undefined;
for (const cat of this.categories) {
sub = cat.subcategories?.find(s => s.id === subcategoryId);
if (sub) {
Object.assign(sub, data);
break;
}
const sub = this.findSubcategoryById(subcategoryId);
if (sub) {
Object.assign(sub, data);
}
return of(sub!).pipe(delay(300));
}
createSubcategory(categoryId: string, data: Partial<Subcategory>): Observable<Subcategory> {
const cat = this.categories.find(c => c.id === categoryId)!;
createSubcategory(parentId: string, data: Partial<Subcategory>): Observable<Subcategory> {
// Check if parent already has items
const parentSubcategory = this.findSubcategoryById(parentId);
if (parentSubcategory?.hasItems) {
throw new Error('Cannot create subcategory: parent already has items');
}
const newSub: Subcategory = {
id: `sub${Date.now()}`,
id: data.id || `sub${Date.now()}`,
name: data.name || 'New Subcategory',
visible: data.visible ?? true,
priority: data.priority || 99,
img: data.img,
categoryId,
categoryId: parentId,
itemCount: 0
};
if (!cat.subcategories) cat.subcategories = [];
cat.subcategories.push(newSub);
return of(newSub).pipe(delay(300));
// Try to find parent category first
const cat = this.categories.find(c => c.id === parentId);
if (cat) {
if (!cat.subcategories) cat.subcategories = [];
cat.subcategories.push(newSub);
return of(newSub).pipe(delay(300));
}
// If not a category, search for parent subcategory recursively
const parent = this.findSubcategoryById(parentId);
if (parent) {
if (!parent.subcategories) parent.subcategories = [];
parent.subcategories.push(newSub);
return of(newSub).pipe(delay(300));
}
// Parent not found
throw new Error(`Parent with id ${parentId} not found`);
}
private findSubcategoryById(id: string): Subcategory | null {
for (const cat of this.categories) {
const result = this.searchSubcategories(cat.subcategories || [], id);
if (result) return result;
}
return null;
}
private searchSubcategories(subcategories: Subcategory[], id: string): Subcategory | null {
for (const sub of subcategories) {
if (sub.id === id) return sub;
if (sub.subcategories) {
const result = this.searchSubcategories(sub.subcategories, id);
if (result) return result;
}
}
return null;
}
deleteSubcategory(subcategoryId: string): Observable<void> {
// Try to delete from category level
for (const cat of this.categories) {
const index = cat.subcategories?.findIndex(s => s.id === subcategoryId) ?? -1;
if (index > -1) {
cat.subcategories?.splice(index, 1);
break;
return of(void 0).pipe(delay(300));
}
// Try to delete from nested subcategories
if (this.deleteFromSubcategories(cat.subcategories || [], subcategoryId)) {
return of(void 0).pipe(delay(300));
}
}
return of(void 0).pipe(delay(300));
}
private deleteFromSubcategories(subcategories: Subcategory[], id: string): boolean {
for (const sub of subcategories) {
if (sub.subcategories) {
const index = sub.subcategories.findIndex(s => s.id === id);
if (index > -1) {
sub.subcategories.splice(index, 1);
return true;
}
if (this.deleteFromSubcategories(sub.subcategories, id)) {
return true;
}
}
}
return false;
}
getItems(subcategoryId: string, page = 1, limit = 20, search?: string, filters?: any): Observable<ItemsListResponse> {
let allItems = [...this.items, ...this.generateMoreItems(subcategoryId, 50)];
@@ -354,12 +407,32 @@ export class MockDataService {
subcategoryId
};
this.items.push(newItem);
// Mark subcategory as having items
const subcategory = this.findSubcategoryById(subcategoryId);
if (subcategory) {
subcategory.hasItems = true;
}
return of(newItem).pipe(delay(300));
}
deleteItem(itemId: string): Observable<void> {
const item = this.items.find(i => i.id === itemId);
const index = this.items.findIndex(i => i.id === itemId);
if (index > -1) this.items.splice(index, 1);
if (index > -1) {
const subcategoryId = this.items[index].subcategoryId;
this.items.splice(index, 1);
// Check if subcategory still has items
const remainingItems = this.items.filter(i => i.subcategoryId === subcategoryId);
if (remainingItems.length === 0) {
const subcategory = this.findSubcategoryById(subcategoryId);
if (subcategory) {
subcategory.hasItems = false;
}
}
}
return of(void 0).pipe(delay(300));
}