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