Skip to content

Commit

Permalink
Merge pull request #81 from Eugene600/ask_Me
Browse files Browse the repository at this point in the history
Applied Separation of concerns in Ask Me
  • Loading branch information
IamMuuo authored Sep 24, 2024
2 parents bf2d062 + 247e250 commit 16c112a
Show file tree
Hide file tree
Showing 15 changed files with 552 additions and 452 deletions.
36 changes: 36 additions & 0 deletions lib/tools/ask_me/controllers/ask_me_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:academia/exports/barrel.dart';
import 'package:academia/tools/ask_me/models/services/askme_service.dart.dart';
import 'package:dartz/dartz.dart';
import 'package:get/get.dart';
import 'controllers.dart';

class AskMeController extends GetxController{
final AskMeService _service = AskMeService();
UserController userController = Get.find<UserController>();
QuizSettingsController quizSettingsController = Get.find<QuizSettingsController>();
// final userId = 'MQ4';
/*
For testing purposes as of now, I would recommend you to uncomment the above
while comment the userId gotten from the userController since there is an issue
at the moment on the backend since the backend does not expect the current number
of characters for the user id which is 32 which is a really large number.
This the error I am getting:
Error: {"user_id":["Ensure this field has no more than 12 characters."]}
The above issue is currently being fixed though.
*/

Future<Either<String, dynamic>> fetchQuestions() async{
final result = await _service.fetchQuestions(
userController.user.value?.id,
// userId,
quizSettingsController.fileTitle.value,
quizSettingsController.filePath.value,
quizSettingsController.multipleChoice.value);

return result.fold((l) {
return left(l);
}, (r) {
return right(r);
});
}
}
5 changes: 3 additions & 2 deletions lib/tools/ask_me/controllers/controllers.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'files_controller.dart';
export 'quiz_settings_controller.dart';
export 'files_and_scores_controller.dart';
export 'quiz_settings_controller.dart';
export 'ask_me_controller.dart';
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import 'package:get/get.dart';
import '../models/models.dart';

