request detail

This commit is contained in:
Phuoc Nguyen
2025-11-28 15:47:51 +07:00
parent 65f6f825a6
commit 9e7bda32f2
14 changed files with 1320 additions and 718 deletions

View File

@@ -0,0 +1,96 @@
/// Design Request Remote Data Source
///
/// Handles remote API calls for design requests.
library;
import 'package:worker/core/constants/api_constants.dart';
import 'package:worker/core/network/dio_client.dart';
import 'package:worker/features/showrooms/data/models/design_request_model.dart';
/// Design Request Remote Data Source Interface
abstract class DesignRequestRemoteDataSource {
/// Fetch list of design requests from API
Future<List<DesignRequestModel>> getDesignRequests({
int limitStart = 0,
int limitPageLength = 0,
});
/// Fetch detail of a design request by name
Future<DesignRequestModel> getDesignRequestDetail(String name);
}
/// Design Request Remote Data Source Implementation
class DesignRequestRemoteDataSourceImpl implements DesignRequestRemoteDataSource {
const DesignRequestRemoteDataSourceImpl(this._dioClient);
final DioClient _dioClient;
/// Get list of design requests
///
/// Calls: POST /api/method/building_material.building_material.api.design_request.get_list
/// Body: { "limit_start": 0, "limit_page_length": 0 }
/// Returns: List of design requests
@override
Future<List<DesignRequestModel>> getDesignRequests({
int limitStart = 0,
int limitPageLength = 0,
}) async {
try {
final response = await _dioClient.post<Map<String, dynamic>>(
'${ApiConstants.frappeApiMethod}${ApiConstants.getDesignRequestList}',
data: {
'limit_start': limitStart,
'limit_page_length': limitPageLength,
},
);
final data = response.data;
if (data == null) {
throw Exception('No data received from getDesignRequestList API');
}
// API returns: { "message": [...] }
final message = data['message'];
if (message == null) {
throw Exception('No message field in getDesignRequestList response');
}
final List<dynamic> requestsList = message as List<dynamic>;
return requestsList
.map((json) => DesignRequestModel.fromJson(json as Map<String, dynamic>))
.toList();
} catch (e) {
throw Exception('Failed to get design requests: $e');
}
}
/// Get detail of a design request by name
///
/// Calls: POST /api/method/building_material.building_material.api.design_request.get_detail
/// Body: { "name": "ISS-2025-00005" }
/// Returns: Full design request detail with files_list
@override
Future<DesignRequestModel> getDesignRequestDetail(String name) async {
try {
final response = await _dioClient.post<Map<String, dynamic>>(
'${ApiConstants.frappeApiMethod}${ApiConstants.getDesignRequestDetail}',
data: {'name': name},
);
final data = response.data;
if (data == null) {
throw Exception('No data received from getDesignRequestDetail API');
}
// API returns: { "message": {...} }
final message = data['message'];
if (message == null) {
throw Exception('No message field in getDesignRequestDetail response');
}
return DesignRequestModel.fromJson(message as Map<String, dynamic>);
} catch (e) {
throw Exception('Failed to get design request detail: $e');
}
}
}

View File

@@ -0,0 +1,103 @@
/// Data Model: Design Request Model
///
/// JSON serialization model for design request API responses.
library;
import 'package:worker/features/showrooms/data/models/sample_project_model.dart';
import 'package:worker/features/showrooms/domain/entities/design_request.dart';
/// Design Request Model
///
/// Handles JSON serialization/deserialization for API communication.
class DesignRequestModel {
/// Unique request identifier (API: name)
final String name;
/// Request subject/title (API: subject)
final String subject;
/// Request description - may contain HTML (API: description)
final String? description;
/// Deadline date string (API: dateline)
final String? dateline;
/// Status display text (API: status)
final String status;
/// Status color code (API: status_color)
final String statusColor;
/// List of attached files (API: files_list) - available in detail
final List<ProjectFileModel> filesList;
const DesignRequestModel({
required this.name,
required this.subject,
this.description,
this.dateline,
required this.status,
required this.statusColor,
this.filesList = const [],
});
/// Create model from JSON map
factory DesignRequestModel.fromJson(Map<String, dynamic> json) {
final filesListJson = json['files_list'] as List<dynamic>?;
return DesignRequestModel(
name: json['name'] as String? ?? '',
subject: json['subject'] as String? ?? '',
description: json['description'] as String?,
dateline: json['dateline'] as String?,
status: json['status'] as String? ?? '',
statusColor: json['status_color'] as String? ?? '',
filesList: filesListJson != null
? filesListJson
.map((f) => ProjectFileModel.fromJson(f as Map<String, dynamic>))
.toList()
: [],
);
}
/// Convert model to JSON map
Map<String, dynamic> toJson() {
return {
'name': name,
'subject': subject,
'description': description,
'dateline': dateline,
'status': status,
'status_color': statusColor,
'files_list': filesList.map((f) => f.toJson()).toList(),
};
}
/// Convert to domain entity
DesignRequest toEntity() {
return DesignRequest(
id: name,
subject: subject,
description: description,
dateline: dateline,
statusText: status,
statusColor: statusColor,
filesList: filesList.map((f) => f.toEntity()).toList(),
);
}
/// Create model from domain entity
factory DesignRequestModel.fromEntity(DesignRequest entity) {
return DesignRequestModel(
name: entity.id,
subject: entity.subject,
description: entity.description,
dateline: entity.dateline,
status: entity.statusText,
statusColor: entity.statusColor,
filesList: entity.filesList
.map((f) => ProjectFileModel(name: f.id, fileUrl: f.fileUrl))
.toList(),
);
}
}

