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

Commit

Permalink
Rate raid from outlaw (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmuhamed committed Dec 22, 2019
1 parent abe93ff commit 4b956c3
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 35 deletions.
5 changes: 3 additions & 2 deletions tests/test_end_raid.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_end_raid():
outlaws = [outlaw1, outlaw2]
gang.add_members(outlaws)
raid = Raid("amazing raid", "street 1, 05", Sheriff(outlaw1), gang,
date=datetime.now())
date=datetime.now(), raid_id=None)

raid.join(outlaw=outlaw1)
raid.join(outlaw=outlaw2)
Expand All @@ -32,6 +32,7 @@ def test_end_raid():

assert result == "Gang's score: 0. Sheriff's score on raid 'amazing raid': 100"


def test_raid_can_not_be_ended_throws_exception():
raid_repository = MockRaidRepository()
gang = Gang(1, "Gang")
Expand All @@ -40,7 +41,7 @@ def test_raid_can_not_be_ended_throws_exception():
outlaws = [outlaw1, outlaw2]
gang.add_members(outlaws)
raid = Raid("amazing raid", "street 1, 05", Sheriff(outlaw1), gang,
date=datetime.now())
date=datetime.now(), raid_id=None)

raid.join(outlaw=outlaw1)
raid.join(outlaw=outlaw2)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_grade_raid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from datetime import datetime

from tests.mock_outlaw_repository import MockOutlawRepository
from thesheriff.application.raid.grade_raid import GradeRaid
from thesheriff.domain.gang.gang import Gang
from thesheriff.domain.raid.raid import Raid
from thesheriff.domain.outlaw.outlaw import Outlaw
from thesheriff.domain.outlaw.sheriff import Sheriff
Expand All @@ -9,7 +12,9 @@ def test_grade_raid():
sheriff = Sheriff(Outlaw(1, "the sheriff", "sheriff@yopmail.com"))
sheriff.update_score(22)

raid = Raid("very nice restaurant", None, sheriff, None, None)
gang = Gang(2, "The Gang")

raid = Raid("very nice restaurant", "Fake ST 123", sheriff, gang, datetime.now(), None)

raid.add_rate(10)
raid.add_rate(9)
Expand Down
11 changes: 9 additions & 2 deletions tests/test_rate_raid.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
from datetime import datetime

from tests.mock_raid_repository import MockRaidRepository
from tests.mock_outlaw_repository import MockOutlawRepository
from thesheriff.application.outlaw.rate_raid import RateRaid
from thesheriff.domain.gang.gang import Gang
from thesheriff.domain.outlaw.sheriff import Sheriff
from thesheriff.domain.raid.raid import Raid
from thesheriff.domain.outlaw.outlaw import Outlaw
from thesheriff.domain.outlaw.score import Score


def test_rate_raid():
outlaw_repository = MockOutlawRepository()
outlaw = Outlaw(1, None, None)
outlaw = Outlaw(1, "MockedOutLaw", "iam_a_mocked_outlaw@thesheriff.corp")
outlaw_repository.add(outlaw)

gang = Gang(2, "The Gang")
sheriff = Sheriff(Outlaw(2, "Sheriff", "iam_thesheriff_gang2@thesheriff.corp"))

raid_repository = MockRaidRepository()
raid = Raid("very nice restaurant", None, None, None, None)
raid = Raid("very nice restaurant", "None", sheriff, gang, datetime.now(), None)
raid_repository.add(raid)

rate_raid = RateRaid(outlaw_repository, raid_repository)
Expand Down
4 changes: 2 additions & 2 deletions thesheriff/domain/outlaw/score.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NUMERO_CRITERIOS = 4
CRITERIAS = 4


class Score:
Expand Down Expand Up @@ -29,4 +29,4 @@ def value(self) -> float:
return (
self.__food_quantity + self.__food_quality +
self.__service_quality + self.__price
) / NUMERO_CRITERIOS
) / CRITERIAS
9 changes: 5 additions & 4 deletions thesheriff/domain/raid/raid.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ class Raid:
:type gang: Gang.
:param date: Date and time when the raid happens.
:type date: datetime.datetime.
:param id: Optional, Raid Id.
:type id: Integer.
:param raid_id: Optional, Raid Id.
:type raid_id: Integer.
"""

def __init__(
self, name: str, location: str,
sheriff: Sheriff, gang: Gang, date: datetime
sheriff: Sheriff, gang: Gang, date: datetime,
raid_id: Optional[int] = None
):
self.name = name
self.outlaws = list()
self.location = location
self.sheriff = sheriff
self.gang = gang
self.rates = []
self.__id = None
self.id = raid_id

def add_rate(self, rate: float) -> NoReturn:
"""Method add_rate, adds a new rate for the Raid.
Expand Down
4 changes: 2 additions & 2 deletions thesheriff/domain/raid/raid_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class RaidFactory(Raid):

