0

I am trying to create a backend server with Aqueduct in dart. I am trying to create a resource controller that will deal with account issues.

routing code is like this;

router
        .route("/$apiEntryPointUrl/account/:operation").link(() => AccountController(context, authServer));

When I try to test routers path variables like this;

   @Operation.post("asdf")
  Future<Response> test() async {
    return Response.ok("testsuccess");
  }

  @Operation.post("operation")
  Future<Response> test5() async {
    return Response.ok("bugsuccess");
  }

  @Operation.post("test3")
  Future<Response> test2() async {
    return Response.ok("testsucces3333s");
  }

When I add Operation.post("operation") as path variable, anything I pass into variable goes to this function. If I do a post request to like /account/tes2 it invokes test5() function. Is this normal? Am I missing some basics?

ZayX
  • 160
  • 1
  • 8

1 Answers1

0

Actually yes, that's normal and intended. If you specify something like this: '/account/:operation' or '/account/:id' in your router then you end up with a kinda wild card, which is in these example cases 'operation' or 'id'. The operator ':' acts as a wild card operation. This means in fact an api call to a route like '/account/tes2' will call the wild card route automatically.

jayjah
  • 1
  • Thank you for your response. My plan was to create a variable definition like :operation and replace it with path for different operations. Like Operation.post('test1') refers to account/test1 and Operation.post('test2') refers to account/test2. I guess its better to create different resource controllers for each? Just wanted to keep same route in one controller. – ZayX Dec 26 '20 at 19:54
  • 1
    No it works in one resource controller. You just have to make your separation inside of the method, not with your router or the methods in your resource controller. To make it happen your method should look like something like this: ``` @Operation.post("operation") Future test5(@Bind.path("operation) String operation) async { // e.G. switch - case statement } ``` – jayjah Dec 27 '20 at 13:37
  • Thank you. I thought of same but code was getting lengthy. I think I better create my methods at the end of class and just use a clean switch case for main method. – ZayX Dec 27 '20 at 18:15