예제 #1
0
 def delete_client(client_id):
     try:
         ClientService.get_client_by_id(int(client_id))
         ClientService.delete_client(int(client_id))
         return 'Successfully Deleted', 205
     except ResourceNotFound as r:
         return r.message, 404
예제 #2
0
 def test_update_client(self):
     repo = Repository()
     undo_service = UndoService()
     client_service = ClientService(repo, undo_service)
     client_service.add_client(1, "Popescu Alin")
     client_service.update_client(1, "Ionescu George")
     list_clients = client_service.list_clients()
     self.assertEqual(len(list_clients), 1)
     with self.assertRaises(IdError):        # client does not exist
         client_service.update_client(2, "Mara")
예제 #3
0
    def test_delete_rentals(self):
        repoRentals = Repository()
        repoClient = Repository()
        repoMovie = Repository()
        undo_service = UndoService()
        serviceC = ClientService(repoClient, undo_service)
        serviceM = MovieService(repoMovie, undo_service)
        serviceC.add_client(2, "Ana", False)
        serviceC.add_client(5, "Ion", False)
        serviceC.add_client(1, "Paula", False)
        serviceM.add_movie(6, "Frozen", "Ice and songs", "Animation", False)
        serviceM.add_movie(2, "Titanic", "a", "Romance", False)

        service = RentalService(repoRentals, repoClient, repoMovie, undo_service)
        service.rent_a_movie(2, 6, "2019-05-15", "2019-05-26", None)
        service.rent_a_movie(5, 2, "2019-05-15", "2019-05-26", "2019-05-25")

        serviceC.delete_client(2, False)
    # Remove rental by client id
        service.remove_rental_by_client(2, False)
        self.assertEqual(len(service.list_rentals()), 1)

        serviceM.delete_movie(2, False)
    # Remove rental by movie id
        service.remove_rental_by_movie(2, False)
        self.assertEqual(len(service.list_rentals()), 0)
예제 #4
0
 def get_client(client_id):
     try:
         client = ClientService.get_client_by_id(int(client_id))
         return jsonify(client.json()), 200
     except ValueError:
         return "Not a valid ID", 400  # Bad Request
     except ResourceNotFound as r:
         return r.message, 404
예제 #5
0
 def put_client(client_id):
     try:
         client = Client.json_parse(request.json)
         client.client_id = int(client_id)
         client = ClientService.update_client(client)
         return jsonify(client.json()), 200
     except ResourceNotFound as r:
         return r.message, 404
예제 #6
0
    def test_late_rentals(self):
        repoR = Repository()
        repoC = Repository()
        repoM = Repository()
        undo_service = UndoService()
        service_client = ClientService(repoC, undo_service)
        service_client.init_list_clients()

        service_movie = MovieService(repoM, undo_service)
        service_movie.init_list_movies()

        service = RentalService(repoR, repoC, repoM, undo_service)
        service.init_list_rentals()

        statistics = StatisticsService(repoC, repoM, repoR)
        late_rentals = statistics.late_rentals()
        self.assertEqual(len(late_rentals), 7)
예제 #7
0
    def transfer_account(client_id, account_id, account_id2):
        action = request.json['action']
        if action == 'transfer':
            try:
                ClientService.get_client_by_id(int(client_id))
                account = AccountService.get_account_by_id(int(account_id))
                account2 = AccountService.get_account_by_id(int(account_id2))

                AccountService.transfer_money(int(account_id),
                                              int(account_id2))
                return (
                    f'Successfully transferred! First Account new balance: {account.balance} Second Account new balance: {account2.balance}'
                ), 200

            except ResourceUnavailable as e:
                return e.message, 422
            except ResourceNotFound as r:
                return r.message, 404
예제 #8
0
    def test_most_rented_movies(self):
        repoR = Repository()
        repoC = Repository()
        repoM = Repository()
        undo_service = UndoService()
        service_client = ClientService(repoC, undo_service)
        service_client.init_list_clients()

        service_movie = MovieService(repoM, undo_service)
        service_movie.init_list_movies()

        service = RentalService(repoR, repoC, repoM, undo_service)
        service.init_list_rentals()

        statistics = StatisticsService(repoC, repoM, repoR)
        most_rented_list = statistics.most_rented_movies()
        most_rented_list = sorted(most_rented_list, key=lambda rental: rental.days, reverse=True)

        rental_days = most_rented_list[0]
        owner = service_movie.search_by_id(3)
        self.assertEqual(rental_days.owner, owner)
        self.assertEqual(rental_days.days, 56)
