run method

  1. @override
Future<void> run(
  1. HttpRequest request
)
override

Implementation

@override
Future<void> run(HttpRequest request) async {
  for (final method in [
    Method(
      method: Methods.post,
      path: path,
      func: (request) async {
        // get body
        final body = await request.getBody();
        // validate
        validateRegistration(body);
        // find user
        final user = await _serviceUsers.findByEmail(
          email: body['email'].toString(),
        );
        // exception if user found
        if (user != null) {
          throw AppException.unprocessableEntity([
            Validate(
              field: 'email',
              message: 'User with email "${body['email']}" exists',
            )
          ]);
        }
        // create model
        final model = (await _serviceUsers.insert([UserModel.fromJson(body)])).first;
        // set auth cookie
        request.setSessionCookie(model);
        // write data
        request.writeJson(SuccessResponse('Auth successfully'));
      },
    ),
  ]) {
    if (await request.route(method)) {
      return;
    }
  }
}