sample project

This commit is contained in:
Phuoc Nguyen
2025-11-28 15:01:51 +07:00
parent ed6cc4cebc
commit 440b474504
11 changed files with 1071 additions and 343 deletions

View File

@@ -0,0 +1,96 @@
/// Sample Project Remote Data Source
///
/// Handles remote API calls for sample/model house projects.
library;
import 'package:worker/core/constants/api_constants.dart';
import 'package:worker/core/network/dio_client.dart';
import 'package:worker/features/showrooms/data/models/sample_project_model.dart';
/// Sample Project Remote Data Source Interface
abstract class SampleProjectRemoteDataSource {
/// Fetch list of sample/model house projects from API
Future<List<SampleProjectModel>> getSampleProjects({
int limitStart = 0,
int limitPageLength = 0,
});
/// Fetch detail of a sample/model house project by name
Future<SampleProjectModel> getSampleProjectDetail(String name);
}
/// Sample Project Remote Data Source Implementation
class SampleProjectRemoteDataSourceImpl implements SampleProjectRemoteDataSource {
const SampleProjectRemoteDataSourceImpl(this._dioClient);
final DioClient _dioClient;
/// Get list of sample projects
///
/// Calls: POST /api/method/building_material.building_material.api.sample_project.get_list
/// Body: { "limit_start": 0, "limit_page_length": 0 }
/// Returns: List of sample projects with 360° view links
@override
Future<List<SampleProjectModel>> getSampleProjects({
int limitStart = 0,
int limitPageLength = 0,
}) async {
try {
final response = await _dioClient.post<Map<String, dynamic>>(
'${ApiConstants.frappeApiMethod}${ApiConstants.getSampleProjectList}',
data: {
'limit_start': limitStart,
'limit_page_length': limitPageLength,
},
);
final data = response.data;
if (data == null) {
throw Exception('No data received from getSampleProjectList API');
}
// API returns: { "message": [...] }
final message = data['message'];
if (message == null) {
throw Exception('No message field in getSampleProjectList response');
}
final List<dynamic> projectsList = message as List<dynamic>;
return projectsList
.map((json) => SampleProjectModel.fromJson(json as Map<String, dynamic>))
.toList();
} catch (e) {
throw Exception('Failed to get sample projects: $e');
}
}
/// Get detail of a sample project by name
///
/// Calls: POST /api/method/building_material.building_material.api.sample_project.get_detail
/// Body: { "name": "PROJ-0001" }
/// Returns: Full project detail with files_list
@override
Future<SampleProjectModel> getSampleProjectDetail(String name) async {
try {
final response = await _dioClient.post<Map<String, dynamic>>(
'${ApiConstants.frappeApiMethod}${ApiConstants.getSampleProjectDetail}',
data: {'name': name},
);
final data = response.data;
if (data == null) {
throw Exception('No data received from getSampleProjectDetail API');
}
// API returns: { "message": {...} }
final message = data['message'];
if (message == null) {
throw Exception('No message field in getSampleProjectDetail response');
}
return SampleProjectModel.fromJson(message as Map<String, dynamic>);
} catch (e) {
throw Exception('Failed to get sample project detail: $e');
}
}
}

View File

@@ -0,0 +1,134 @@
/// Data Model: Sample Project Model
///
/// JSON serialization model for sample project API responses.
library;
import 'package:worker/features/showrooms/domain/entities/sample_project.dart';
/// Sample Project File Model
///
/// Handles JSON serialization for file attachments.
class SampleProjectFileModel {
/// Unique file identifier (API: name)
final String name;
/// Full URL to the file (API: file_url)
final String fileUrl;
const SampleProjectFileModel({
required this.name,
required this.fileUrl,
});
/// Create model from JSON map
factory SampleProjectFileModel.fromJson(Map<String, dynamic> json) {
return SampleProjectFileModel(
name: json['name'] as String? ?? '',
fileUrl: json['file_url'] as String? ?? '',
);
}
/// Convert model to JSON map
Map<String, dynamic> toJson() {
return {
'name': name,
'file_url': fileUrl,
};
}
/// Convert to domain entity
SampleProjectFile toEntity() {
return SampleProjectFile(
id: name,
fileUrl: fileUrl,
);
}
}
/// Sample Project Model
///
/// Handles JSON serialization/deserialization for API communication.
class SampleProjectModel {
/// Unique project identifier (API: name)
final String name;
/// Project display name (API: project_name)
final String projectName;
/// Project description/notes - may contain HTML (API: notes)
final String? notes;
/// URL to 360° view (API: link)
final String? link;
/// Thumbnail image URL (API: thumbnail)
final String? thumbnail;
/// List of attached files/images (API: files_list) - available in detail
final List<SampleProjectFileModel> filesList;
const SampleProjectModel({
required this.name,
required this.projectName,
this.notes,
this.link,
this.thumbnail,
this.filesList = const [],
});
/// Create model from JSON map
factory SampleProjectModel.fromJson(Map<String, dynamic> json) {
final filesListJson = json['files_list'] as List<dynamic>?;
return SampleProjectModel(
name: json['name'] as String? ?? '',
projectName: json['project_name'] as String? ?? '',
notes: json['notes'] as String?,
link: json['link'] as String?,
thumbnail: json['thumbnail'] as String?,
filesList: filesListJson != null
? filesListJson
.map((f) => SampleProjectFileModel.fromJson(f as Map<String, dynamic>))
.toList()
: [],
);
}
/// Convert model to JSON map
Map<String, dynamic> toJson() {
return {
'name': name,
'project_name': projectName,
'notes': notes,
'link': link,
'thumbnail': thumbnail,
'files_list': filesList.map((f) => f.toJson()).toList(),
};
}
/// Convert to domain entity
SampleProject toEntity() {
return SampleProject(
id: name,
projectName: projectName,
description: notes,
viewUrl: link,
thumbnailUrl: thumbnail,
filesList: filesList.map((f) => f.toEntity()).toList(),
);
}
/// Create model from domain entity
factory SampleProjectModel.fromEntity(SampleProject entity) {
return SampleProjectModel(
name: entity.id,
projectName: entity.projectName,
notes: entity.description,
link: entity.viewUrl,
thumbnail: entity.thumbnailUrl,
filesList: entity.filesList
.map((f) => SampleProjectFileModel(name: f.id, fileUrl: f.fileUrl))
.toList(),
);
}
}

View File

@@ -0,0 +1,41 @@
/// Sample Project Repository Implementation
///
/// Implements the sample project repository interface.
library;
import 'package:worker/features/showrooms/data/datasources/sample_project_remote_datasource.dart';
import 'package:worker/features/showrooms/domain/entities/sample_project.dart';
import 'package:worker/features/showrooms/domain/repositories/sample_project_repository.dart';
/// Sample Project Repository Implementation
class SampleProjectRepositoryImpl implements SampleProjectRepository {
const SampleProjectRepositoryImpl(this._remoteDataSource);
final SampleProjectRemoteDataSource _remoteDataSource;
@override
Future<List<SampleProject>> getSampleProjects({
int limitStart = 0,
int limitPageLength = 0,
}) async {
try {
final models = await _remoteDataSource.getSampleProjects(
limitStart: limitStart,
limitPageLength: limitPageLength,
);
return models.map((model) => model.toEntity()).toList();
} catch (e) {
rethrow;
}
}
@override
Future<SampleProject> getSampleProjectDetail(String name) async {
try {
final model = await _remoteDataSource.getSampleProjectDetail(name);
return model.toEntity();
} catch (e) {
rethrow;
}
}
}