public IResult Handle(GetOrdersQuery query)
        {
            int clientId = SessionRepository.GetClientIdOfSession(query.sessionToken);

            if (clientId == -1)
            {
                throw new Exception();
            }

            var clients      = DatabaseQueryProcessor.GetClients();
            var foundClients = clients.FindAll(c => c.clientId == clientId);

            if (foundClients.Count != 1)
            {
                throw new Exception();
            }

            var orderRecords = DatabaseQueryProcessor.GetOrders(foundClients[0].clientId);
            var orderDtos    = new OrderDto[orderRecords.Count];

            for (var i = 0; i < orderRecords.Count; i++)
            {
                orderDtos[i]     = new OrderDto();
                orderDtos[i].key = orderRecords[i].orderId;
                var addressRecord = DatabaseQueryProcessor.GetAddress(orderRecords[i].addressId);
                orderDtos[i].address = new AddressDto()
                {
                    country         = addressRecord.country,
                    city            = addressRecord.city,
                    ZIPCode         = addressRecord.ZIPCode,
                    apartmentNumber = addressRecord.apartmentNumber,
                    buildingNumber  = addressRecord.buildingNumber,
                    street          = addressRecord.street,
                    key             = addressRecord.addressId
                };
                orderDtos[i].openDate  = orderRecords[i].openDate;
                orderDtos[i].closeDate = orderRecords[i].closeDate;
                orderDtos[i].status    = orderRecords[i].status;

                var orderEntriesRecords = DatabaseQueryProcessor.GetOrderEntries(orderRecords[i].orderId);
                var orderEntriesDtos    = new OrderEntryDto[orderEntriesRecords.Count];
                for (int j = 0; j < orderEntriesRecords.Count; j++)
                {
                    orderEntriesDtos[j] = new OrderEntryDto();
                    var product = DatabaseQueryProcessor.GetProduct(orderEntriesRecords[j].productId);
                    orderEntriesDtos[j].key      = orderEntriesRecords[j].orderEntryId;
                    orderEntriesDtos[j].name     = product.name;
                    orderEntriesDtos[j].price    = product.price;
                    orderEntriesDtos[j].quantity = orderEntriesRecords[j].quantity;
                }
                orderDtos[i].orderEntries = orderEntriesDtos;
                orderDtos[i].totalPrice   = orderEntriesDtos.Sum(orderEntry => orderEntry.price * orderEntry.quantity);
            }

            return(new OrdersDto()
            {
                isSuccess = true,
                orders = orderDtos
            });
        }
Esempio n. 2
0
        public void AddAddress_DuplicateAddress_Success(int _clientId, string _country, string _city, string _street, int _ZIPCode, int _buildingNumber, string _apartmentNumber)
        {
            DatabaseQueryProcessor.Erase();
            Shared.FillTheDatabase();

            var testSessionToken = SessionRepository.StartNewSession(_clientId);
            var addressExists    = DatabaseQueryProcessor.GetAddress(_clientId, _country, _city, _street, _ZIPCode, _buildingNumber, _apartmentNumber);

            if (addressExists != null)
            {
                DatabaseQueryProcessor.UpdateAddress(addressExists.addressId, false);
            }

            var addAddressCommand = new AddAddressCommand
            {
                sessionToken    = testSessionToken,
                country         = _country,
                city            = _city,
                street          = _street,
                ZIPCode         = _ZIPCode,
                buildingNumber  = _buildingNumber,
                apartmentNumber = _apartmentNumber
            };

            var handler     = new AddAddressCommandHandler();
            var addressBook = (AddressBookDto)handler.Handle(addAddressCommand);

            var record = DatabaseQueryProcessor.GetAddress(_clientId, _country, _city, _street, _ZIPCode, _buildingNumber, _apartmentNumber);

            DatabaseQueryProcessor.Erase();
            SessionRepository.RemoveSession(testSessionToken);

            Assert.IsTrue(record.isActive);
        }
Esempio n. 3
0
        public void AddAddress_AddNewAddress_Success(int _clientId, string _country, string _city, string _street, int _ZIPCode, int _buildingNumber, string _apartmentNumber)
        {
            DatabaseQueryProcessor.Erase();
            Shared.FillTheDatabase();

            var testSessionToken = SessionRepository.StartNewSession(_clientId);

            var addAddressCommand = new AddAddressCommand
            {
                sessionToken    = testSessionToken,
                country         = _country,
                city            = _city,
                street          = _street,
                ZIPCode         = _ZIPCode,
                buildingNumber  = _buildingNumber,
                apartmentNumber = _apartmentNumber
            };

            var handler     = new AddAddressCommandHandler();
            var addressBook = (AddressBookDto)handler.Handle(addAddressCommand);

            var record = DatabaseQueryProcessor.GetAddress(_clientId, _country, _city, _street, _ZIPCode, _buildingNumber, _apartmentNumber);

            DatabaseQueryProcessor.Erase();
            SessionRepository.RemoveSession(testSessionToken);

            Assert.IsNotNull(record);
            Assert.AreEqual(_clientId, record.clientId);
            Assert.AreEqual(_country, record.country);
            Assert.AreEqual(_city, record.city);
            Assert.AreEqual(_street, record.street);
            Assert.AreEqual(_ZIPCode, record.ZIPCode);
            Assert.AreEqual(_buildingNumber, record.buildingNumber);
            Assert.AreEqual(_apartmentNumber, record.apartmentNumber);
        }
