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 }); }
public IResult Handle(GetPersonalDataQuery 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(); } return(new PersonalDataDto() { isSuccess = true, email = foundClients[0].email, lastName = foundClients[0].lastName, firstName = foundClients[0].firstName, receiveNewsletterEmail = foundClients[0].isSignedUpForNewsletter, newsletterEmail = foundClients[0].newsletterEmail }); }
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 }); }
public IResult Handle(AddPaymentCommand command) { int clientId = SessionRepository.GetClientIdOfSession(command.sessionToken); if (clientId == -1) { throw new Exception(); } var order = DatabaseQueryProcessor.GetTheMostRecentOrder(clientId); if (order == null) { throw new Exception(); } var totalPrice = DatabaseQueryProcessor.GetTotal(order.orderId); DatabaseQueryProcessor.CreateNewPayment( command.paymentId, order.orderId, totalPrice, DateTime.Now.ToString("yyyy-MM-dd") ); ThreadPool.QueueUserWorkItem( o => new OrderPlacementEmail().Send(clientId)); var isSuccessPayment = PaymentMethod.Check(command.paymentId, totalPrice); if (isSuccessPayment) { DatabaseQueryProcessor.UpdateOrder(order.orderId, 1); ThreadPool.QueueUserWorkItem(o => new SuccessfullPaymentEmail().Send(clientId)); } else { ThreadPool.QueueUserWorkItem(o => new UnsuccessfullPaymentEmail().Send(clientId)); } return(new SuccessInfoDto { isSuccess = true }); }
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(AddComplaintCommand command) { int clientId = SessionRepository.GetClientIdOfSession(command.sessionToken); if (clientId == -1) { throw new Exception(); } var order = DatabaseQueryProcessor.GetOrder(command.orderId); if (order == null) { throw new Exception(); } if (order.clientId != clientId) { throw new Exception(); } var foundComplaint = DatabaseQueryProcessor.GetComplaint(order.orderId); if (foundComplaint != null) { throw new Exception(); } DatabaseQueryProcessor.CreateNewComplaint( command.orderId, 0, command.description, DateTime.Now.ToString("yyyy-MM-dd"), false ); return(new SuccessInfoDto() { isSuccess = true }); }
public IResult Handle(GetAddressBookQuery 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 addresses = DatabaseQueryProcessor.GetAddresses(foundClients[0].clientId); var foundActiveAddresses = addresses.FindAll(a => a.isActive == true); var addressesDto = new AddressDto[foundActiveAddresses.Count]; for (var i = 0; i < foundActiveAddresses.Count; i++) { addressesDto[i] = new AddressDto(); addressesDto[i].key = foundActiveAddresses[i].addressId; addressesDto[i].country = foundActiveAddresses[i].country; addressesDto[i].city = foundActiveAddresses[i].city; addressesDto[i].street = foundActiveAddresses[i].street; addressesDto[i].ZIPCode = foundActiveAddresses[i].ZIPCode; addressesDto[i].buildingNumber = foundActiveAddresses[i].buildingNumber; addressesDto[i].apartmentNumber = foundActiveAddresses[i].apartmentNumber; } return(new AddressBookDto() { isSuccess = true, addresses = addressesDto }); }
public IResult Handle(UpdatePersonalDataCommand command) { int clientId = SessionRepository.GetClientIdOfSession(command.sessionToken); if (clientId == -1) { throw new Exception(); } if (!InputChecker.isValidEmail(command.email) || !InputChecker.isValidName(command.firstName) || !InputChecker.isValidName(command.lastName) || (command.receiveNewsletterEmail && !InputChecker.isValidEmail(command.newsletterEmail))) { throw new Exception(); } DatabaseQueryProcessor.UpdateClientCredentials(clientId, "email", command.email); DatabaseQueryProcessor.UpdateClientCredentials(clientId, "firstName", command.firstName); DatabaseQueryProcessor.UpdateClientCredentials(clientId, "lastName", command.lastName); if (command.changePassword) { DatabaseQueryProcessor.UpdateClientCredentials(clientId, "password", PasswordEncryptor.encryptSha256(command.newPassword)); } if (command.receiveNewsletterEmail) { DatabaseQueryProcessor.UpdateClientCredentials(clientId, "newsletterEmail", command.newsletterEmail); } else { DatabaseQueryProcessor.UpdateClientCredentials(clientId, "newsletterEmail", ""); } return(new GetPersonalDataQueryHandler().Handle(new GetPersonalDataQuery() { 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 }); }