Skip to content

Commit

Permalink
API connections, visual changes, functionality improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jumpiniasty committed Jun 23, 2023
1 parent f460cd4 commit 3cc5101
Show file tree
Hide file tree
Showing 13 changed files with 495 additions and 404 deletions.
21 changes: 12 additions & 9 deletions lib/controllers/auth_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,34 @@ import '../models/create_user.dart';

///Used for logging in and signing up a user
class AuthController {
///Used only for logging in
TextEditingController emailCtrl = TextEditingController();

///Used only for logging in
TextEditingController passwordCtrl = TextEditingController();

///Handles logging in
Future<bool> loginUser() async {
final url = '$mainUrl/auth/token/';
final url = "$mainUrl/auth/token/";
final res = await http.post(
Uri.parse(url),
body: {
"email": emailCtrl.text,
"password": passwordCtrl.text,
},
);

if (res.statusCode == 200) {
final loginArr = json.decode(res.body);
const storage = FlutterSecureStorage();
await storage.write(
key: 'access',
value: loginArr['access'],
key: "access",
value: loginArr["access"],
);
await storage.write(
key: 'refresh',
value: loginArr['refresh'],
key: "refresh",
value: loginArr["refresh"],
);

return true;
}
return false;
Expand All @@ -41,20 +44,20 @@ class AuthController {
///Handles the creation of new user
static Future<bool> registerUser(CreateUser user) async {
final url = "$mainUrl/users/";
final dOB = user.dateOfBirth!;

final res = await http.post(
Uri.parse(url),
body: {
"username": user.username,
"email": user.email,
"first_name": user.firstName,
"last_name": user.lastName,
"date_of_birth":
'${user.dateOfBirth.year}-${user.dateOfBirth.month}-${user.dateOfBirth.day}',
"date_of_birth": "${dOB.year}-${dOB.month}-${dOB.day}",
"password": user.password,
"pfp": user.pfp!,
},
);
print("${res.body}");
return res.statusCode == 201;
}
}
28 changes: 22 additions & 6 deletions lib/controllers/party_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,40 @@ import '../utils/consts.dart';
import '../models/party.dart';
import '../models/party_invitation.dart';
import '../models/friend.dart';
import '../utils/ext.dart' show POINTtoLatLng;
import '../utils/ext.dart' show POINTtoLatLng, LatLngToPOINT;

///Used by the regular user to join, leave, accept party requests and more
class PartyController {
///Sends a join request to the [party]
static Future<void> sendJoinRequest(Party party) async {
static Future<bool> sendJoinRequest(Party party) async {
const storage = FlutterSecureStorage();
final token = await storage.read(key: "access");
//TODO implement
final url = "$mainUrl/";
await http.post(
//TODO saving user parameters in storage
final userPublicId = await storage.read(key: "user_publicId");
final url = "$mainUrl/parties/requests/${party.publicId}/";
final res = await http.post(
Uri.parse(url),
body: {},
body: {
"party": {
"owner": {},
"owner_public_id": party.ownerPublicId,
"name": party.name,
"privacy_status": party.privacyStatus,
"description": party.description,
"location": party.location.toPOINT(),
"start_time": party.startTime.toIso8601String(),
"stop_time": party.stopTime.toIso8601String(),
},
"party_public_id": party.publicId,
"sender": {},
"sender_public_id": userPublicId,
},
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer $token",
},
);
return res.statusCode == 201;
}

///Leaves from the provided [party]
Expand Down
3 changes: 3 additions & 0 deletions lib/controllers/party_creator_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ class PartyCreatorController {
];
return parties;
}

///Creates a party
static Future<void> createParty(Party party) async {}
}
39 changes: 35 additions & 4 deletions lib/controllers/user_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import '../utils/consts.dart';
import '../models/user.dart';
import '../models/friend.dart';

///Used for getting information about the user, updating, deleting and searching.
///Used for getting information about the user, updating, deleting and searching
class UserController {
///Returns information about you.
///Returns information about you
static Future<User> me() async {
const storage = FlutterSecureStorage();
final token = await storage.read(key: "access");
Expand Down Expand Up @@ -56,9 +56,40 @@ class UserController {
"Authorization": "Bearer $token",
},
);
return res.statusCode == 204;
if (res.statusCode == 204) {
await storage.delete(key: "access");
await storage.delete(key: "refresh");
return true;
}
return false;
}

///Updates user's account
static Future<void> updateMe() async {}
static Future<bool> updateMe(User user) async {
const storage = FlutterSecureStorage();
final token = await storage.read(key: "access");
final url = "$mainUrl/users/";

final dOB = user.dateOfBirth!;
final Map<String, dynamic> json = {
"first_name": user.firstName,
"last_name": user.lastName,
"date_of_birth": "${dOB.year}-${dOB.month}-${dOB.day}",
"password": user.password,
};
json.removeWhere((k, v) {
return v == null && k != "password" && k != "date_of_birth";
});

final res = await http.patch(
Uri.parse(url),
body: json,
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer $token",
},
);

return res.statusCode == 200;
}
}
16 changes: 8 additions & 8 deletions lib/models/create_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import './friend.dart';

class CreateUser {
final String? publicId;
final String username;
final String email;
final String? username;
final String? email;
final String? firstName;
final String? lastName;
final DateTime dateOfBirth;
final DateTime? dateOfBirth;
final Uri? pfp;
final List<Friend>? friends;
final String password;
final String? password;

const CreateUser({
this.publicId,
required this.username,
required this.email,
this.username,
this.email,
this.firstName,
this.lastName,
required this.dateOfBirth,
this.dateOfBirth,
this.pfp,
this.friends,
required this.password,
this.password,
});
}
4 changes: 2 additions & 2 deletions lib/routes/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class _LoginPageState extends State<LoginPage> {
index: 0,
selectedFieldIndex: selectedFieldIndex,
caption: AppLocalizations.of(context)!.email,
icon: Icons.email_outlined,
icon: Icons.email_rounded,
placeholder: AppLocalizations.of(context)!.emailField,
ctrl: authCtrl.emailCtrl,
keyboardType: TextInputType.emailAddress,
Expand All @@ -92,7 +92,7 @@ class _LoginPageState extends State<LoginPage> {
index: 1,
selectedFieldIndex: selectedFieldIndex,
caption: AppLocalizations.of(context)!.password,
icon: Icons.lock_outline,
icon: Icons.password,
placeholder: AppLocalizations.of(context)!.passwordField,
ctrl: authCtrl.passwordCtrl,
isPassword: true,
Expand Down
Loading

0 comments on commit 3cc5101

Please sign in to comment.