Esempio n. 4
0
        public IResult Handle(InactivateAddressCommand command)
        {
            int clientId = SessionRepository.GetClientIdOfSession(command.sessionToken);

            if (clientId == -1)
            {
                throw new Exception();
            }

            var address = DatabaseQueryProcessor.GetAddress(command.addressKey);

            if (address != null && address.clientId == clientId)
            {
                DatabaseQueryProcessor.UpdateAddress(address.addressId, false);
                return(new GetAddressBookQueryHandler().Handle(new GetAddressBookQuery()
                {
                    sessionToken = command.sessionToken
                }));
            }

            return(new SuccessInfoDto()
            {
                isSuccess = false
            });
        }
Esempio n. 5
0
        public IResult Handle(AddAddressCommand command)
        {
            int clientId = SessionRepository.GetClientIdOfSession(command.sessionToken);

            if (clientId == -1)
            {
                throw new Exception();
            }

            if (!InputChecker.isValidName(command.country) ||
                !InputChecker.isValidName(command.city) ||
                !InputChecker.isValidName(command.street) ||
                !InputChecker.isValidApartmentNumber(command.apartmentNumber))
            {
                throw new Exception();
            }

            var duplicate = DatabaseQueryProcessor.GetAddress(clientId, command.country, command.city, command.street, command.ZIPCode, command.buildingNumber, command.apartmentNumber);

            if (duplicate != null)
            {
                DatabaseQueryProcessor.UpdateAddress(duplicate.addressId, true);
            }
            else
            {
                DatabaseQueryProcessor.CreateNewAddress(
                    clientId,
                    command.country,
                    command.city,
                    command.street,
                    command.ZIPCode,
                    command.buildingNumber,
                    command.apartmentNumber
                    );
            }

            return(new GetAddressBookQueryHandler().Handle(new GetAddressBookQuery()
            {
                sessionToken = command.sessionToken
            }));
        }
        public IResult Handle(AddOrderCommand command)
        {
            int clientId = SessionRepository.GetClientIdOfSession(command.sessionToken);

            if (clientId == -1)
            {
                throw new Exception();
            }

            var address = DatabaseQueryProcessor.GetAddress(
                clientId,
                command.address.country,
                command.address.city,
                command.address.street,
                command.address.ZIPCode,
                command.address.buildingNumber,
                command.address.apartmentNumber
                );

            if (address == null)
            {
                throw new Exception();
            }

            var products   = DatabaseQueryProcessor.GetProducts();
            var totalPrice = 0;

            foreach (var orderEntry in command.orderEntries)
            {
                var foundProducts = products.FindAll(p => p.name == orderEntry.name);
                if (foundProducts.Count != 1)
                {
                    throw new Exception();
                }

                totalPrice += foundProducts[0].price / 100 * orderEntry.quantity;
            }

            if (totalPrice.ToString("F", CultureInfo.InvariantCulture) != command.totalPrice)
            {
                throw new Exception();
            }

            var orderId = DatabaseQueryProcessor.CreateNewOrder(
                clientId,
                0,
                address.addressId,
                0,
                DateTime.Now.ToString("yyyy-MM-dd")
                );

            foreach (var orderEntry in command.orderEntries)
            {
                var foundProducts = products.FindAll(p => p.name == orderEntry.name);

                DatabaseQueryProcessor.CreateNewOrderEntry(
                    orderId,
                    foundProducts[0].productId,
                    orderEntry.quantity
                    );
            }

            return(new SuccessInfoDto()
            {
                isSuccess = true
            });
        }
Esempio n. 7
0
        public void AddOrder_CorrectAttributes_Success(int _clientId)
        {
            int    _key             = 0;
            string _country         = "Poland";
            string _city            = "Gdynia";
            string _street          = "Rzemieslnicza";
            int    _ZIPCode         = 30445;
            int    _buildingNumber  = 12;
            string _apartmentNumber = "1a";

            var testSessionToken = SessionRepository.StartNewSession(_clientId);

            DatabaseQueryProcessor.Erase();
            Shared.FillTheDatabase();

            var testOrderEntry = new OrderEntryDto
            {
                key      = 0,
                name     = "Lavazza",
                quantity = 1,
                price    = 15
            };
            var testOrderEntries = new OrderEntryDto[1];

            testOrderEntries[0] = testOrderEntry;

            var testAddress = new AddressDto
            {
                key             = _key,
                country         = _country,
                city            = _city,
                street          = _street,
                ZIPCode         = _ZIPCode,
                buildingNumber  = _buildingNumber,
                apartmentNumber = _apartmentNumber
            };


            var addOrderCommand = new AddOrderCommand
            {
                sessionToken = testSessionToken,
                orderEntries = testOrderEntries,
                totalPrice   = "15.00",
                address      = testAddress
            };

            var addressId = DatabaseQueryProcessor.GetAddress(_clientId,
                                                              _country,
                                                              _city,
                                                              _street,
                                                              _ZIPCode,
                                                              _buildingNumber,
                                                              _apartmentNumber);

            var handler = new AddOrderCommandHandler();
            var result  = (SuccessInfoDto)handler.Handle(addOrderCommand);

            var expectedOrder = DatabaseQueryProcessor.GetTheMostRecentOrder(_clientId);

            SessionRepository.RemoveSession(testSessionToken);
            DatabaseQueryProcessor.Erase();


            Assert.IsTrue(result.isSuccess);
        }