Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

feat: add Task Comments #136

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/remote/api_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:logging/logging.dart';
import 'package:mentorship_client/failure.dart';
import 'package:mentorship_client/remote/services/auth_service.dart';
import 'package:mentorship_client/remote/services/comment_service.dart';
import 'package:mentorship_client/remote/services/relation_service.dart';
import 'package:mentorship_client/remote/services/task_service.dart';
import 'package:mentorship_client/remote/services/user_service.dart';
Expand All @@ -16,6 +17,7 @@ class ApiManager {
final UserService userService = UserService.create();
final RelationService relationService = RelationService.create();
final TaskService taskService = TaskService.create();
final CommentService commentService = CommentService.create();

ApiManager._internal();

Expand Down
17 changes: 17 additions & 0 deletions lib/remote/models/comment.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:json_annotation/json_annotation.dart';
part 'comment.g.dart';
@JsonSerializable(fieldRename: FieldRename.snake)
class Comment {
final int id;
final int userId;
final int taskId;
final int relationId;
final double creationDate;
final double modificationDate;
final String comment;

Comment(this.id, this.userId, this.taskId, this.relationId, this.creationDate,
this.modificationDate, this.comment);
factory Comment.fromJson(Map<String, dynamic> json) => _$CommentFromJson(json);
Map<String, dynamic> toJson() => _$CommentToJson(this);
}
29 changes: 29 additions & 0 deletions lib/remote/models/comment.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/remote/models/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class User {
);

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
final Map<String, dynamic> data = Map<String, dynamic>();
data['id'] = this.id;
data['username'] = this.username;
data['name'] = this.name;
Expand Down
55 changes: 55 additions & 0 deletions lib/remote/repositories/comment_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import 'package:mentorship_client/remote/api_manager.dart';
import 'package:mentorship_client/remote/models/comment.dart';
import 'package:mentorship_client/remote/requests/comment_request.dart';
import 'package:mentorship_client/remote/responses/custom_response.dart';

class CommentRepository {
static final CommentRepository instance = CommentRepository._internal();

CommentRepository._internal();

Future<List<Comment>> getAllComments(int relationId, int taskId) async {
final body = await ApiManager.callSafely(
() => ApiManager.instance.commentService.getAllComments(relationId, taskId));
List<Comment> comments = [];

for (var json in body) {
comments.add(Comment.fromJson(json));
}

return comments;
}

Future<CustomResponse> newComment(
int relationId, int taskId, CommentRequest commentRequest) async {
final body = await ApiManager.callSafely(() => ApiManager.instance.commentService.newComment(
relationId,
taskId,
commentRequest,
));

return CustomResponse.fromJson(body);
}

Future<CustomResponse> editComment(
int relationId, int taskId, int commentId, CommentRequest commentRequest) async {
final body = await ApiManager.callSafely(() => ApiManager.instance.commentService.editComment(
relationId,
taskId,
commentId,
commentRequest,
));

return CustomResponse.fromJson(body);
}

Future<CustomResponse> deleteCommemt(int relationId, int taskId, int commentId) async {
final body = await ApiManager.callSafely(() => ApiManager.instance.commentService.deleteComment(
relationId,
taskId,
commentId,
));

return CustomResponse.fromJson(body);
}
}
2 changes: 1 addition & 1 deletion lib/remote/requests/change_password.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ChangePassword {
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
final Map<String, dynamic> data = Map<String, dynamic>();
data["current_password"] = this.currentPassword;
data["new_password"] = this.newPassword;
return data;
Expand Down
16 changes: 16 additions & 0 deletions lib/remote/requests/comment_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:flutter/cupertino.dart';

class CommentRequest {
String comment;

CommentRequest({@required this.comment});

factory CommentRequest.fromJson(Map<String, dynamic> json) =>
CommentRequest(comment: json["comment"]);

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data['comment'] = this.comment;
return data;
}
}
2 changes: 1 addition & 1 deletion lib/remote/requests/relation_requests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RelationRequest {
);

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
final Map<String, dynamic> data = Map<String, dynamic>();
data['mentor_id'] = this.mentorId;
data['mentee_id'] = this.menteeId;
data['notes'] = this.notes;
Expand Down
2 changes: 1 addition & 1 deletion lib/remote/requests/task_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TaskRequest {
TaskRequest(description: json["description"]);

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
final Map<String, dynamic> data = Map<String, dynamic>();
data['description'] = this.description;
return data;
}
Expand Down
53 changes: 53 additions & 0 deletions lib/remote/services/comment_service.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions lib/remote/services/comment_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:chopper/chopper.dart';
import 'package:mentorship_client/constants.dart';
import 'package:mentorship_client/remote/auth_interceptor.dart';
import 'package:mentorship_client/remote/requests/comment_request.dart';

part 'comment_service.chopper.dart';

@ChopperApi(baseUrl: "")
abstract class CommentService extends ChopperService {
@Get(path: "mentorship_relation/{relation_id}/task/{task_id}/comments")
Future<Response<List<dynamic>>> getAllComments(
@Path("relation_id") int relationId,
@Path("task_id") int taskId,
);

@Post(path: "mentorship_relation/{relation_id}/task/{task_id}/comment")
Future<Response<Map<String, dynamic>>> newComment(
@Path("relation_id") int relationId,
@Path("task_id") int taskId,
@Body() CommentRequest commentRequest,
);

@Put(path: "mentorship_relation/{relation_id}/task/{task_id}/comment/{comment_id}")
Future<Response<Map<String, dynamic>>> editComment(
@Path("relation_id") int relationId,
@Path("task_id") int taskId,
@Path("comment_id") int commentId,
@Body() CommentRequest commentRequest,
);

@Delete(path: "mentorship_relation/{relation_id}/task/{task_id}/comment/{comment_id}")
Future<Response<Map<String, dynamic>>> deleteComment(
@Path("relation_id") int relationId,
@Path("task_id") int taskId,
@Path("comment_id") int commentId,
);

static CommentService create() {
final client = ChopperClient(
baseUrl: API_URL,
services: [
_$CommentService(),
],
converter: JsonConverter(),
interceptors: [
HttpLoggingInterceptor(),
AuthInterceptor(),
]);

return _$CommentService(client);
}
}
3 changes: 3 additions & 0 deletions lib/screens/comment/bloc/bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'comment_page_bloc.dart';
export 'comment_page_event.dart';
export 'comment_page_state.dart';
67 changes: 67 additions & 0 deletions lib/screens/comment/bloc/comment_page_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:logging/logging.dart';
import 'package:mentorship_client/failure.dart';
import 'package:mentorship_client/remote/models/comment.dart';
import 'package:mentorship_client/remote/repositories/comment_repository.dart';
import 'package:mentorship_client/remote/repositories/relation_repository.dart';
import 'package:mentorship_client/remote/repositories/task_repository.dart';
import 'package:mentorship_client/remote/responses/custom_response.dart';
import 'bloc.dart';

class CommentPageBloc extends Bloc<CommentPageEvent, CommentPageState> {
final RelationRepository relationRepository;
final TaskRepository taskRepository;
final CommentRepository commentRepository;
CommentPageBloc(this.relationRepository, this.taskRepository, this.commentRepository)
: super(CommentPageInitial());

@override
Stream<CommentPageState> mapEventToState(CommentPageEvent event) async* {
if (event is CommentPageShowed) {
yield CommentPageLoading();
try {
List<Comment> comments =
await commentRepository.getAllComments(event.relation.id, event.taskId);
yield CommentPageSuccess(comments);
} on Failure catch (failure) {
Logger.root.severe("CommentPageBloc: ${failure.message}");
yield CommentPageFailure(message: failure.message);
}
}
if (event is CommentCreated) {
try {
CustomResponse response = await commentRepository.newComment(
event.relation.id, event.taskId, event.commentRequest);
var comments = await commentRepository.getAllComments(event.relation.id, event.taskId);
yield CommentPageSuccess(comments, message: response.message);
} on Failure catch (failure) {
Logger.root.severe("CommentPageBloc: ${failure.message}");
yield CommentPageFailure(message: failure.message);
}
}
if (event is CommentEditing) {
try {
CustomResponse response = await commentRepository.editComment(
event.relation.id, event.taskId, event.commentId, event.commentRequest);
var comments = await commentRepository.getAllComments(event.relation.id, event.taskId);
yield CommentPageSuccess(comments, message: response.message);
} on Failure catch (failure) {
Logger.root.severe("CommentPageBloc: ${failure.message}");
yield CommentPageFailure(message: failure.message);
}
}
if (event is CommentDeleted) {
try {
CustomResponse response =
await commentRepository.deleteCommemt(event.relation.id, event.taskId, event.commentId);
var comments = await commentRepository.getAllComments(event.relation.id, event.taskId);
yield CommentPageSuccess(comments, message: response.message);
} on Failure catch (failure) {
Logger.root.severe("CommentPageBloc: ${failure.message}");
yield CommentPageFailure(message: failure.message);
}
}
}
}
Loading