@@ -2,59 +2,60 @@ import 'package:http/http.dart' as http;
22import 'dart:convert' ;
33
44class ApiService {
5- //main server URL on Fly.io
6- final String baseUrl = 'https://unstuck.fly.dev/api' ;
5+ //main server URL on Fly.io
6+ final String baseUrl = 'https://unstuck.fly.dev/api' ;
77
8- //Check if server is running
9- Future <bool > checkServer () async {
10- try {
11- final response = await http.get (Uri .parse ('$baseUrl /' ));
12- return response.statusCode == 200 ;
13- } catch (e) {
14- return false ;
15- }
16- }
17-
18- // == Login ==
19-
20- Future <bool > registerUser (String username, String password) async {
21- try {
22- final response = await http.post (
23- Uri .parse ('$baseUrl /register' ),
24- headers: {'Content-Type' : 'application/json' },
25- body: jsonEncode ({
26- 'username' : username,
27- 'password' : password,
28- }),
29- );
30- return response.statusCode == 201 ;
31- } catch (e) {
32- print ('Registration error: $e ' );
33- return false ;
34- }
35- }
36-
37- Future <bool > loginUser (String username, String password) async {
38- try {
39- final response = await http.post (
40- Uri .parse ('$baseUrl /login' ),
41-
42- headers: {'Content-Type' : 'application/json' },
43- body: jsonEncode ({
44- 'username' : username,
45- 'password' : password,
46- }),
47- );
48- return response.statusCode == 200 ;
49- } catch (e) {
50- print ('Login error: $e ' );
51- return false ; // Return false if the request fails
52- }
53- }
54-
55- // == Query and Answer ==
8+ //Check if server is running
9+ Future <bool > checkServer () async {
10+ try {
11+ final response = await http.get (Uri .parse ('https://unstuck.fly.dev/' ));
12+ return response.statusCode == 200 ;
13+ } catch (e) {
14+ return false ;
15+ }
16+ }
5617
57- // - Read from the DB -
18+ // == Login ==
19+
20+ Future <bool > registerUser (String username, String password) async {
21+ try {
22+ final response = await http.post (
23+ Uri .parse ('$baseUrl /register' ),
24+ headers: {'Content-Type' : 'application/json' },
25+ body: jsonEncode ({
26+ 'username' : username,
27+ 'password' : password,
28+ }),
29+ );
30+ return response.statusCode == 201 ;
31+ } catch (e) {
32+ print ('Registration error: $e ' );
33+ return false ;
34+ }
35+ }
36+
37+ Future <String ?> loginUser (String username, String password) async {
38+ try {
39+ final response = await http.post (
40+ Uri .parse ('$baseUrl /login' ),
41+ headers: {'Content-Type' : 'application/json' },
42+ body: jsonEncode ({
43+ 'username' : username,
44+ 'password' : password,
45+ }),
46+ );
47+ if (response.statusCode == 200 ) {
48+ final data = jsonDecode (response.body);
49+ return data['user' ]['username' ];
50+ }
51+ return null ;
52+ } catch (e) {
53+ print ('Login error: $e ' );
54+ return null ;
55+ }
56+ }
57+
58+ // == Read from the DB ==
5859
5960 //Get all inquiries from database
6061 Future <List > getInquiries () async {
@@ -83,32 +84,31 @@ class ApiService {
8384 return [];
8485 }
8586 }
86-
87- //- New entry to DB -
87+
88+ //== New entry to DB ==
8889
8990 //POST - Add an inquiry to the database
9091 Future <bool > addInquiry (String title, String body) async {
91- try {
92- final response = await http.post (
93- Uri .parse ('$baseUrl /create-entry' ),
94- headers: {'Content-Type' : 'application/json' },
95- body: jsonEncode ({
96- 'resource' : 'inquiries' ,
97- 'data' : {
98- 'body' : body,
99- 'title' : title,
100- 'chosen_answer_id' : null ,
101- 'is_solved' : false ,
102- },
103- }),
104- );
105-
106- return response.statusCode == 200 ;
107- } catch (e) {
108- print ( 'Error: $ e ' ) ;
109- return false ;
92+ try {
93+ final response = await http.post (
94+ Uri .parse ('$baseUrl /create-entry' ),
95+ headers: {'Content-Type' : 'application/json' },
96+ body: jsonEncode ({
97+ 'resource' : 'inquiries' ,
98+ 'data' : {
99+ 'body' : body,
100+ 'title' : title,
101+ 'chosen_answer_id' : null ,
102+ 'is_solved' : false ,
103+ },
104+ }),
105+ );
106+ return response.statusCode == 200 ;
107+ } catch (e) {
108+ print ( 'Error: $ e ' );
109+ return false ;
110+ }
110111 }
111- }
112112
113113 //POST - Add an answer to the db
114114 Future <bool > addAnswer (int inquiryId, String answerText) async {
@@ -124,12 +124,10 @@ class ApiService {
124124 },
125125 }),
126126 );
127-
128127 return response.statusCode == 200 ;
129128 } catch (e) {
130129 print ('Error sending answer: $e ' );
131130 return false ;
132131 }
133132 }
134-
135133}
0 commit comments