Commit 64ba3f63 authored by Leon Tappe's avatar Leon Tappe 🔥
Browse files

update bloc

parent 98486709
......@@ -5,8 +5,8 @@ import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import '../../admins.dart';
import '../network_exception.dart';
import '../models/admin_sort.dart';
import '../network_exception.dart';
import 'admins_events.dart';
import 'admins_state.dart';
......@@ -16,14 +16,11 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
final http.Client _client;
String _token;
AdminsBloc(this._client) {
_log.info('$this started');
}
List<Admin> _admins = [];
@override
AdminsState get initialState => AdminsState.init();
AdminsBloc(this._client) : super(AdminsState.init()) {
_log.info('$this started');
}
@override
Future<void> close() {
......@@ -48,8 +45,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
}
} else if (event is AddAdmin) {
try {
await _postAdmin(event.firstName, event.lastName, event.password,
login: event.login);
await _postAdmin(event.firstName, event.lastName, event.password, login: event.login);
await _getAdmins();
yield AdminsState.result(_admins);
} on NetworkException catch (e) {
......@@ -85,42 +81,76 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
}
}
void onCreateAdmin(String firstName, String lastName, String password,
{String login}) =>
void onCreateAdmin(String firstName, String lastName, String password, {String login}) =>
add(AddAdmin(firstName, lastName, login, password));
void onStart(String token) => add(InitAdmins(token));
void onDeleteAdmin(int id) => add(DeleteAdmin(id));
void onUpdateAdmin(int id, String login, String firstName, String lastName) =>
add(UpdateAdmin(id, login, firstName, lastName));
void onRefresh() => add(RefreshAdmins());
void onSetAdminPassword(int id, String password) =>
add(SetAdminPassword(id, password));
void onSetAdminPassword(int id, String password) => add(SetAdminPassword(id, password));
void onSortAdmins(AdminSortParam param, AdminSortDirection direction) =>
add(SortAdmins(param, direction));
void onRefresh() => add(RefreshAdmins());
void onStart(String token) => add(InitAdmins(token));
@override
void onTransition(Transition<AdminsEvent, AdminsState> transition) {
_log.fine(
'Transition from ${transition.currentState} to ${transition.nextState}');
_log.fine('Transition from ${transition.currentState} to ${transition.nextState}');
super.onTransition(transition);
}
void onUpdateAdmin(int id, String login, String firstName, String lastName) =>
add(UpdateAdmin(id, login, firstName, lastName));
Future<void> _deleteAdmin(int id) async {
_log.finer('_deleteAdmin: $id');
return await _client.delete(
'https://astaprint.upb.de/api/v1/admin/admins/$id',
headers: {
'X-Api-Key': _token,
},
).then((http.Response response) {
_log.finer('_deleteAdmin: ${response.statusCode}');
if (response.statusCode == 205) {
return;
} else {
throw NetworkException('status code other than 205', response.statusCode);
}
});
}
Future<void> _getAdmins() async {
_log.finer('_getAdmins');
return await _client.get(
'https://astaprint.upb.de/api/v1/admin/admins',
headers: {
'X-Api-Key': _token,
'Accept': 'application/json',
},
).then((http.Response response) {
_log.finer('_getAdmins: ${response.body}');
if (response.statusCode == 200) {
_admins = List.from((json.decode(response.body) as Iterable)
.map<Admin>((dynamic admin) => Admin.fromMap(admin as Map<String, dynamic>)));
return;
} else {
throw NetworkException('status code other than 200 received', response.statusCode);
}
});
}
Future<Admin> _postAdmin(String firstName, String lastName, String password,
{String login}) async {
_log.fine('_postAdmin: run for $lastName, $firstName ($login)');
if (login == null && firstName.isNotEmpty && lastName.isNotEmpty) {
login =
'${firstName.substring(0, 1).toLowerCase()}${lastName.toLowerCase()}';
login = '${firstName.substring(0, 1).toLowerCase()}${lastName.toLowerCase()}';
_log.fine('_postAdmin: no login specified, built login "$login"');
}
return await _client.post(
return await _client
.post(
'https://astaprint.uni-paderborn.de/api/v1/admin/admins',
headers: {
'X-Api-Key': _token,
......@@ -133,7 +163,8 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
'password': password,
'owner': false,
}),
).then((http.Response response) {
)
.then((http.Response response) {
_log.finer('_postAdmin: ${response.body}');
if (response.statusCode == 204) {
return;
......@@ -143,45 +174,19 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
});
}
Future<void> _updateAdmin(
int id, String login, String firstName, String lastName) async {
_log.fine('_updateAdmin: run for $id, $lastName, $firstName, $login');
return await _client.put(
'https://astaprint.upb.de/api/v1/admin/admins/$id',
headers: {
'X-Api-Key': _token,
'Accept': 'application/json',
},
body: json.encode({
'first_name': firstName,
'last_name': lastName,
'login': login,
'service': null,
'locked': null,
'expires': null,
}),
).then((http.Response response) {
_log.finer('_updateAdmin: ${response.body}');
if (response.statusCode == 205) {
return;
} else {
throw NetworkException('status code other 205 received', response.statusCode);
}
});
}
Future<void> _setAdminPassword(int id, String password) async {
_log.fine('_setAdminPassword: run for $id, $password');
return await _client.put(
return await _client
.put(
'https://astaprint.upb.de/api/v1/admin/admins/$id/password',
headers: {
'X-Api-Key': _token,
'Accept': 'application/json',
},
body: json.encode(password),
).then((http.Response response) {
)
.then((http.Response response) {
_log.finer('_setAdminPassword: ${response.body}');
if (response.statusCode == 204) {
return;
......@@ -191,44 +196,6 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
});
}
Future<void> _getAdmins() async {
_log.finer('_getAdmins');
return await _client.get(
'https://astaprint.upb.de/api/v1/admin/admins',
headers: {
'X-Api-Key': _token,
'Accept': 'application/json',
},
).then((http.Response response) {
_log.finer('_getAdmins: ${response.body}');
if (response.statusCode == 200) {
_admins = List.from((json.decode(response.body) as Iterable)
.map<Admin>((dynamic admin) =>
Admin.fromMap(admin as Map<String, dynamic>)));
return;
} else {
throw NetworkException('status code other than 200 received', response.statusCode);
}
});
}
Future<void> _deleteAdmin(int id) async {
_log.finer('_deleteAdmin: $id');
return await _client.delete(
'https://astaprint.upb.de/api/v1/admin/admins/$id',
headers: {
'X-Api-Key': _token,
},
).then((http.Response response) {
_log.finer('_deleteAdmin: ${response.statusCode}');
if (response.statusCode == 205) {
return;
} else {
throw NetworkException('status code other than 205', response.statusCode);
}
});
}
void _sortAdmins(AdminSortParam param, AdminSortDirection direction) {
_log.finer('_sortAdmins: $param $direction');
switch (param) {
......@@ -263,4 +230,33 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
break;
}
}
Future<void> _updateAdmin(int id, String login, String firstName, String lastName) async {
_log.fine('_updateAdmin: run for $id, $lastName, $firstName, $login');
return await _client
.put(
'https://astaprint.upb.de/api/v1/admin/admins/$id',
headers: {
'X-Api-Key': _token,
'Accept': 'application/json',
},
body: json.encode({
'first_name': firstName,
'last_name': lastName,
'login': login,
'service': null,
'locked': null,
'expires': null,
}),
)
.then((http.Response response) {
_log.finer('_updateAdmin: ${response.body}');
if (response.statusCode == 205) {
return;
} else {
throw NetworkException('status code other 205 received', response.statusCode);
}
});
}
}
......@@ -14,13 +14,10 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
String _token;
AuthBloc(this.client) {
AuthBloc(this.client) : super(AuthState.unauthorized()) {
log.fine('$this started');
}
@override
AuthState get initialState => AuthState.unauthorized();
@override
Future<void> close() {
log.fine('closing $this');
......
......@@ -16,13 +16,10 @@ class DispatcherBloc extends Bloc<DispatcherEvent, DispatcherState> {
final Client _client;
String _token;
DispatcherBloc(this._client) {
DispatcherBloc(this._client) : super(DispatcherState.init()) {
_l.fine('$this started');
}
@override
DispatcherState get initialState => DispatcherState.init();
@override
Future<void> close() {
_l.fine('closing $this');
......
......@@ -22,13 +22,10 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
List<Transaction> _journal = [];
List<Transaction> _journalFiltered = [];
JournalBloc(this.client) {
JournalBloc(this.client) : super(JournalState.init()) {
log.fine('$this started');
}
@override
JournalState get initialState => JournalState.init();
@override
Future<void> close() {
log.fine('closing $this');
......
......@@ -19,13 +19,10 @@ class JournalTokenBloc extends Bloc<JournalTokenEvent, JournalTokenState> {
String _token;
List<JournalToken> _journalTokens;
JournalTokenBloc(this.client) {
JournalTokenBloc(this.client) : super(JournalTokenState.init()) {
log.fine('$this started');
}
@override
JournalTokenState get initialState => JournalTokenState.init();
@override
Future<void> close() {
log.fine('closing $this');
......
......@@ -17,13 +17,10 @@ class PrintQueueBloc extends Bloc<PrintQueueEvent, PrintQueueState> {
String _token;
int _printer;
PrintQueueBloc(this._client) {
PrintQueueBloc(this._client) : super(PrintQueueState.init()) {
log.fine('$this started');
}
@override
PrintQueueState get initialState => PrintQueueState.init();
@override
Future<void> close() {
log.fine('closing $this with device id $_printer');
......
......@@ -24,13 +24,10 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
List<Printer> _printers;
List<Printer> _printersFiltered = [];
PrintersBloc(this.client) {
PrintersBloc(this.client) : super(PrintersState.init()) {
log.fine('$this started');
}
@override
PrintersState get initialState => PrintersState.init();
@override
Future<void> close() {
log.fine('closing $this');
......
......@@ -20,13 +20,10 @@ class UsersBloc extends Bloc<UsersEvent, UsersState> {
List<User> _users;
List<User> _filteredList;
UsersBloc(this.client) {
UsersBloc(this.client) : super(UsersState.init()) {
log.fine('$this started');
}
@override
UsersState get initialState => UsersState.init();
@override
Future<void> close() {
log.fine('closing $this');
......
......@@ -10,7 +10,7 @@ environment:
dependencies:
logging: ^0.11.4
bloc: ^3.0.0
bloc: ^6.0.0
http: ^0.12.0+4
meta: ^1.1.8
......
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