View File

@@ -5,24 +5,26 @@ library;
import 'package:worker/features/showrooms/domain/entities/sample_project.dart';
/// Sample Project File Model
/// Project File Model
///
/// Handles JSON serialization for file attachments.
class SampleProjectFileModel {
/// Shared model for file attachments used by:
/// - SampleProjectModel (model houses)
/// - DesignRequestModel (design requests)
class ProjectFileModel {
/// Unique file identifier (API: name)
final String name;
/// Full URL to the file (API: file_url)
final String fileUrl;
const SampleProjectFileModel({
const ProjectFileModel({
required this.name,
required this.fileUrl,
});
/// Create model from JSON map
factory SampleProjectFileModel.fromJson(Map<String, dynamic> json) {
return SampleProjectFileModel(
factory ProjectFileModel.fromJson(Map<String, dynamic> json) {
return ProjectFileModel(
name: json['name'] as String? ?? '',
fileUrl: json['file_url'] as String? ?? '',
);
@@ -37,8 +39,8 @@ class SampleProjectFileModel {
}
/// Convert to domain entity
SampleProjectFile toEntity() {
return SampleProjectFile(
ProjectFile toEntity() {
return ProjectFile(
id: name,
fileUrl: fileUrl,
);
@@ -65,7 +67,7 @@ class SampleProjectModel {
final String? thumbnail;
/// List of attached files/images (API: files_list) - available in detail
final List<SampleProjectFileModel> filesList;
final List<ProjectFileModel> filesList;
const SampleProjectModel({
required this.name,
@@ -88,7 +90,7 @@ class SampleProjectModel {
thumbnail: json['thumbnail'] as String?,
filesList: filesListJson != null
? filesListJson
.map((f) => SampleProjectFileModel.fromJson(f as Map<String, dynamic>))
.map((f) => ProjectFileModel.fromJson(f as Map<String, dynamic>))
.toList()
: [],
);
@@ -127,7 +129,7 @@ class SampleProjectModel {
link: entity.viewUrl,
thumbnail: entity.thumbnailUrl,
filesList: entity.filesList
.map((f) => SampleProjectFileModel(name: f.id, fileUrl: f.fileUrl))
.map((f) => ProjectFileModel(name: f.id, fileUrl: f.fileUrl))
.toList(),
);
}

View File

@@ -0,0 +1,41 @@
/// Design Request Repository Implementation
///
/// Implements the design request repository interface.
library;
import 'package:worker/features/showrooms/data/datasources/design_request_remote_datasource.dart';
import 'package:worker/features/showrooms/domain/entities/design_request.dart';
import 'package:worker/features/showrooms/domain/repositories/design_request_repository.dart';
/// Design Request Repository Implementation
class DesignRequestRepositoryImpl implements DesignRequestRepository {
const DesignRequestRepositoryImpl(this._remoteDataSource);
final DesignRequestRemoteDataSource _remoteDataSource;
@override
Future<List<DesignRequest>> getDesignRequests({
int limitStart = 0,
int limitPageLength = 0,
}) async {
try {
final models = await _remoteDataSource.getDesignRequests(
limitStart: limitStart,
limitPageLength: limitPageLength,
);
return models.map((model) => model.toEntity()).toList();
} catch (e) {
rethrow;
}
}
@override
Future<DesignRequest> getDesignRequestDetail(String name) async {
try {
final model = await _remoteDataSource.getDesignRequestDetail(name);
return model.toEntity();
} catch (e) {
rethrow;
}
}
}