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