class FilesController extends GetxController {
class FilesAndScoresController extends GetxController {
RxList<AskMeFiles> files = <AskMeFiles>[].obs;
RxList<AskMeScores> scores = <AskMeScores>[].obs;

@override
void onInit() {
super.onInit();
loadFiles();
loadFilesAndScores();
}

Future<void> loadFiles() async {
Future<void> loadFilesAndScores() async {
// Fetch files
FilesModelHelper().queryAll().then((value) {
files.value = value.map((e) => AskMeFiles.fromJson(e)).toList();
Expand All @@ -24,41 +24,47 @@ class FilesController extends GetxController {
}

// Fetch scores by file ID
Future<void> fetchScoresByFileId(int fileId) async {
final scoreList = await ScoresModelHelper().queryScoresByFileId(fileId);
scores.value = scoreList.map((score) => AskMeScores.fromJson(score)).toList();
}
// Future<void> fetchScoresByFileId(int fileId) async {
// final scoreList = await ScoresModelHelper().queryScoresByFileId(fileId);
// scores.value = scoreList.map((score) => AskMeScores.fromJson(score)).toList();
// }

//Adds Scores
Future<void> addScores(AskMeScores score) async {
Future<bool> addScores(AskMeScores score) async {
final id = await ScoresModelHelper().create(score.toJson());
score.id = id;
scores.add(score);

// Reload files and scores
await loadFiles();
await loadFilesAndScores();
return id == 0 ? false : true;
}

Future<void> addFile(AskMeFiles file) async {
Future<bool> addFile(AskMeFiles file) async {
final id = await FilesModelHelper().create(file.toJson());
file.id = id;
files.add(file);
await loadFiles();
await loadFilesAndScores();
return id == 0 ? false : true;
}
//Update an existing file
Future<void> updateFile(AskMeFiles file) async {
Future<AskMeFiles> updateFile(AskMeFiles file) async {
await FilesModelHelper().update(file.toJson());
files[files.indexWhere((f) => f.id == file.id)] = file;

await loadFiles();
await loadFilesAndScores();
return file;
}

// Delete a file and its associated scores
Future<void> deleteFile(AskMeFiles file) async {
await FilesModelHelper().delete(file.toJson());
files.remove(file);
Future<bool> deleteFile(AskMeFiles file) async {
files.removeWhere((value) => file.id == value.id);
int value = await FilesModelHelper().delete(file.toJson());
// files.remove(file);

await loadFiles();
await loadFilesAndScores();
return value == 0 ? false : true;
}

}

5 changes: 0 additions & 5 deletions lib/tools/ask_me/controllers/quiz_settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class QuizSettingsController extends GetxController {
var filePath = ''.obs;
var questionType = 'Multiple choice'.obs;
var multipleChoice = true.obs;
var selectedTimer = 2.obs;
var minute = 2.obs;
var seconds = 30.obs;

Expand All @@ -20,10 +19,6 @@ class QuizSettingsController extends GetxController {
filePath.value = path;
}

void setSelectedTimer(int timer) {
selectedTimer.value = timer;
}

void setTimer(int min, sec) {
minute.value = min;
seconds.value = sec;
Expand Down
20 changes: 0 additions & 20 deletions lib/tools/ask_me/models/core/multiple_choice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,3 @@ class MultipleChoiceQuestion{
}
}

//Multiple choice Quiz model
class MultipleChoiceQuiz {
List<MultipleChoiceQuestion> questions;

MultipleChoiceQuiz({required this.questions});

factory MultipleChoiceQuiz.fromJson(Map<String, dynamic> json) {
return MultipleChoiceQuiz(
questions: List<MultipleChoiceQuestion>.from(
json['questions'].map((questionJson) => MultipleChoiceQuestion.fromJson(questionJson)),
),
);
}

Map<String, dynamic> toJson() {
return {
'questions': questions.map((question) => question.toJson()).toList(),
};
}
}
21 changes: 21 additions & 0 deletions lib/tools/ask_me/models/core/multiple_choice_quiz.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import '../models.dart';

class MultipleChoiceQuiz {
List<MultipleChoiceQuestion> questions;

MultipleChoiceQuiz({required this.questions});

factory MultipleChoiceQuiz.fromJson(Map<String, dynamic> json) {
return MultipleChoiceQuiz(
questions: List<MultipleChoiceQuestion>.from(
json['questions'].map((questionJson) => MultipleChoiceQuestion.fromJson(questionJson)),
),
);
}

Map<String, dynamic> toJson() {
return {
'questions': questions.map((question) => question.toJson()).toList(),
};
}
}
40 changes: 20 additions & 20 deletions lib/tools/ask_me/models/core/question.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
class Question {
final String question;
final String answer;

const Question({required this.question, required this.answer});

factory Question.fromJson(Map<String, dynamic> json) {
return Question(
question: json['question'],
answer: json['answer'],
);
}

Map<String, dynamic> toJson() {
return {
'question': question,
'answer': answer
};
}
}
// class Question {
// final String question;
// final String answer;

// const Question({required this.question, required this.answer});

// factory Question.fromJson(Map<String, dynamic> json) {
// return Question(
// question: json['question'],
// answer: json['answer'],
// );
// }

// Map<String, dynamic> toJson() {
// return {
// 'question': question,
// 'answer': answer
// };
// }
// }



Expand Down
34 changes: 17 additions & 17 deletions lib/tools/ask_me/models/core/quiz.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import '../models.dart';
// import '../models.dart';

class Quiz {
final List<Question> questions;
// class Quiz {
// final List<Question> questions;

Quiz({required this.questions});
// Quiz({required this.questions});

// Factory method to create a Quiz from JSON
factory Quiz.fromJson(Map<String, dynamic> json) {
var questionsJson = json['questions'] as List;
List<Question> questionList = questionsJson.map((q) => Question.fromJson(q)).toList();
// // Factory method to create a Quiz from JSON
// factory Quiz.fromJson(Map<String, dynamic> json) {
// var questionsJson = json['questions'] as List;
// List<Question> questionList = questionsJson.map((q) => Question.fromJson(q)).toList();

return Quiz(questions: questionList);
}
// return Quiz(questions: questionList);
// }

// Convert the Quiz object to JSON
Map<String, dynamic> toJson() {
return {
'questions': questions.map((q) => q.toJson()).toList(),
};
}
}
// // Convert the Quiz object to JSON
// Map<String, dynamic> toJson() {
// return {
// 'questions': questions.map((q) => q.toJson()).toList(),
// };
// }
// }

20 changes: 0 additions & 20 deletions lib/tools/ask_me/models/core/true_false.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,3 @@ class TrueFalseQuestion {
}
}

//True or False Quiz model
class TrueFalseQuiz {
List<TrueFalseQuestion> questions;

TrueFalseQuiz({required this.questions});

factory TrueFalseQuiz.fromJson(Map<String, dynamic> json) {
return TrueFalseQuiz(
questions: List<TrueFalseQuestion>.from(
json['questions'].map((questionJson) => TrueFalseQuestion.fromJson(questionJson)),
),
);
}

Map<String, dynamic> toJson() {
return {
'questions': questions.map((question) => question.toJson()).toList(),
};
}
}
21 changes: 21 additions & 0 deletions lib/tools/ask_me/models/core/true_or_false_quiz.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import '../models.dart';

class TrueFalseQuiz {
List<TrueFalseQuestion> questions;

TrueFalseQuiz({required this.questions});

factory TrueFalseQuiz.fromJson(Map<String, dynamic> json) {
return TrueFalseQuiz(
questions: List<TrueFalseQuestion>.from(
json['questions'].map((questionJson) => TrueFalseQuestion.fromJson(questionJson)),
),
);
}

Map<String, dynamic> toJson() {
return {
'questions': questions.map((question) => question.toJson()).toList(),
};
}
}
2 changes: 2 additions & 0 deletions lib/tools/ask_me/models/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export 'core/askme_scores.dart';
export 'core/scores_model_helper.dart';
export 'core/multiple_choice.dart';
export 'core/true_false.dart';
export 'core/multiple_choice_quiz.dart';
export 'core/true_or_false_quiz.dart';
export 'services/askme_service.dart.dart';
Loading

0 comments on commit 16c112a

Please sign in to comment.