private void OnConsumerReceived(object sender, BasicDeliverEventArgs e)
        {
            Byte[] body                  = null;
            string messageJson           = null;
            ActionBenefitMessage message = null;
            string exchangeName          = null;

            try
            {
                exchangeName = e.Exchange;
                body         = e.Body.ToArray();
                messageJson  = Encoding.UTF8.GetString(body);
                message      = JsonConvert.DeserializeObject <ActionBenefitMessage>(messageJson);
                HandleMessage(message, exchangeName);
                _channel.BasicAck(e.DeliveryTag, false);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                _channel.BasicReject(e.DeliveryTag, false);
                using (var scope = _service.CreateScope())
                {
                    IPharmacyService pharmacyService = scope.ServiceProvider.GetRequiredService <IPharmacyService>();
                    PharmacySystem   pharmacySystem  = pharmacyService.GetPharmacyByExchangeName(exchangeName);
                    if (pharmacySystem != null)
                    {
                        SendEmail(ex.Message, pharmacySystem.Email);
                    }
                }
            }
        }
        private void HandleMessage(ActionBenefitMessage message, string exchangeName)
        {
            using (var scope = _service.CreateScope())
            {
                IActionBenefitService actionBenefitService = scope.ServiceProvider.GetRequiredService <IActionBenefitService>();

                actionBenefitService.CreateActionBenefit(exchangeName, message);
            }
        }
Esempio n. 3
0
        public void CreateActionBenefit_Valid_Invalid_Test()
        {
            DbContextInMemory testData = new DbContextInMemory();
            MyDbContext       context  = testData._context;
            var pharmacyRepo           = new MySqlPharmacyRepo(context);
            var actionBenefitRepo      = new MySqlActionBenefitRepository(context);
            var actionBenefitService   = new ActionBenefitService(actionBenefitRepo, pharmacyRepo);
            var message = new ActionBenefitMessage("akcija1", "blablabla");

            actionBenefitService.CreateActionBenefit("exchange", message);
            Assert.Contains(context.ActionsBenefits, ab => ab.Message.Message == message.Message && ab.Message.Subject == message.Subject);

            Assert.Throws <ValidationException>(() => new ActionBenefitMessage("", ""));
        }
        public void CreateActionBenefit(string exchangeName, ActionBenefitMessage message)
        {
            PharmacySystem p = _pharmacyRepo.GetPharmacyByExchangeName(exchangeName);

            if (p == null)
            {
                throw new ArgumentNullException("There is no pharmacy with that exchange!");
            }
            if (message == null)
            {
                throw new ArgumentNullException("Message can not be null!");
            }

            ActionBenefit ab = new ActionBenefit(p.Id, message);

            CreateActionBenefit(ab);
        }
        public void CreateActionBenefit_Valid_VerifyCreateActionBenefit()
        {
            var mockPharmacyRepo      = new Mock <IPharmacyRepo>();
            var mockActionBenefitRepo = new Mock <IActionBenefitRepository>();
            var exchange = "ex1";
            var pharmacy = new PharmacySystem {
                Id = 1, Name = "apoteka1", ApiKey = "api1", Url = "url1", ActionsBenefitsExchangeName = exchange, ActionsBenefitsSubscribed = true
            };
            var message = new ActionBenefitMessage("akcija1", "blablabla");

            mockPharmacyRepo.Setup(r => r.GetPharmacyByExchangeName(exchange)).Returns(pharmacy);
            mockActionBenefitRepo.Setup(r => r.CreateActionBenefit(It.Is <ActionBenefit>(ab => ab.PharmacyId == pharmacy.Id && ab.Message.Message == message.Message && ab.Message.Subject == message.Subject))).Verifiable();
            ActionBenefitService actionBenefitService = new ActionBenefitService(mockActionBenefitRepo.Object, mockPharmacyRepo.Object);

            actionBenefitService.CreateActionBenefit(exchange, message);

            mockActionBenefitRepo.Verify();
        }