-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#18] Implement getting Survey Detail from API
- Loading branch information
Showing
11 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:survey_flutter/model/response/answer_response.dart'; | ||
import 'package:survey_flutter/utils/string_extension.dart'; | ||
|
||
class AnswerModel { | ||
final String id; | ||
final String type; | ||
final String text; | ||
final int displayOrder; | ||
final String displayType; | ||
|
||
const AnswerModel({ | ||
required this.id, | ||
required this.type, | ||
required this.text, | ||
required this.displayOrder, | ||
required this.displayType, | ||
}); | ||
} | ||
|
||
extension AnswerModelExtension on AnswerResponse { | ||
AnswerModel toAnswerModel() => AnswerModel( | ||
id: id.orEmpty(), | ||
type: type.orEmpty(), | ||
text: text.orEmpty(), | ||
displayOrder: displayOrder ?? 0, | ||
displayType: displayType.orEmpty(), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:survey_flutter/model/answer_model.dart'; | ||
import 'package:survey_flutter/model/response/question_response.dart'; | ||
import 'package:survey_flutter/utils/string_extension.dart'; | ||
|
||
class QuestionModel { | ||
final String id; | ||
final String type; | ||
final String text; | ||
final String displayType; | ||
final List<AnswerModel> answers; | ||
|
||
QuestionModel({ | ||
required this.id, | ||
required this.type, | ||
required this.text, | ||
required this.displayType, | ||
required this.answers, | ||
}); | ||
} | ||
|
||
extension QuestionModelExtension on QuestionResponse { | ||
QuestionModel toQuestionModel() => QuestionModel( | ||
id: id.orEmpty(), | ||
type: type.orEmpty(), | ||
text: text.orEmpty(), | ||
displayType: displayType.orEmpty(), | ||
answers: | ||
answers?.map((answer) => answer.toAnswerModel()).toList() ?? [], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
|
||
part 'answer_response.g.dart'; | ||
|
||
@JsonSerializable() | ||
class AnswerResponse { | ||
@JsonKey(name: 'id') | ||
final String? id; | ||
@JsonKey(name: 'type') | ||
final String? type; | ||
@JsonKey(name: 'text') | ||
final String? text; | ||
@JsonKey(name: 'display_order') | ||
final int? displayOrder; | ||
@JsonKey(name: 'display_type') | ||
final String? displayType; | ||
|
||
AnswerResponse({ | ||
this.id, | ||
this.type, | ||
this.text, | ||
this.displayOrder, | ||
this.displayType, | ||
}); | ||
|
||
factory AnswerResponse.fromJson(Map<String, dynamic> json) => | ||
_$AnswerResponseFromJson(json); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:survey_flutter/model/response/answer_response.dart'; | ||
|
||
part 'question_response.g.dart'; | ||
|
||
@JsonSerializable() | ||
class QuestionResponse { | ||
@JsonKey(name: 'id') | ||
final String? id; | ||
@JsonKey(name: 'type') | ||
final String? type; | ||
@JsonKey(name: 'text') | ||
final String? text; | ||
@JsonKey(name: 'display_type') | ||
final String? displayType; | ||
@JsonKey(name: 'answers') | ||
final List<AnswerResponse>? answers; | ||
|
||
QuestionResponse({ | ||
this.id, | ||
this.type, | ||
this.text, | ||
this.displayType, | ||
this.answers, | ||
}); | ||
|
||
factory QuestionResponse.fromJson(Map<String, dynamic> json) => | ||
_$QuestionResponseFromJson(json); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:survey_flutter/model/response/survey_detail_response.dart'; | ||
|
||
part 'survey_detail_data_response.g.dart'; | ||
|
||
@JsonSerializable() | ||
class SurveyDetailDataResponse { | ||
@JsonKey(name: 'data') | ||
final SurveyDetailResponse? surveyDetailResponse; | ||
|
||
SurveyDetailDataResponse(this.surveyDetailResponse); | ||
|
||
factory SurveyDetailDataResponse.fromJson(Map<String, dynamic> json) => | ||
_$SurveyDetailDataResponseFromJson(json); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:survey_flutter/model/response/question_response.dart'; | ||
|
||
part 'survey_detail_response.g.dart'; | ||
|
||
@JsonSerializable() | ||
class SurveyDetailResponse { | ||
@JsonKey(name: 'id') | ||
final String? id; | ||
@JsonKey(name: 'type') | ||
final String? type; | ||
@JsonKey(name: 'title') | ||
final String? title; | ||
@JsonKey(name: 'description') | ||
final String? description; | ||
@JsonKey(name: 'cover_image_url') | ||
final String? coverImageUrl; | ||
@JsonKey(name: 'survey_type') | ||
final String? surveyType; | ||
@JsonKey(name: 'questions') | ||
final List<QuestionResponse>? questions; | ||
|
||
SurveyDetailResponse({ | ||
this.id, | ||
this.type, | ||
this.title, | ||
this.description, | ||
this.coverImageUrl, | ||
this.surveyType, | ||
this.questions, | ||
}); | ||
|
||
factory SurveyDetailResponse.fromJson(Map<String, dynamic> json) => | ||
_$SurveyDetailResponseFromJson(json); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'package:survey_flutter/model/question_model.dart'; | ||
import 'package:survey_flutter/model/response/survey_detail_response.dart'; | ||
import 'package:survey_flutter/utils/string_extension.dart'; | ||
|
||
class SurveyDetailModel { | ||
final String id; | ||
final String type; | ||
final String title; | ||
final String description; | ||
final String coverImageUrl; | ||
final String surveyType; | ||
final List<QuestionModel> questions; | ||
|
||
const SurveyDetailModel({ | ||
required this.id, | ||
required this.type, | ||
required this.title, | ||
required this.description, | ||
required this.coverImageUrl, | ||
required this.surveyType, | ||
required this.questions, | ||
}); | ||
} | ||
|
||
extension SurveyDetailModelExtension on SurveyDetailResponse { | ||
SurveyDetailModel toSurveyDetailModel() => SurveyDetailModel( | ||
id: id.orEmpty(), | ||
type: type.orEmpty(), | ||
title: type.orEmpty(), | ||
description: description.orEmpty(), | ||
coverImageUrl: coverImageUrl.orEmpty(), | ||
surveyType: surveyType.orEmpty(), | ||
questions: | ||
questions?.map((question) => question.toQuestionModel()).toList() ?? | ||
[], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:survey_flutter/model/survey_detail_model.dart'; | ||
import 'package:survey_flutter/repositories/survey_repository.dart'; | ||
import 'package:survey_flutter/usecases/base/base_use_case.dart'; | ||
|
||
class GetSurveyDetailUseCase extends UseCase<SurveyDetailModel, String> { | ||
final SurveyRepository _surveyRepository; | ||
|
||
GetSurveyDetailUseCase(this._surveyRepository); | ||
|
||
@override | ||
// ignore: avoid_renaming_method_parameters | ||
Future<Result<SurveyDetailModel>> call(String surveyId) async { | ||
try { | ||
final result = await _surveyRepository.getSurveyDetail(surveyId); | ||
return Success(result); | ||
} catch (exception) { | ||
return Failed(UseCaseException(exception)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
extension StringExtension on String? { | ||
String unWrappedOr(String fallback) => this ?? fallback; | ||
String orEmpty() => unWrappedOr(''); | ||
} |