Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
CreateRaid connected from controller (#88)
Browse files Browse the repository at this point in the history
* CreateRaid connected from controller

* Docu
  • Loading branch information
rmuhamed committed Dec 24, 2019
1 parent 658305b commit a99df70
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 95 deletions.
Binary file modified doc/build/doctrees/application.doctree
Binary file not shown.
Binary file modified doc/build/doctrees/domain.doctree
Binary file not shown.
Binary file modified doc/build/doctrees/environment.pickle
Binary file not shown.
Binary file modified doc/build/doctrees/index.doctree
Binary file not shown.
Binary file modified doc/build/doctrees/infrastructure.doctree
Binary file not shown.
Binary file modified doc/build/doctrees/usage.doctree
Binary file not shown.
2 changes: 1 addition & 1 deletion doc/build/html/application.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ <h2>Outlaw<a class="headerlink" href="#outlaw" title="Permalink to this headline
</dl>
<dl class="method">
<dt id="thesheriff.application.outlaw.create_gang.CreateGang.execute">
<code class="sig-name descname">execute</code><span class="sig-paren">(</span><em class="sig-param">owner_id: int</em>, <em class="sig-param">name: str</em><span class="sig-paren">)</span> &#x2192; thesheriff.domain.gang.gang.Gang<a class="headerlink" href="#thesheriff.application.outlaw.create_gang.CreateGang.execute" title="Permalink to this definition"></a></dt>
<code class="sig-name descname">execute</code><span class="sig-paren">(</span><em class="sig-param">request: thesheriff.application.gang.request.create_gang_request.CreateGangRequest</em><span class="sig-paren">)</span> &#x2192; thesheriff.domain.gang.gang.Gang<a class="headerlink" href="#thesheriff.application.outlaw.create_gang.CreateGang.execute" title="Permalink to this definition"></a></dt>
<dd><p>execute is the actual action of the Raid rating use case.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
Expand Down
4 changes: 2 additions & 2 deletions doc/build/html/domain.html
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ <h2>Outlaw<a class="headerlink" href="#outlaw" title="Permalink to this headline
<h2>Raid<a class="headerlink" href="#raid" title="Permalink to this headline"></a></h2>
<span class="target" id="module-thesheriff.domain.raid.raid"><span id="id3"></span></span><dl class="class">
<dt id="thesheriff.domain.raid.raid.Raid">
<em class="property">class </em><code class="sig-prename descclassname">thesheriff.domain.raid.raid.</code><code class="sig-name descname">Raid</code><span class="sig-paren">(</span><em class="sig-param">name: str</em>, <em class="sig-param">location: str</em>, <em class="sig-param">sheriff: thesheriff.domain.outlaw.sheriff.Sheriff</em>, <em class="sig-param">gang: thesheriff.domain.gang.gang.Gang</em>, <em class="sig-param">date: datetime.datetime</em>, <em class="sig-param">raid_id: Optional[int] = None</em><span class="sig-paren">)</span><a class="headerlink" href="#thesheriff.domain.raid.raid.Raid" title="Permalink to this definition"></a></dt>
<em class="property">class </em><code class="sig-prename descclassname">thesheriff.domain.raid.raid.</code><code class="sig-name descname">Raid</code><span class="sig-paren">(</span><em class="sig-param">name: str</em>, <em class="sig-param">location: str</em>, <em class="sig-param">sheriff: thesheriff.domain.outlaw.sheriff.Sheriff</em>, <em class="sig-param">gang: thesheriff.domain.gang.gang.Gang</em>, <em class="sig-param">date: datetime.datetime</em>, <em class="sig-param">raid_id: Optional[int] = None</em>, <em class="sig-param">outlaws: Optional[thesheriff.domain.outlaw.outlaw.Outlaw] = []</em><span class="sig-paren">)</span><a class="headerlink" href="#thesheriff.domain.raid.raid.Raid" title="Permalink to this definition"></a></dt>
<dd><p>Class Raid represents the Raid entity.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
Expand Down Expand Up @@ -254,7 +254,7 @@ <h2>Raid<a class="headerlink" href="#raid" title="Permalink to this headline">¶

<span class="target" id="module-thesheriff.domain.raid.raid_factory"><span id="raid-factory"></span></span><dl class="class">
<dt id="thesheriff.domain.raid.raid_factory.RaidFactory">
<em class="property">class </em><code class="sig-prename descclassname">thesheriff.domain.raid.raid_factory.</code><code class="sig-name descname">RaidFactory</code><span class="sig-paren">(</span><em class="sig-param">name: str</em>, <em class="sig-param">location: str</em>, <em class="sig-param">sheriff: thesheriff.domain.outlaw.sheriff.Sheriff</em>, <em class="sig-param">gang: thesheriff.domain.gang.gang.Gang</em>, <em class="sig-param">date: datetime.datetime</em>, <em class="sig-param">raid_id: Optional[int] = None</em><span class="sig-paren">)</span><a class="headerlink" href="#thesheriff.domain.raid.raid_factory.RaidFactory" title="Permalink to this definition"></a></dt>
<em class="property">class </em><code class="sig-prename descclassname">thesheriff.domain.raid.raid_factory.</code><code class="sig-name descname">RaidFactory</code><span class="sig-paren">(</span><em class="sig-param">name: str</em>, <em class="sig-param">location: str</em>, <em class="sig-param">sheriff: thesheriff.domain.outlaw.sheriff.Sheriff</em>, <em class="sig-param">gang: thesheriff.domain.gang.gang.Gang</em>, <em class="sig-param">date: datetime.datetime</em>, <em class="sig-param">raid_id: Optional[int] = None</em>, <em class="sig-param">outlaws: Optional[thesheriff.domain.outlaw.outlaw.Outlaw] = []</em><span class="sig-paren">)</span><a class="headerlink" href="#thesheriff.domain.raid.raid_factory.RaidFactory" title="Permalink to this definition"></a></dt>
<dd><p>Class RaidFactory produces Raids.</p>
<dl class="method">
<dt id="thesheriff.domain.raid.raid_factory.RaidFactory.create">
Expand Down
54 changes: 37 additions & 17 deletions doc/build/html/infrastructure.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,6 @@ <h3>thesheriff.infrastructure.controllers.outlaw_controller<a class="headerlink"
</pre></div>
</div>
</li>
<li><p><em>/&lt;prefix&gt;/outlaw/&lt;int:outlaw_id&gt;/raid/&lt;int:raid_id&gt;/</em> (POST)</p>
<p><strong>Request Example:</strong></p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> curl localhost:5000/api/&lt;version&gt;/outlaw/1/raid/1 <span class="se">\</span>
-X POST --data @examples/json/rate_raid.json <span class="se">\</span>
-H <span class="s1">&#39;Content-Type: application/json&#39;</span>
</pre></div>
</div>
<p><strong>Response Example:</strong></p>
<div class="highlight-json notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="nt">&quot;status&quot;</span><span class="p">:</span> <span class="mi">201</span><span class="p">,</span>
<span class="nt">&quot;message&quot;</span><span class="p">:</span> <span class="s2">&quot;raid rated successfully&quot;</span>
<span class="p">}</span>
</pre></div>
</div>
</li>
</ul>
</dd></dl>

