Merge pull request #3838 from pangeachat/3831-refactor-modules-to-topics
3831 refactor modules to topics
This commit is contained in:
commit
ad78f3fedd
4 changed files with 80 additions and 41 deletions
|
|
@ -150,40 +150,40 @@ class CoursePlanModel {
|
|||
factory CoursePlanModel.fromCmsDocs(
|
||||
CmsCoursePlan cmsCoursePlan,
|
||||
List<CmsCoursePlanMedia>? cmsCoursePlanMedias,
|
||||
List<CmsCoursePlanTopic>? cmsCoursePlanModules,
|
||||
List<CmsCoursePlanTopicLocation>? cmsCoursePlanModuleLocations,
|
||||
List<CmsCoursePlanTopic>? cmsCoursePlanTopics,
|
||||
List<CmsCoursePlanTopicLocation>? cmsCoursePlanTopicLocations,
|
||||
List<CmsCoursePlanActivity>? cmsCoursePlanActivities,
|
||||
List<CmsCoursePlanActivityMedia>? cmsCoursePlanActivityMedias,
|
||||
) {
|
||||
// fetch topics
|
||||
List<Topic>? topics;
|
||||
if (cmsCoursePlanModules != null) {
|
||||
for (final module in cmsCoursePlanModules) {
|
||||
// select locations of current module
|
||||
List<CmsCoursePlanTopicLocation>? moduleLocations;
|
||||
if (cmsCoursePlanModuleLocations != null) {
|
||||
for (final location in cmsCoursePlanModuleLocations) {
|
||||
if (location.coursePlanTopics.contains(module.id)) {
|
||||
moduleLocations ??= [];
|
||||
moduleLocations.add(location);
|
||||
if (cmsCoursePlanTopics != null) {
|
||||
for (final topic in cmsCoursePlanTopics) {
|
||||
// select locations of current topic
|
||||
List<CmsCoursePlanTopicLocation>? topicLocations;
|
||||
if (cmsCoursePlanTopicLocations != null) {
|
||||
for (final location in cmsCoursePlanTopicLocations) {
|
||||
if (location.coursePlanTopics.contains(topic.id)) {
|
||||
topicLocations ??= [];
|
||||
topicLocations.add(location);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// select activities of current module
|
||||
List<CmsCoursePlanActivity>? moduleActivities;
|
||||
// select activities of current topic
|
||||
List<CmsCoursePlanActivity>? topicActivities;
|
||||
if (cmsCoursePlanActivities != null) {
|
||||
for (final activity in cmsCoursePlanActivities) {
|
||||
if (activity.coursePlanTopics.contains(module.id)) {
|
||||
moduleActivities ??= [];
|
||||
moduleActivities.add(activity);
|
||||
if (activity.coursePlanTopics.contains(topic.id)) {
|
||||
topicActivities ??= [];
|
||||
topicActivities.add(activity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<ActivityPlanModel>? activityPlans;
|
||||
if (moduleActivities != null) {
|
||||
for (final activity in moduleActivities) {
|
||||
if (topicActivities != null) {
|
||||
for (final activity in topicActivities) {
|
||||
// select media of current activity
|
||||
List<CmsCoursePlanActivityMedia>? activityMedias;
|
||||
if (cmsCoursePlanActivityMedias != null) {
|
||||
|
|
@ -238,11 +238,11 @@ class CoursePlanModel {
|
|||
topics ??= [];
|
||||
topics.add(
|
||||
Topic(
|
||||
uuid: module.id,
|
||||
title: module.title,
|
||||
description: module.description,
|
||||
location: moduleLocations != null && moduleLocations.isNotEmpty
|
||||
? moduleLocations.first.name
|
||||
uuid: topic.id,
|
||||
title: topic.title,
|
||||
description: topic.description,
|
||||
location: topicLocations != null && topicLocations.isNotEmpty
|
||||
? topicLocations.first.name
|
||||
: "Any",
|
||||
activities: activityPlans,
|
||||
),
|
||||
|
|
|
|||
|
|
@ -195,19 +195,25 @@ class CoursePlansRepo {
|
|||
static Future<CoursePlanModel> _fromCmsCoursePlan(
|
||||
CmsCoursePlan cmsCoursePlan,
|
||||
) async {
|
||||
final medias = await _getMedia(cmsCoursePlan);
|
||||
final topics = await _getTopics(cmsCoursePlan);
|
||||
final locations = await _getTopicLocations(topics ?? []);
|
||||
final activities = await _getTopicActivities(topics ?? []);
|
||||
final activityMedias = await _getActivityMedia(activities ?? []);
|
||||
return CoursePlanModel.fromCmsDocs(
|
||||
cmsCoursePlan,
|
||||
medias,
|
||||
topics,
|
||||
locations,
|
||||
activities,
|
||||
activityMedias,
|
||||
);
|
||||
try {
|
||||
final medias = await _getMedia(cmsCoursePlan);
|
||||
final topics = await _getTopics(cmsCoursePlan);
|
||||
final locations = await _getTopicLocations(topics ?? []);
|
||||
final activities = await _getTopicActivities(topics ?? []);
|
||||
final activityMedias = await _getActivityMedia(activities ?? []);
|
||||
return CoursePlanModel.fromCmsDocs(
|
||||
cmsCoursePlan,
|
||||
medias,
|
||||
topics,
|
||||
locations,
|
||||
activities,
|
||||
activityMedias,
|
||||
);
|
||||
} catch (e, stack) {
|
||||
print(e);
|
||||
print(stack);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
static Future<List<CmsCoursePlanMedia>?> _getMedia(
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import 'package:fluffychat/pangea/payload_client/polymorphic_relationship.dart';
|
|||
|
||||
/// Represents course plan activity media from the CMS API
|
||||
class CmsCoursePlanActivityMedia {
|
||||
static const String slug = "course-plan-activity-medias";
|
||||
static const String slug = "course-plan-activity-media";
|
||||
|
||||
final String id;
|
||||
final String? alt;
|
||||
|
|
|
|||
|
|
@ -114,16 +114,15 @@ class PayloadClient {
|
|||
Map<String, dynamic>? where,
|
||||
String? sort,
|
||||
}) async {
|
||||
final queryParams = <String, String>{};
|
||||
final Map<String, dynamic> queryParams = {};
|
||||
|
||||
if (page != null) queryParams['page'] = page.toString();
|
||||
if (limit != null) queryParams['limit'] = limit.toString();
|
||||
if (where != null) queryParams['where'] = jsonEncode(where);
|
||||
if (where != null && where.isNotEmpty) queryParams['where'] = where;
|
||||
if (sort != null) queryParams['sort'] = sort;
|
||||
|
||||
final endpoint =
|
||||
'$basePath/$collection${queryParams.isNotEmpty ? '?${Uri(queryParameters: queryParams).query}' : ''}';
|
||||
|
||||
'$basePath/$collection${queryParams.isNotEmpty ? '?${queryStringify(queryParams)}' : ''}';
|
||||
final response = await get(endpoint);
|
||||
final json = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
|
||||
|
|
@ -178,4 +177,38 @@ class PayloadClient {
|
|||
final json = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
return fromJson(json);
|
||||
}
|
||||
|
||||
static String queryStringify(
|
||||
Map<String, dynamic> params, {
|
||||
bool encode = true,
|
||||
}) {
|
||||
final List<String> parts = [];
|
||||
|
||||
void build(String prefix, dynamic value) {
|
||||
if (value == null) return;
|
||||
|
||||
if (value is Map) {
|
||||
value.forEach((k, v) {
|
||||
build('$prefix[$k]', v);
|
||||
});
|
||||
} else if (value is List) {
|
||||
for (var i = 0; i < value.length; i++) {
|
||||
build('$prefix[$i]', value[i]);
|
||||
}
|
||||
} else {
|
||||
final String encodedKey =
|
||||
encode ? Uri.encodeQueryComponent(prefix) : prefix;
|
||||
final String encodedVal = encode
|
||||
? Uri.encodeQueryComponent(value.toString())
|
||||
: value.toString();
|
||||
parts.add('$encodedKey=$encodedVal');
|
||||
}
|
||||
}
|
||||
|
||||
params.forEach((key, value) {
|
||||
build(key, value);
|
||||
});
|
||||
|
||||
return parts.join('&');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue