vendredi 6 mai 2016

REST API one-to-many resource relationships best practice

Let's say I have one-to-many relationship here:
Each customer has many credit cards.

And the API endpoints for credit card:
GET /customers/{customer_id}/cards/{card_id}
GET /customers/{customer_id}/cards
POST /customers/{customer_id}/cards
PATCH /customers/{customer_id}/cards/{card_id}
DELETE /customers/{customer_id}/cards/{card_id}

5 methods for card-related operations:
1. Get a card of a customer
2. Get all cards of a customer
3. Create a card for a customer
4. Update 'a card' details of a customer
5. Delete a card of a customer

My API architecture layering uses controller -> service -> repository.
There are:
CustomersController, CustomersService, CustomersRepository, CardsService, CardsRepository.

My question is, where should I put each method for card-related operations and how should the relation between the controller & service & repository layer be like?

Currently I think of it this way:
CustomersController has a CustomersService and a CardsService. CardsService has a CustomersRepository and CardsRepository (injected via constructor as usual). All the 5 methods above reside in CardsService. The purpose of CustomersRepository in CardsService is to retrieve stripe customer id from my DB so I can perform the same operations on Stripe too.

Is this approach correct according to the best practice? Or should I put the 5 methods in CustomersService instead thus not using CardsService at all?

What would be the best solution for this?

Additional info:
- I am using PHP and Laravel framework, and Stripe as the payment platform

Aucun commentaire:

Enregistrer un commentaire