0

I have a collection of users. Each user has a cards array, which is composed of the ids of the cards that the user chose.

user.cards = [10, 12, 24, 31]

I want to have the 10 cards that are the most chosen by users.

I have user.service.js and card.service.js, which I guess are DAOs. card.service.js looks something like this (used plain Promises for simplicity).

(function(app) {
    app.CardService = ng.core.Injectable({}).Class({
        constructor: function() {},
        getCards: function() {
            return Promise.resolve(app.cards)
        },
        getCard: function(cardId) {
            return Promise.resolve(app.cards.get(cardId));
        },
        getTop10Cards: function() {
            // How would I go about doing this method?
        }
    })
})(window.app || (window.app = {}));

Logically, I would need the list of all the users, collect their cardIds and aggregate them to find the most used cards. But should CardService use UserService (as an injection)? Or is using a DAO in another DAO the wrong way to go about it? Should I have a separate collection that contains the links between each models (like a joint table)?

Maxime Dupré
  • 5,319
  • 7
  • 38
  • 72

1 Answers1

0

To help myself deal with this client-side problem, I investigated on what the back-end resources would look like. I came up with something similar than "How to handle many-to-many relationships in a RESTful API?".

These are what my API endpoints will potentially look like: /users, /cards, /cards/{cardId}/votes. /cards/{cardId}/votes contains resources that define the relationship between the users and the cards they have chosen.

In the code, I am indeed injecting vote.service.js inside card.service.js. When I GET the cards, I also compose them with their subset of votes. See "angular js model relationships" for more information.

I hope this will help anyone that will come across this question.

Cheers!

Community
  • 1
  • 1
Maxime Dupré
  • 5,319
  • 7
  • 38
  • 72