Expand All @@ -202,11 +187,14 @@ <h3>thesheriff.infrastructure.controllers.raid_controller<a class="headerlink" h
<p>This module implements the RESTful part of the Raid use cases.</p>
<dl class="function">
<dt id="thesheriff.infrastructure.controllers.raid_controller.raid_controller">
<code class="sig-prename descclassname">thesheriff.infrastructure.controllers.raid_controller.</code><code class="sig-name descname">raid_controller</code><span class="sig-paren">(</span><em class="sig-param">rate_raid: thesheriff.application.outlaw.rate_raid.RateRaid</em><span class="sig-paren">)</span> &#x2192; flask.blueprints.Blueprint<a class="headerlink" href="#thesheriff.infrastructure.controllers.raid_controller.raid_controller" title="Permalink to this definition"></a></dt>
<code class="sig-prename descclassname">thesheriff.infrastructure.controllers.raid_controller.</code><code class="sig-name descname">raid_controller</code><span class="sig-paren">(</span><em class="sig-param">create_raid: thesheriff.application.raid.create_raid.CreateRaid</em>, <em class="sig-param">rate_raid: thesheriff.application.outlaw.rate_raid.RateRaid</em><span class="sig-paren">)</span> &#x2192; flask.blueprints.Blueprint<a class="headerlink" href="#thesheriff.infrastructure.controllers.raid_controller.raid_controller" title="Permalink to this definition"></a></dt>
<dd><p>raid_controller holds the blueprint for all raid routes.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>rate_raid</strong> (<a class="reference internal" href="application.html#thesheriff.application.outlaw.rate_raid.RateRaid" title="thesheriff.application.outlaw.rate_raid.RateRaid"><em>RateRaid</em></a>) – RateRaid use case implementation.</p>
<dd class="field-odd"><ul class="simple">
<li><p><strong>create_raid</strong> (<em>CreateRaid</em>) – CreateRaid use case implementation.</p></li>
<li><p><strong>rate_raid</strong> (<a class="reference internal" href="application.html#thesheriff.application.outlaw.rate_raid.RateRaid" title="thesheriff.application.outlaw.rate_raid.RateRaid"><em>RateRaid</em></a>) – RateRaid use case implementation.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>Flask Blueprint.</p>
Expand All @@ -216,6 +204,38 @@ <h3>thesheriff.infrastructure.controllers.raid_controller<a class="headerlink" h
</dd>
</dl>
<p>Implements the following routes:</p>
<ul>
<li><p><em>/&lt;prefix&gt;/raid/</em> (POST)</p>
<p><strong>Request Example:</strong></p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> curl localhost:5000/api/&lt;version&gt;/raid/ <span class="se">\</span>
-X POST --data @examples/json/create_raid.json <span class="se">\</span>
-H <span class="s1">&#39;Content-Type: application/json&#39;</span>
</pre></div>
</div>
<p><strong>Response Example:</strong></p>
<div class="highlight-json notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="nt">&quot;status&quot;</span><span class="p">:</span> <span class="mi">201</span><span class="p">,</span>
<span class="nt">&quot;message&quot;</span><span class="p">:</span> <span class="s2">&quot;Raid added successfully&quot;</span>
<span class="p">}</span>
</pre></div>
</div>
</li>
<li><p><em>/&lt;prefix&gt;/outlaw/&lt;int:outlaw_id&gt;/raid/&lt;int:raid_id&gt;/</em> (PUT)</p>
<p><strong>Request Example:</strong></p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> curl localhost:5000/api/&lt;version&gt;/outlaw/1/raid/1 <span class="se">\</span>
-X PUT --data @examples/json/rate_raid.json <span class="se">\</span>
-H <span class="s1">&#39;Content-Type: application/json&#39;</span>
</pre></div>
</div>
<p><strong>Response Example:</strong></p>
<div class="highlight-json notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="nt">&quot;status&quot;</span><span class="p">:</span> <span class="mi">204</span><span class="p">,</span>
<span class="nt">&quot;message&quot;</span><span class="p">:</span> <span class="s2">&quot;raid rated successfully&quot;</span>
<span class="p">}</span>
</pre></div>
</div>
</li>
</ul>
</dd></dl>