예제 #9
0
    def put_client_account_id(client_id, account_id):
        try:
            client_id = ClientService.get_client_by_id(int(client_id))
            AccountService.get_account_by_id(int(account_id))

            account = Account.json_parse(request.json)
            account.client_id = client_id

            account.account_id = int(account_id)
            account = AccountService.update_account(account)
            return jsonify(account.json()), 200
        except ResourceNotFound as r:
            return r.message, 404
예제 #10
0
    def test_add_client(self):
        the_id = 1
        name = "Popescu Alin"
        repo = Repository()
        undo_service = UndoService()
        client_service = ClientService(repo, undo_service)
        client_service.add_client(the_id, name)
        list_clients = client_service.list_clients()
        self.assertEqual(len(list_clients), 1)

        with self.assertRaises(IdError):       # client already added
            client_service.add_client(1, "Mara Pop")
예제 #11
0
    def patch_account(client_id, account_id):

        action = request.json['action']
        if action == 'deposit':
            try:
                ClientService.get_client_by_id(int(client_id))
                AccountService.get_account_by_id(int(account_id))

                balance = AccountService.deposit_account(int(account_id))
                return (
                    f'Successfully deposited! New balance is: {balance}'), 200
            except ResourceNotFound as r:
                return r.message, 404

        elif action == 'withdraw':
            try:
                balance = AccountService.withdraw_account(int(account_id))
                return (
                    f'Successfully withdrawn! New balance is: {balance}'), 200
            except ResourceUnavailable as e:
                return e.message, 422
            except ResourceNotFound as r:
                return r.message, 404
예제 #12
0
    def test_add_rentals(self):
        repoRentals = Repository()
        repoClient = Repository()
        repoMovie = Repository()
        undo_service = UndoService()
        serviceC = ClientService(repoClient, undo_service)
        serviceM = MovieService(repoMovie,undo_service)
        serviceC.add_client(2, "Ana",False)
        serviceC.add_client(5, "Ion", False)
        serviceC.add_client(1, "Paula",False)
        serviceM.add_movie(6, "Frozen", "Ice and songs", "Animation", False)
        serviceM.add_movie(2, "Titanic", "a", "Romance", False)

        service = RentalService(repoRentals, repoClient, repoMovie, undo_service)
        service.rent_a_movie(2, 6, "2019-05-15", "2019-05-26", None)
        service.rent_a_movie(5, 2, "2019-05-15", "2019-05-26", "2019-05-25")

    # Rent a movie
        with self.assertRaises(IdError):  # client does not exist
            service.rent_a_movie(3, 6, "2019-05-20", "2019-06-07", None)

        with self.assertRaises(ServiceError):   # client eligible
            service.rent_a_movie(2, 2, "2019-05-29", "2019-06-15", None)

        with self.assertRaises(ServiceError):       # movie not available
            service.rent_a_movie(5, 6, "2019-05-20", "2019-06-07", None)

        with self.assertRaises(ServiceError):       # rental already added
            service.rent_a_movie(5, 2, "2019-05-15", "2019-05-26", "2019-05-25")

        with self.assertRaises(ValueError):     # invalid due date
            Rv.validate_due_date("2019-11-26", "2019-10-02")

    # Return a movie
        with self.assertRaises(IdError):      # client does not exist
            service.return_a_movie(3, 6, "2019-05-26")

        with self.assertRaises(IdError):      # movie does not exist
            service.return_a_movie(2, 1, "2019-05-26")

        service.return_a_movie(2, 6, "2019-05-25") # return a movie
        self.assertEqual(len(serviceM.list_movies()), 2)       # 2 valid rentals
예제 #13
0
                                                   ["idClient", "name"])
            movie_repository = DatabaseRepository(
                database, cursor, 'movies',
                ["id_movie", "title", "description", "genre"])
            rental_repository = DatabaseRepository(
                database, cursor, 'rentals', [
                    "id_client", "id_movie", "rented_date", "due_data",
                    "returned_date"
                ])
        else:
            client_repository = repository_type(settings["clients"])
            movie_repository = repository_type(settings["movies"])
            rental_repository = repository_type(settings["rentals"])

        undo_service = UndoService()
        client_service = ClientService(client_repository, undo_service)
        movie_service = MovieService(movie_repository, undo_service)
        rental_service = RentalService(rental_repository, client_repository,
                                       movie_repository, client_service,
                                       movie_service, undo_service)
        statistics_service = StatisticsService(client_repository,
                                               movie_repository,
                                               rental_repository)

        ui = Ui(client_service, movie_service, rental_service,
                statistics_service, undo_service, settings["init_list"])
        ui.start()

    except Exception as ex:
        print(ex)