@staticmethod
def create(
id: Optional[int], name: str, outlaws: List[Outlaw], location: str,
sheriff: Sheriff, gang: Gang, date: datetime
id: Optional[int], name: str, outlaws: List[Outlaw],
sheriff: Sheriff, gang: Gang, location: str, date: datetime
) -> Raid:
"""Method create, produces a Raid instance.
:param id: Optional, Raid's Id.
Expand Down
4 changes: 4 additions & 0 deletions thesheriff/domain/raid/repository/raid_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ def of_id(self, raid_id: int) -> Raid:
@abc.abstractmethod
def add(self, new_raid: Raid) -> NoReturn:
pass

@abc.abstractmethod
def update(self, mod_raid: Raid) -> NoReturn:
pass
24 changes: 22 additions & 2 deletions thesheriff/infrastructure/controllers/outlaw_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
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


@inject.autoparams()
def outlaw_blueprint(
create_outlaw: CreateOutlaw, list_friends: ListFriends,
list_gangs: ListGangs
create_outlaw: CreateOutlaw, list_friends: ListFriends,
list_gangs: ListGangs, rate_raid: RateRaid
) -> Blueprint:
blueprint_outlaw = Blueprint('outlaw', __name__)

Expand Down Expand Up @@ -48,4 +50,22 @@ 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 = {'raid_id': raid_id, 'message': 'rated successfully'}
return jsonify(message)

return blueprint_outlaw
16 changes: 0 additions & 16 deletions thesheriff/infrastructure/controllers/raid_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,3 @@ def raid_blueprint(rate_raid: RateRaid) -> Blueprint:
# new_asalto = request.json
# asalto = crear_asalto.execute(**new_asalto)
# return jsonify(asalto.to_dict())
@blueprint_raid.route("/raid/<int:asalto_id>/rate", methods=['POST'])
def rate_raid_endpoint(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 Asalto to be rated
:type raid_id: Integer.
:returns: Response -- Flask Response.
"""
data = request.json
outlaw_id = data.get('outlaw_id')
rate = data.get('rate')
score = Score(**rate)
rate_raid.execute(outlaw_id, score)
message = {'raid_id': raid_id, 'message': 'rated successfully'}
return jsonify(message)
return blueprint_raid
19 changes: 15 additions & 4 deletions thesheriff/infrastructure/repository/mysql_raid_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, database_uri: str, meta: MetaData, raid_table: Table):
self.__raid_table = raid_table
meta.create_all(self.__connection)

def of_id(self, raid_id: int) -> Outlaw:
def of_id(self, raid_id: int) -> Raid:
"""Method of_id searches for an Raid matching raid_id
:param raid_id: Id of the Raid to be returned.
:type raid_id: Integer.
Expand All @@ -43,12 +43,13 @@ def of_id(self, raid_id: int) -> Outlaw:
outlaws=outlaws,
sheriff=result.get('sheriff_id'),
gang=result.get('gang_id'),
location=result.get('location'),
date=result.get('date'))

def add(self, new_outlaw: Outlaw) -> NoReturn:
def add(self, new_raid: Raid) -> NoReturn:
"""Method add persists a new Outlaw to MySQL.
:param new_outlaw: Object with the Outlaw information
:type new_outlaw: Outlaw.
:param new_raid: Object with the Raid information
:type new_raid: Raid.
:return: NoReturn.
"""
outlaws_ids = self.__find_outlaws_ids()
Expand All @@ -60,6 +61,16 @@ def add(self, new_outlaw: Outlaw) -> NoReturn:
)
self.__connection.execute(query)

def update(self, mod_raid: Raid) -> NoReturn:
"""Method update updates an existing Raid.
:param mod_raid: Object with the Raid information
:type mod_raid: Raid.
:return: NoReturn.
"""
query = self.__raid_table.update().where(
self.__raid_table.c.id == mod_raid.id).values(**mod_raid)
self.__connection.execute(query)

def __join_bandidos_ids(self):
ids = [bandido.id for bandido in self.bandidos]
return ','.join(ids)
Expand Down

0 comments on commit 4b956c3

Please sign in to comment.