Commit 73fcf475 authored by Leon Tappe's avatar Leon Tappe 🔥
Browse files

update user model and bloc for better state management

parent 8d08af30
......@@ -4,19 +4,18 @@ export 'transaction.dart';
class User {
final int id;
String name;
String email;
int credit;
//JobOptions defaultOptions;
int card;
int pin;
bool locked;
final String name;
final String email;
final int card;
final int pin;
final bool locked;
final DateTime created;
final DateTime updated;
List<Transaction> journal;
final int credit;
final List<Transaction> journal;
User(
this.id, {
const User({
this.id,
this.name,
this.email,
this.card,
......@@ -24,10 +23,12 @@ class User {
this.locked,
this.created,
this.updated,
this.credit,
this.journal,
});
factory User.fromMap(Map<String, dynamic> map) => User(
map['id'] as int,
id: map['id'] as int,
name: map['name'] as String,
email: map['email'] as String,
card: map['card'] as int,
......@@ -49,6 +50,31 @@ class User {
'updated': updated,
};
User copyWith({
int id,
String name,
String email,
int card,
int pin,
bool locked,
DateTime created,
DateTime updated,
int credit,
List<Transaction> journal,
}) =>
User(
id: id ?? this.id,
name: name ?? this.name,
email: email ?? this.email,
card: card ?? this.card,
pin: pin ?? this.pin,
locked: locked ?? this.locked,
created: created ?? this.created,
updated: updated ?? this.updated,
credit: credit ?? this.credit,
journal: journal ?? this.journal,
);
@override
String toString() => '[User $toMap]';
}
......@@ -180,8 +180,9 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
).then((Response response) {
if (response.statusCode == 200) {
_log.finer(formatResponse(response));
_users[_users.indexWhere((User user) => user.id == userId)].credit =
json.decode(response.body) as int;
_users[_users.indexWhere((User user) => user.id == userId)] =
_users[_users.indexWhere((User user) => user.id == userId)]
.copyWith(credit: json.decode(response.body) as int);
return;
} else {
_log.severe('_getCredit: ${response.body}');
......@@ -204,10 +205,12 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
).then((Response response) {
if (response.statusCode == 200) {
_log.finer(formatResponse(response));
_users[_users.indexWhere((User user) => user.id == userId)].journal =
(json.decode(response.body) as List)
.map<Transaction>((dynamic transaction) => Transaction.fromMap(transaction as Map))
.toList();
_users[_users.indexWhere((User user) => user.id == userId)] =
_users[_users.indexWhere((User user) => user.id == userId)].copyWith(
journal: (json.decode(response.body) as List)
.map<Transaction>(
(dynamic transaction) => Transaction.fromMap(transaction as Map))
.toList());
return;
} else {
_log.severe('_getJournal: ${response.body}');
......@@ -294,7 +297,8 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
.then((Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 205) {
_users[_users.indexWhere((User user) => user.id == userId)].locked = locked;
_users[_users.indexWhere((User user) => user.id == userId)] =
_users[_users.indexWhere((User user) => user.id == userId)].copyWith(locked: locked);
return;
} else {
throw NetworkException('status code other than 205 received', response.statusCode);
......@@ -303,7 +307,7 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
}
Future<void> _putUserEmail(int userId, String email) async {
_log.finer('_puUserEmail: $userId');
_log.finer('_putUserEmail: $userId $email');
return await put(
Uri.parse('https://astaprint.upb.de/api/v1/admin/users/$userId/email'),
headers: {
......@@ -314,6 +318,8 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
).then((Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 205) {
_users[_users.indexWhere((User user) => user.id == userId)] =
_users[_users.indexWhere((User user) => user.id == userId)].copyWith(email: email);
return;
} else {
throw NetworkException('status code other than 205 received', response.statusCode);
......
......@@ -5,7 +5,7 @@ homepage: https://asta.upb.de/
repository: https://git.uni-paderborn.de/asta/blocs_astaprint
issue_tracker: https://git.uni-paderborn.de/asta/blocs_astaprint/-/issues
environment:
sdk: '>=2.6.0 <3.0.0'
sdk: ">=2.6.0 <3.0.0"
dependencies:
logging: ^1.0.2
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment