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

Commit

Permalink
validate use case create gang
Browse files Browse the repository at this point in the history
  • Loading branch information
Tati authored and tbanjos committed Dec 23, 2019
1 parent 02c4a80 commit d036f0d
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 20 deletions.
6 changes: 6 additions & 0 deletions examples/json/create_gang.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"gang": {
"owner_id": "1",
"name": "The best gang"
}
}
12 changes: 12 additions & 0 deletions thesheriff/application/gang/request/create_gang_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateGangRequest:
"""Class JoinGangRequest holds data required to join a Gang.
:param name: Name of the Gang.
:type name: String
:param owner_id: Id of the Outlaw creating the gang.
:type owner_id: Integer
"""

def __init__(self, owner_id: int, name: str):
self.owner_id = owner_id
self.name = name
7 changes: 5 additions & 2 deletions thesheriff/application/outlaw/create_gang.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import inject

from thesheriff.application.gang.request.create_gang_request import \
CreateGangRequest
from thesheriff.domain.gang.gang_factory import GangFactory
from thesheriff.domain.gang.repository.gang_repository import GangRepository
from thesheriff.domain.gang.gang import Gang
Expand All @@ -15,7 +18,7 @@ class CreateGang:
def __init__(self, gang_repository: GangRepository):
self.__gang_repository = gang_repository

def execute(self, owner_id: int, name: str) -> Gang:
def execute(self, request: CreateGangRequest) -> Gang:
"""execute is the actual action of the Raid rating use case.
:param owner_id: ID of the Outlaw creating the Gang.
Expand All @@ -25,6 +28,6 @@ def execute(self, owner_id: int, name: str) -> Gang:
:return: The created Gang.
:rtype: Gang
"""
gang = GangFactory.create(owner_id, name)
gang = GangFactory.create(request.owner_id, request.name)
self.__gang_repository.add(gang)
return gang
4 changes: 3 additions & 1 deletion thesheriff/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

gang_table = Table('gangs', METADATA,
Column('id', Integer, primary_key=True, autoincrement=True),
Column('score', Integer, nullable=False),
Column('score', Integer, nullable=False, default=0.0),
Column('owner_id', Integer, nullable=False),
Column('name', Text, nullable=False),
Column('members', ForeignKey('outlaws.id')))

raid_table = Table('raids', METADATA,
Expand Down
5 changes: 2 additions & 3 deletions thesheriff/domain/gang/gang.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ class Gang:

def __init__(self, owner_id: int, name: str):
# TODO(all)
# It would seem only logical that the owner is also a member.
# Should owner_id be an actual Outlaw instance?
# Verify owner_id is of an existing outlaw
self.name = name
self.members = list()
self.created_raids = 0
self.owner_id = owner_id
self.id = None

if self.name is None:
raise Exception('Gang name required')

Expand All @@ -41,4 +39,5 @@ def add_members(self, members: list) -> NoReturn:
"""
# TODO(all)
# Should this be singular? add_member.
# It depends, if its a list, add_members is correct
self.members = members
25 changes: 13 additions & 12 deletions thesheriff/infrastructure/controllers/gang_controller.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import inject
import json
from flask import Blueprint, jsonify, Response, request
from flask import Blueprint, jsonify, Response, request, make_response

from thesheriff.application.gang.request.create_gang_request import \
CreateGangRequest
from thesheriff.application.gang.request.join_gang_request import \
JoinGangRequest
from thesheriff.application.outlaw.join_gang import JoinGang
from thesheriff.application.outlaw.create_gang import CreateGang
from thesheriff.application.gang.list_gangs import ListGangs
from thesheriff.application.outlaw.create_gang import CreateGang

This comment has been minimized.

Copy link
@rmuhamed

rmuhamed Dec 23, 2019

Collaborator

Perfecto!



@inject.autoparams()
def gang_controller(
join_gang: JoinGang, create_gang: CreateGang, list_gangs: ListGangs
join_gang: JoinGang, create_gang: CreateGang, list_gangs: ListGangs
) -> Blueprint:
"""gang_controller holds the blueprint for all gang routes.
Expand Down Expand Up @@ -104,16 +107,14 @@ def create_gang_endpoint() -> Response:
return jsonify(message)

data = request.json
new_gang = data.get('gang')

outlaw_id = data.get('owner_id')
name = data.get('name')

gang = create_gang.execute(outlaw_id, name)
gang_request = CreateGangRequest(new_gang.get('owner_id'),
new_gang.get('name'))
gang = create_gang.execute(gang_request)

gang_json = json.dumps(gang)

message = {'status': 201, 'gang': gang_json}

return jsonify(message)
gang_json = json.dumps(gang.__dict__)
resp = make_response(gang_json, 201)
return resp

return blueprint_gang
4 changes: 3 additions & 1 deletion thesheriff/infrastructure/controllers/outlaw_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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
Expand Down Expand Up @@ -172,7 +174,7 @@ def invite_friend_blueprint(invite_friend: InviteFriend) -> Blueprint:
:returns: Blueprint
"""

@invite_friend.route("/outlaw/invite_friend/", methods=['POST'])
@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
to him an invitation mail to join the app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def add(self, new_gang: Gang) -> NoReturn:
:return: No returned value.
:rtype: NoReturn
"""
query = self.__gang_table.insert().value(**new_gang)
query = self.__gang_table.insert()\
.values(owner_id=new_gang.owner_id, name=new_gang.name)
self.__connection.execute(query)

def update(self, mod_gang: Gang) -> NoReturn:
Expand Down

0 comments on commit d036f0d

Please sign in to comment.