David Dahan
David Dahan

Reputation: 11172

Choosing the right place to write logic in a client/api/server solution

I'm currently designing a solution with this pretty standard pattern:

As a beginner in these architectures, I'm just asking myself where the logic should go and I'd like to use a simple example case to answer my concerns:

  1. On the mobile client App, a client is asked to subscribe by entering only an email address in a form.
  2. a) If the address is unused, inscription is done (stuff is written on the DB).
  3. b) If the address is used, an error is raised, and the user is asked to try again.

What is the workflow to perform these simple operations?

I'm asking for example how to compare the entered e-mail address in the mobile app with the existing e-mail adresses in my DB:

I have the feeling I am clearly missing something here... Thanks a lot for help.

Upvotes: 1

Views: 126

Answers (2)

Eric Stein
Eric Stein

Reputation: 13682

PUT and POST are designed to be used to create and update resources. The server may or may not have a database behind it. It might use a local filesystem, or it might handle anything in memory. It's none of the client's business. It is certainly common to have business logic on most servers which provide APIs.

Use PUT/POST to send up the email address to the server. The server checks to see if the email address is (a) valid, and (b) allowed. If it fails either check, return a relevant response to the client as documented in the RFC. I would go with 403 Forbidden, which indicates a problem with the data being sent up to the server. Use the entity in the response to detail what the problem was with the request.

Upvotes: 2

Vamsi
Vamsi

Reputation: 9780

I had done similar thing in a angular web app,

I have disabled the submit button, and added a check availability button beside the email field.

I have send the email to server and checked if it already exist and got the result to client, then asked the user to enter an alternate email if not valid or enable the form's submit button

Alternatively

when the user leaves the email field, You can send the email to a service that validates the email, and get the response, and show a message that this email already exist and disable the submit, or enable the submit button otherwise

Upvotes: 0

Related Questions