</div>
Expand Down
2 changes: 1 addition & 1 deletion doc/build/html/searchindex.js

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions examples/json/create_raid.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
"name": "Asalto 1",
"date": "2019-12-31 23:59:00",
"location": "Barcelona",
"gang": {},
"sheriff": {},
"outlaws": [],
"rates": []
"gang_id": 1,
"sheriff_id": 1,
"outlaws_id": [1, 2, 3]
}
}
8 changes: 8 additions & 0 deletions examples/json/rate_raid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rate": {
"food_quantity": 6,
"food_quality": 8,
"service_quality": 5,
"price": 7.5
}
}
48 changes: 1 addition & 47 deletions thesheriff/infrastructure/controllers/outlaw_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
"""
import json

import inject
from flask import Blueprint, jsonify, Response, request

from thesheriff.application.outlaw import rate_raid
from thesheriff.application.outlaw.create_outlaw import CreateOutlaw
from thesheriff.application.outlaw.list_friends import ListFriends
from thesheriff.application.outlaw.list_gangs import ListGangs
from thesheriff.application.outlaw.rate_raid import RateRaid
from thesheriff.application.outlaw.request.create_outlaw_request import \
CreateOutlawRequest
from thesheriff.domain.outlaw.score import Score
from thesheriff.application.outlaw.invite_friend import InviteFriend


@inject.autoparams()
Expand Down Expand Up @@ -95,24 +92,6 @@ def outlaw_controller(
"gang2": {}
}
}
* */<prefix>/outlaw/<int:outlaw_id>/raid/<int:raid_id>/* (POST)
**Request Example:**
.. code-block:: console
$ curl localhost:5000/api/<version>/outlaw/1/raid/1 \\
-X POST --data @examples/json/rate_raid.json \\
-H 'Content-Type: application/json'
**Response Example:**
.. code-block:: json
{
"status": 201,
"message": "raid rated successfully"
}
"""
blueprint_outlaw = Blueprint('outlaw', __name__)

Expand Down Expand Up @@ -149,31 +128,6 @@ def create_outlaw_endpoint() -> Response:

return jsonify(message)

@blueprint_outlaw.route("/outlaw/<int:outlaw_id>/raid/<int:raid_id>/",
methods=['PUT'])
def rate_raid_endpoint(outlaw_id: int, raid_id: int) -> Response:
"""rate_raid_endpoint recives rates for a Raid an executes
the Rate Raid use case.
:param raid_id: Id of the Raid to be rated
:type raid_id: Integer.
:param outlaw_id: Id of the Outlaw performing the rata
:type outlaw_id: Integer.
:returns: Response -- Flask Response.
"""
data = request.json
rate = data.get('rate')
score = Score(**rate)
rate_raid.execute(outlaw_id, raid_id, score)
message = {'status': 201, 'message': 'rated successfully'}
return jsonify(message)

