Skip to content

Commit

Permalink
Merge pull request #41 from avuenja/20-criar-comentários
Browse files Browse the repository at this point in the history
20 criar comentários
  • Loading branch information
avuenja authored Dec 11, 2022
2 parents 1e74d59 + f086a1d commit 15ba0f8
Show file tree
Hide file tree
Showing 12 changed files with 518 additions and 113 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Aplicativo TabNews feito com muito ♥️ e Flutter pela comunidade, para o site
- [x] Meus conteúdos
- [x] Gerencimaneto de conta
- [x] Criação de conta pelo App
- [ ] Resposta dos conteúdos
- [x] Resposta dos conteúdos
- [x] Interação com Tabcoins
- [x] Postagens de conteúdos
- [x] Visualização do perfil de outros usuários
- [x] Favoritos (local database)
Expand Down
43 changes: 43 additions & 0 deletions lib/src/controllers/comment.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:flutter/material.dart';

import 'package:tabnews/src/controllers/app.dart';
import 'package:tabnews/src/interfaces/view_action.dart';
import 'package:tabnews/src/services/content.dart';
import 'package:tabnews/src/services/http_response.dart';

class CommentController {
final ViewAction _view;
final ContentService _contentService = ContentService();

final ValueNotifier<bool> _isLoading = ValueNotifier(false);
ValueNotifier<bool> get isLoading => _isLoading;

CommentController(this._view);

void create(String body, String parentId) async {
if (body.isEmpty) {
_view.onError(message: 'É necessário preencher o campo obrigatório!');

return;
}

_setLoading(true);
final HttpResponse content = await _contentService.postComment(
AppController.auth.value,
parentId,
body,
);

if (content.ok) {
_view.onSuccess();
} else {
_view.onError(message: content.message);
}

_setLoading(false);
}

void _setLoading(bool value) {
_isLoading.value = value;
}
}
28 changes: 28 additions & 0 deletions lib/src/controllers/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';

import 'package:tabnews/src/controllers/app.dart';
import 'package:tabnews/src/interfaces/view_action.dart';
import 'package:tabnews/src/models/content.dart';
import 'package:tabnews/src/services/content.dart';
import 'package:tabnews/src/services/http_response.dart';

Expand Down Expand Up @@ -41,4 +42,31 @@ class ContentController {
void _setLoading(bool value) {
_isLoading.value = value;
}

void getContent(String slug) async {
_setLoading(true);
var contentResp = await _contentService.fetchContent(slug);

if (contentResp.ok) {
if (contentResp.data['parent_id'] != null) {
contentResp.data['parent'] = await _getParentContent(slug);
}

_view.onSuccess(data: contentResp.data);
} else {
_view.onError(message: contentResp.message);
}

_setLoading(false);
}

Future<Content?> _getParentContent(String slug) async {
var parentContent = await _contentService.fetchContentParent(slug);

if (parentContent.ok) {
return Content.fromJson(parentContent.data);
} else {
return null;
}
}
}
4 changes: 4 additions & 0 deletions lib/src/models/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Content {
String? ownerUsername;
int? tabcoins;
int? childrenDeepCount;
Content? parent;

Content({
this.id,
Expand All @@ -31,6 +32,7 @@ class Content {
this.ownerUsername,
this.tabcoins,
this.childrenDeepCount,
this.parent,
});

Content.fromJson(Map<String, dynamic> json) {
Expand All @@ -49,6 +51,7 @@ class Content {
tabcoins = json['tabcoins'];
ownerUsername = json['owner_username'];
childrenDeepCount = json['children_deep_count'];
parent = json['parent'];
}

Map<String, dynamic> toJson() {
Expand All @@ -69,6 +72,7 @@ class Content {
data['tabcoins'] = tabcoins;
data['owner_username'] = ownerUsername;
data['children_deep_count'] = childrenDeepCount;
data['parent'] = parent;

return data;
}
Expand Down
40 changes: 33 additions & 7 deletions lib/src/services/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ class ContentService {
}
}

Future<Content> fetchContent(String slug) async {
Future<HttpResponse> fetchContent(String slug) async {
final response = await http.get(Uri.parse('$apiUrl/$slug'));

if (response.statusCode == 200) {
return Content.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load singular content');
}
return HttpResponse(response.statusCode, response.body);
}

Future<HttpResponse> fetchContentParent(String slug) async {
final response = await http.get(Uri.parse('$apiUrl/$slug/parent'));

return HttpResponse(response.statusCode, response.body);
}

Future<List<Comment>> fetchContentComments(String slug) async {
Expand Down Expand Up @@ -117,7 +119,29 @@ class ContentService {
}),
);

return HttpResponse<Content>(response.statusCode, response.body);
return HttpResponse(response.statusCode, response.body);
}

Future<HttpResponse> postComment(
String token,
String parentId,
String body,
) async {
final response = await http.post(
Uri.parse(apiUrl),
headers: {
'Set-Cookie': 'session_id=$token',
'Cookie': 'session_id=$token',
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode({
'parent_id': parentId,
'body': body,
'status': 'published',
}),
);

return HttpResponse(response.statusCode, response.body);
}

Future<HttpResponse> postTabcoins(String slug, String type) async {
Expand All @@ -133,6 +157,8 @@ class ContentService {
}),
);

AppController.updateUser();

return HttpResponse(response.statusCode, response.body);
}
}
Loading

0 comments on commit 15ba0f8

Please sign in to comment.