예제 #14
0
 def test_delete_client(self):
     repo = Repository()
     undo_service = UndoService()
     client_service = ClientService(repo, undo_service)
     client_service.add_client(1, "Popescu Alin")
     client_service.add_client(2, "Ionescu Maria")
     client_service.add_client(3, "Trifan Ion")
     client_service.delete_client(2)
     list_clients = client_service.list_clients()
     self.assertEqual(len(list_clients), 2)
     with self.assertRaises(IdError):    # client does not exist
         client_service.delete_client(4)
예제 #15
0
from service.client_service import ClientService
from service.movie_service import MovieService
from service.rental_service import RentalService
from domain.client_formatter import ClientFormatter
from domain.client_validator import ClientValidator
from domain.movie_formatter import MovieFormatter
from domain.movie_validator import MovieValidator
from domain.rental_formatter import RentalFormatter
from domain.rental_validator import RentalValidator
from repository.client_repository import ClientRepository
from repository.movie_repository import MovieRepository
from repository.rental_repository import RentalRepository
from ui.consoleUI import ConsoleUI

if __name__ == "__main__":
    movie_repository = MovieRepository()
    client_repository = ClientRepository()
    rental_repository = RentalRepository()

    client_controller = ClientService(client_repository, ClientValidator(),
                                      ClientFormatter(), rental_repository,
                                      True)
    movie_controller = MovieService(movie_repository, MovieValidator(),
                                    MovieFormatter(), True)
    rental_controller = RentalService(rental_repository, RentalValidator(),
                                      RentalFormatter(), movie_repository,
                                      client_repository, True)

    ui = ConsoleUI(client_controller, movie_controller, rental_controller)
    ui.show()
예제 #16
0
 def get_all_account(client_id):
     try:
         ClientService.get_client_by_id(int(client_id))
         return jsonify(AccountService.all_accounts(client_id)), 200
     except ResourceNotFound as r:
         return r.message, 404
예제 #17
0
    def test_search(self):
        repo = Repository()
        undo_service = UndoService()
        client_service = ClientService(repo, undo_service)
        client_service.add_client(1, "Popescu Alin")
        client_service.add_client(2, "Ionescu Maria")
        client_service.add_client(3, "Trifan Ion")
        client_service.add_client(4, "Muresan Alina")

        # by id
        match = client_service.search_by_id(1)
        self.assertEqual(match.id, 1)

        # by name
        match = client_service.search_by_name("alin")
        self.assertEqual(match[0].name, "Popescu Alin")
        self.assertEqual(match[1].name, "Muresan Alina")
from flask import Blueprint, Response
from flask import jsonify, request

from controller.helpers.authorize import auth_required_with_role
from controller.helpers.mapper import Mapper
from domain.enums.role import Role
from repository.client_repository import ClientRepository
from service.client_service import ClientService

client_repo = ClientRepository()
client_service = ClientService(client_repo)

clients = Blueprint('clients', __name__)


@clients.route('/clients', methods=['GET'])
@auth_required_with_role([Role.administrator, Role.scrum_master, Role.hr])
def get_clients():
    client_id = request.args.get('clientid')
    if client_id is None:
        # get all clients
        return jsonify([
            Mapper.get_instance().client_to_json(client)
            for client in client_service.getAll()
        ])
    else:
        # get one client
        try:
            client = client_service.getOne(client_id)
        except ValueError as err:
            return Response(str(err), 400)
예제 #19
0
 def get_all_client():
     return jsonify(ClientService.all_clients()), 200
예제 #20
0
 def post_client():
     client = Client.json_parse(request.json)
     client = ClientService.create_client(client)
     return jsonify(client.json()), 201
from flask import request
from flask_restful import Resource
from ..flask_config import app
from service.client_service import ClientService

client_service = ClientService()


class ClientController(Resource):
    def get(self, idclient=None):
        return client_service.get_clients(idclient)

    def post(self):
        nombre = request.json['nombre']

        new_client = client_service.add_client(None, nombre)

        return new_client

    def put(self):
        idclient = request.json['idclient']
        nombre = request.json['nombre']

        return client_service.update_client(idclient, nombre)

    def delete(self):
        idclient = request.json['idclient']

        return client_service.delete_client(idclient)
예제 #22
0
 def test_init_list(self):
     repo = Repository()
     undo_service = UndoService()
     Cs = ClientService(repo, undo_service)
     Cs.init_list_clients()
     self.assertEqual(len(repo), 10)