pollmelo / backberry

fruity backend for pollmelo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Create and register PollDataTransferObjectConverter and PollDataTransferObject

martincodes-de opened this issue · comments

At this moment we want to return a poll at any endpoints with the calculated up- and downvotes like this object

{
    "id": 1,
    "name": "Queen left off, quite out of his great wig.' The judge, by the prisoner to--to somebody.' 'It must be kind to them,' thought Alice, 'or perhaps they won't walk the way out of his great wig.' The.",
    "description": "For instance, suppose it doesn't matter a bit,' said the March Hare. 'He denies it,' said Alice, a little bottle that stood near. The three soldiers wandered about in the window, she suddenly spread.",
    "endDate": "2023-03-07",
    "phase": 1,
   "upvotes": 81,
    "downvotes": 67,
    "created_at": "2023-01-31T21:41:16.000000Z",
    "updated_at": "2023-01-31T21:41:16.000000Z"
},

The problem is that we have to calculate and build the response at any endpoint manual. We don't like manual in our team.

To have one consistent representation of a poll in our API and have one place to generate that object for a poll that we only call in our endpoints, we need a PollDataTransferObject and PollDataTransferObjectConverter. (keyword: SOLID principles - single responsibility)

PollDataTransferObject

Create a PollDataTransferObject in app/DTOs/HttpApi/PollDataTransferObject.php with all attributes from the json above with correct attributes + types (not PHPdoc) and as final & readonly class. Pay attention to namespaces.

PollDataTransferObjectConverter

Create a PollDataTransferObjectConverter that has a public, non-static method convert() that takes 1 Models/Poll and returns a PollDataTransferObject. The location should be app/Converter/PollDataTransferObjectConverter.php. Inside the convert-method, do the movement from model to data transfer object. Create some unit tests. After that, register the converter as a service to do dependency injection. Without that, we can't use the dependency injection from laravel.

I assigned you both (@NiklasBGit @emsbe) because I think that would be a good beginner task for pairprogramming.

Would you construct the PollDataTransferObject like in #33 ? Meaning that this Object would expect a Poll, and two ints when an instance is created?

The converter method expects only 1 poll model and creates + returns the PollDataTransferObject. The Dto has only read-only properties and ofc only public attributes and no methods because it's a datatransferobject

The converter method expects only 1 poll model and creates + returns the PollDataTransferObject. The Dto has only read-only properties and ofc only public attributes and no methods because it's a datatransferobject

I disagree. The converter shouldn't do a db-request

i agree your disagreement