@inject.autoparams()
def invite_friend_blueprint(invite_friend: InviteFriend) -> Blueprint:
"""Sends an email to the receiver
:param invitar_amigo: Object to invite the receiver
:returns: Blueprint
"""

@blueprint_outlaw.route("/outlaw/invite_friend/", methods=['POST'])
def invite_friend(receiver_mail_address: str) -> Response:
"""Invite friend will receive a mail from a json payload and will send
Expand Down
106 changes: 83 additions & 23 deletions thesheriff/infrastructure/controllers/raid_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,106 @@
This module implements the RESTful part of the Raid use cases.
"""
import json

import inject
from flask import Blueprint, jsonify, Response, request
from thesheriff.domain.outlaw.outlaw import Outlaw
# from thesheriff.application.bandido.crear_asalto import CrearAsalto
from flask import Blueprint, Response, request, make_response

from thesheriff.application.outlaw.rate_raid import RateRaid
from thesheriff.application.raid.create_raid import CreateRaid
from thesheriff.application.raid.create_raid_request import CreateRaidRequest
from thesheriff.domain.outlaw.score import Score


@inject.autoparams()
def raid_controller(rate_raid: RateRaid) -> Blueprint:
def raid_controller(create_raid: CreateRaid, rate_raid: RateRaid) -> Blueprint:
"""raid_controller holds the blueprint for all raid routes.
:param create_raid: CreateRaid use case implementation.
:param rate_raid: RateRaid use case implementation.
:type create_raid: CreateRaid
:type rate_raid: RateRaid
:return: Flask Blueprint.
:rtype: Blueprint
Implements the following routes:
* */<prefix>/raid/* (POST)
**Request Example:**
.. code-block:: console
$ curl localhost:5000/api/<version>/raid/ \\
-X POST --data @examples/json/create_raid.json \\
-H 'Content-Type: application/json'
**Response Example:**
.. code-block:: json
{
"status": 201,
"message": "Raid added successfully"
}
* */<prefix>/outlaw/<int:outlaw_id>/raid/<int:raid_id>/* (PUT)
**Request Example:**
.. code-block:: console
$ curl localhost:5000/api/<version>/outlaw/1/raid/1 \\
-X PUT --data @examples/json/rate_raid.json \\
-H 'Content-Type: application/json'
**Response Example:**
.. code-block:: json
{
"status": 204,
"message": "raid rated successfully"
}
"""
# def asalto_blueprint(
# crear_asalto: CrearAsalto, puntuar_asalto: PuntuarAsalto) -> Blueprint:
# """Create routes for entity Raid
# :param create_raid: Object with raid creation implementation.
# :param rate_raid: Object with raid rating implementation.
# :return: The Raid Blueprint.
# :rtype: Blueprint.
# """
blueprint_raid = Blueprint('raid', __name__)

# @asalto_blueprint.route("/asalto", methods=['POST'])
# def create_asalto() -> Response:
# """create_asalto registers an Asalto
# :return: Response.
# """
# #FIXME(tripledes)
# # CrearAsalto is still not implemented, this code will need to adapt
# # to its signature (we'll probably require bandido ID)
# new_asalto = request.json
# asalto = crear_asalto.execute(**new_asalto)
# return jsonify(asalto.to_dict())
@blueprint_raid.route("/raid", methods=['POST'])
def create_raid() -> Response:
# """create_raid registers a Raid
# :return: Response.
# """
data = request.get_json()
new_raid = data.get('raid')
raid_request = CreateRaidRequest(new_raid.get('name'),
new_raid.get('date'),
new_raid.get('location'),
new_raid.get('gang_id'),
new_raid.get('sheriff_id'),
new_raid.get('outlaw_ids'))

raid = create_raid.execute(raid_request)

raid_json = json.dumps(raid.__dict__)
resp = make_response(raid_json, 201)
return resp

@blueprint_raid.route("/outlaw/<int:outlaw_id>/raid/<int:raid_id>/",
methods=['PUT'])
def rate_raid(outlaw_id: int, raid_id: int) -> Response:
"""rate_raid recives rates for a Raid an executes
the Rate Raid use case.
:param raid_id: Id of the Raid to be rated
:type raid_id: Integer.
:param outlaw_id: Id of the Outlaw performing the rata
:type outlaw_id: Integer.
:returns: Response -- Flask Response.
"""
data = request.json
rate = data.get('rate')
score = Score(**rate)
rate_raid.execute(outlaw_id, raid_id, score)
message = {'message': 'rated successfully'}
return make_response(message, 204)

return blueprint_raid

0 comments on commit a99df70

Please sign in to comment.