Refactor Space and Tag Models: Removed unused JSON serialization methods from SpaceDetailsModel, ProductAllocation, and Subspace. Updated Tag model to eliminate unnecessary fields. Enhanced UpdateSpaceParam to streamline JSON conversion for subspaces and product allocations, improving data handling during updates.

This commit is contained in:
Faris Armoush
2025-07-09 15:08:49 +03:00
parent 2b8d987c69
commit 5cd083a37b
7 changed files with 34 additions and 87 deletions

View File

@ -40,16 +40,6 @@ class SpaceDetailsModel extends Equatable {
); );
} }
Map<String, dynamic> toJson() {
return {
'uuid': uuid,
'spaceName': spaceName,
'icon': icon,
'productAllocations': productAllocations.map((e) => e.toJson()).toList(),
'subspaces': subspaces.map((e) => e.toJson()).toList(),
};
}
SpaceDetailsModel copyWith({ SpaceDetailsModel copyWith({
String? uuid, String? uuid,
String? spaceName, String? spaceName,
@ -89,14 +79,6 @@ class ProductAllocation extends Equatable {
); );
} }
Map<String, dynamic> toJson() {
return {
'uuid': uuid,
'product': product.toJson(),
'tag': tag.toJson(),
};
}
ProductAllocation copyWith({ ProductAllocation copyWith({
String? uuid, String? uuid,
Product? product, Product? product,
@ -134,14 +116,6 @@ class Subspace extends Equatable {
); );
} }
Map<String, dynamic> toJson() {
return {
'uuid': uuid,
'name': name,
'productAllocations': productAllocations.map((e) => e.toJson()).toList(),
};
}
Subspace copyWith({ Subspace copyWith({
String? uuid, String? uuid,
String? name, String? name,

View File

@ -37,7 +37,7 @@ class _SpaceSubSpacesDialogState extends State<SpaceSubSpacesDialog> {
..._subspaces, ..._subspaces,
Subspace( Subspace(
name: name, name: name,
uuid: const Uuid().v4(), uuid: '${const Uuid().v4()}-NewTag',
productAllocations: const [], productAllocations: const [],
), ),
]; ];

View File

@ -3,41 +3,19 @@ import 'package:equatable/equatable.dart';
class Tag extends Equatable { class Tag extends Equatable {
final String uuid; final String uuid;
final String name; final String name;
final String createdAt;
final String updatedAt;
const Tag({ const Tag({
required this.uuid, required this.uuid,
required this.name, required this.name,
required this.createdAt,
required this.updatedAt,
}); });
factory Tag.empty() => const Tag(
uuid: '',
name: '',
createdAt: '',
updatedAt: '',
);
factory Tag.fromJson(Map<String, dynamic> json) { factory Tag.fromJson(Map<String, dynamic> json) {
return Tag( return Tag(
uuid: json['uuid'] as String, uuid: json['uuid'] as String,
name: json['name'] as String, name: json['name'] as String,
createdAt: json['createdAt'] as String,
updatedAt: json['updatedAt'] as String,
); );
} }
Map<String, dynamic> toJson() {
return {
'uuid': uuid,
'name': name,
'createdAt': createdAt,
'updatedAt': updatedAt,
};
}
@override @override
List<Object?> get props => [uuid, name, createdAt, updatedAt]; List<Object?> get props => [uuid, name];
} }

View File

@ -214,9 +214,12 @@ class _AssignTagsDialogState extends State<AssignTagsDialog> {
for (final product in newProducts) { for (final product in newProducts) {
_space.productAllocations.add( _space.productAllocations.add(
ProductAllocation( ProductAllocation(
uuid: const Uuid().v4(), uuid: '${const Uuid().v4()}-NewProductUuid',
product: product, product: product,
tag: Tag.empty(), tag: Tag(
uuid: '${const Uuid().v4()}-NewTag',
name: '',
),
), ),
); );
} }

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/tags/domain/models/tag.dart'; import 'package:syncrow_web/pages/space_management_v2/modules/tags/domain/models/tag.dart';
import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart'; import 'package:syncrow_web/utils/extension/build_context_x.dart';
import 'package:uuid/uuid.dart';
class ProductTagField extends StatefulWidget { class ProductTagField extends StatefulWidget {
final List<Tag> items; final List<Tag> items;
@ -53,13 +54,8 @@ class _ProductTagFieldState extends State<ProductTagField> {
void _submit(String value) { void _submit(String value) {
final lowerCaseValue = value.toLowerCase(); final lowerCaseValue = value.toLowerCase();
final selectedTag = widget.items.firstWhere( final selectedTag = widget.items.firstWhere(
(tag) => tag.name.toLowerCase() == lowerCaseValue, (e) => e.name.toLowerCase() == lowerCaseValue,
orElse: () => Tag( orElse: () => Tag(uuid: '${const Uuid().v4()}-NewTag', name: value),
name: value,
uuid: '',
createdAt: '',
updatedAt: '',
),
); );
widget.onSelected(selectedTag); widget.onSelected(selectedTag);
_closeDropdown(); _closeDropdown();

View File

@ -19,7 +19,7 @@ class RemoteUpdateSpaceService implements UpdateSpaceService {
final path = await _makeUrl(param); final path = await _makeUrl(param);
await _httpService.put( await _httpService.put(
path: path, path: path,
body: param.space.toJson(), body: param.toJson(),
expectedResponseModel: (data) { expectedResponseModel: (data) {
final response = data as Map<String, dynamic>; final response = data as Map<String, dynamic>;
final isSuccess = response['success'] as bool; final isSuccess = response['success'] as bool;

View File

@ -13,33 +13,29 @@ class UpdateSpaceParam {
return { return {
'spaceName': space.spaceName, 'spaceName': space.spaceName,
'icon': space.icon, 'icon': space.icon,
'subspaces': space.subspaces 'subspaces': space.subspaces.map((e) => e._toJson()).toList(),
.map( 'productAllocations':
(e) => { space.productAllocations.map((e) => e._toJson()).toList(),
'subspaceName': e.name, };
'productAllocations': e.productAllocations }
.map( }
(e) => {
'name': e.tag.name, extension _ProductAllocationToJson on ProductAllocation {
'productUuid': e.product.uuid, Map<String, dynamic> _toJson() {
'uuid': e.uuid, final isNewTag = tag.uuid.isEmpty;
}, return <String, dynamic>{
) if (isNewTag) 'tagName': tag.name else 'tagUuid': tag.uuid,
.toList(), 'productUuid': product.uuid,
'uuid': e.uuid, };
}, }
) }
.toList(),
'productAllocations': space.productAllocations extension _SubspaceToJson on Subspace {
.map( Map<String, dynamic> _toJson() {
(e) => { final isNewSubspace = uuid.endsWith('-NewTag');
'tagName': e.tag.name, return <String, dynamic>{
'tagUuid': e.tag.uuid, if (isNewSubspace) 'subspaceName': name else 'uuid': uuid,
'productUuid': e.product.uuid, 'productAllocations': productAllocations.map((e) => e._toJson()).toList(),
},
)
.toList(),
'spaceModelUuid': space.uuid,
}; };
} }
} }