Commit 5de65327 authored by Leon Tappe's avatar Leon Tappe 🔥
Browse files

update admin bloc for more data

parent 58b6ec90
...@@ -37,7 +37,14 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> { ...@@ -37,7 +37,14 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
on<AddAdmin>((AddAdmin event, Emitter<AdminsState> emit) async { on<AddAdmin>((AddAdmin event, Emitter<AdminsState> emit) async {
try { try {
await _postAdmin(event.firstName, event.lastName, event.password, login: event.login); await _postAdmin(
event.firstName,
event.lastName,
event.password,
login: event.login,
expires: event.expires,
service: event.service,
);
await _getAdmins(); await _getAdmins();
emit(AdminsState.result(_admins)); emit(AdminsState.result(_admins));
} on NetworkException catch (e) { } on NetworkException catch (e) {
...@@ -57,7 +64,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> { ...@@ -57,7 +64,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
on<UpdateAdmin>((UpdateAdmin event, Emitter<AdminsState> emit) async { on<UpdateAdmin>((UpdateAdmin event, Emitter<AdminsState> emit) async {
try { try {
await _updateAdmin(event.id, event.login, event.firstName, event.lastName); await _updateAdmin(event.admin);
await _getAdmins(); await _getAdmins();
emit(AdminsState.result(_admins)); emit(AdminsState.result(_admins));
} on NetworkException catch (e) { } on NetworkException catch (e) {
...@@ -87,8 +94,14 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> { ...@@ -87,8 +94,14 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
return super.close(); return super.close();
} }
void onCreateAdmin(String firstName, String lastName, String password, {String login}) => void onCreateAdmin(
add(AddAdmin(firstName, lastName, login, password)); {String firstName,
String lastName,
String password,
String login,
bool service,
DateTime expires}) =>
add(AddAdmin(firstName, lastName, login, password, service, expires));
void onDeleteAdmin(int id) => add(DeleteAdmin(id)); void onDeleteAdmin(int id) => add(DeleteAdmin(id));
...@@ -107,8 +120,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> { ...@@ -107,8 +120,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
super.onTransition(transition); super.onTransition(transition);
} }
void onUpdateAdmin(int id, String login, String firstName, String lastName) => void onUpdateAdmin(Admin admin) => add(UpdateAdmin(admin));
add(UpdateAdmin(id, login, firstName, lastName));
Future<void> _deleteAdmin(int id) async { Future<void> _deleteAdmin(int id) async {
_log.finer('_deleteAdmin: $id'); _log.finer('_deleteAdmin: $id');
...@@ -148,7 +160,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> { ...@@ -148,7 +160,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
} }
Future<Admin> _postAdmin(String firstName, String lastName, String password, Future<Admin> _postAdmin(String firstName, String lastName, String password,
{String login}) async { {String login, bool service = true, DateTime expires}) async {
_log.fine('_postAdmin: run for $lastName, $firstName ($login)'); _log.fine('_postAdmin: run for $lastName, $firstName ($login)');
if (login == null && firstName.isNotEmpty && lastName.isNotEmpty) { if (login == null && firstName.isNotEmpty && lastName.isNotEmpty) {
login = '${firstName.substring(0, 1).toLowerCase()}${lastName.toLowerCase()}'; login = '${firstName.substring(0, 1).toLowerCase()}${lastName.toLowerCase()}';
...@@ -167,7 +179,8 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> { ...@@ -167,7 +179,8 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
'last_name': lastName, 'last_name': lastName,
'login': login, 'login': login,
'password': password, 'password': password,
'owner': false, 'service': service,
if (expires != null) 'expires': expires.millisecondsSinceEpoch ~/ 1000,
}), }),
) )
.then((http.Response response) { .then((http.Response response) {
...@@ -175,6 +188,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> { ...@@ -175,6 +188,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
if (response.statusCode == 204) { if (response.statusCode == 204) {
return; return;
} else { } else {
_log.severe(response.body);
throw NetworkException('status code other than 204 received', response.statusCode); throw NetworkException('status code other than 204 received', response.statusCode);
} }
}); });
...@@ -237,30 +251,28 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> { ...@@ -237,30 +251,28 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
} }
} }
Future<void> _updateAdmin(int id, String login, String firstName, String lastName) async { Future<void> _updateAdmin(Admin admin) async {
_log.fine('_updateAdmin: run for $id, $lastName, $firstName, $login'); _log.fine('_updateAdmin: run for ${admin.toMap}');
final data = admin.toMap;
data.remove('id');
data.removeWhere((key, value) => value == null);
return await _client return await _client
.put( .put(
Uri.parse('https://astaprint.upb.de/api/v1/admin/admins/$id'), Uri.parse('https://astaprint.upb.de/api/v1/admin/admins/${admin.id}'),
headers: { headers: {
'X-Api-Key': _token, 'X-Api-Key': _token,
'Accept': 'application/json', 'Accept': 'application/json',
}, },
body: json.encode({ body: json.encode(data),
'first_name': firstName,
'last_name': lastName,
'login': login,
'service': null,
'locked': null,
'expires': null,
}),
) )
.then((http.Response response) { .then((http.Response response) {
_log.finer(formatResponse(response)); _log.finer(formatResponse(response));
if (response.statusCode == 205) { if (response.statusCode == 205) {
return; return;
} else { } else {
_log.severe(response.body);
_log.severe(json.encode(data));
throw NetworkException('status code other 205 received', response.statusCode); throw NetworkException('status code other 205 received', response.statusCode);
} }
}); });
......
import '../models/admin.dart';
import '../models/enums.dart'; import '../models/enums.dart';
class AddAdmin extends AdminsEvent { class AddAdmin extends AdminsEvent {
...@@ -5,14 +6,25 @@ class AddAdmin extends AdminsEvent { ...@@ -5,14 +6,25 @@ class AddAdmin extends AdminsEvent {
final String lastName; final String lastName;
final String login; final String login;
final String password; final String password;
final bool service;
AddAdmin(this.firstName, this.lastName, this.login, this.password); final DateTime expires;
AddAdmin(
this.firstName,
this.lastName,
this.login,
this.password,
this.service,
this.expires,
);
Map<String, dynamic> get toMap => { Map<String, dynamic> get toMap => {
'first_name': firstName, 'first_name': firstName,
'last_name': lastName, 'last_name': lastName,
'login': login, 'login': login,
'password': password, 'password': password,
'service': service,
'expires': expires.millisecondsSinceEpoch ~/ 1000,
}; };
@override @override
...@@ -73,19 +85,11 @@ class SortAdmins extends AdminsEvent { ...@@ -73,19 +85,11 @@ class SortAdmins extends AdminsEvent {
} }
class UpdateAdmin extends AdminsEvent { class UpdateAdmin extends AdminsEvent {
final int id; final Admin admin;
final String login;
final String firstName;
final String lastName;
UpdateAdmin(this.id, this.login, this.firstName, this.lastName); UpdateAdmin(this.admin);
Map<String, dynamic> get toMap => { Map<String, dynamic> get toMap => {'admin': admin.toMap};
'id': id,
'login': login,
'first_name': firstName,
'last_name': lastName,
};
@override @override
String toString() => '[UpdateAdmin $toMap]'; String toString() => '[UpdateAdmin $toMap]';
......
class Admin { class Admin {
final int id; final int id;
final String firstName; final String firstName;
final String lastName; final String lastName;
final String login; final String login;
final bool locked;
final bool service;
final DateTime expires;
Admin({ Admin({
this.id, this.id,
this.firstName, this.firstName,
this.lastName, this.lastName,
this.login, this.login,
this.locked,
this.service,
this.expires,
}); });
factory Admin.fromMap(Map<String, dynamic> map) { factory Admin.fromMap(Map<String, dynamic> map) {
...@@ -17,6 +24,11 @@ class Admin { ...@@ -17,6 +24,11 @@ class Admin {
firstName: map['first_name'] as String, firstName: map['first_name'] as String,
lastName: map['last_name'] as String, lastName: map['last_name'] as String,
login: map['login'] as String, login: map['login'] as String,
locked: map['locked'] as bool,
service: map['service'] as bool,
expires: map['expires'] != null
? DateTime.fromMillisecondsSinceEpoch((map['expires'] as int) * 1000)
: null,
); );
} }
...@@ -25,8 +37,29 @@ class Admin { ...@@ -25,8 +37,29 @@ class Admin {
'first_name': firstName, 'first_name': firstName,
'last_name': lastName, 'last_name': lastName,
'login': login, 'login': login,
'locked': locked,
'expires': expires.millisecondsSinceEpoch ~/ 1000,
'service': service,
}; };
Admin copyWith({
String firstName,
String lastName,
String login,
bool locked,
bool service,
DateTime expires,
}) =>
Admin(
id: id,
firstName: firstName ?? this.firstName,
lastName: lastName ?? this.lastName,
login: login ?? this.login,
locked: locked ?? this.locked,
service: service ?? this.service,
expires: expires ?? this.expires,
);
@override @override
String toString() => '[Admin $toMap]'; String toString() => '[Admin $toMap]';
} }
import 'dart:async'; import 'dart:async';
import 'package:blocs_astaprint/src/admins/admins_bloc.dart'; import 'package:blocs_astaprint/admins.dart';
import 'package:blocs_astaprint/src/admins/admins_state.dart';
import 'package:http/http.dart'; import 'package:http/http.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
...@@ -37,7 +36,8 @@ void main() { ...@@ -37,7 +36,8 @@ void main() {
listener.cancel(); listener.cancel();
} }
})); }));
bloc.onCreateAdmin('test1', 'test1', 'test123', login: 'test123'); bloc.onCreateAdmin(
firstName: 'test1', lastName: 'test1', password: 'test123', login: 'test123');
}); });
test('get admins', () { test('get admins', () {
...@@ -82,7 +82,8 @@ void main() { ...@@ -82,7 +82,8 @@ void main() {
listener.cancel(); listener.cancel();
} }
})); }));
bloc.onUpdateAdmin(newAdminId, 'emuster', 'Erika', 'Musterfrau'); bloc.onUpdateAdmin(
Admin(id: newAdminId, login: 'emuster', firstName: 'Erika', lastName: 'Musterfrau'));
}); });
test('delete admin', () { test('delete admin', () {
......
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