public async Task <IActionResult> Update([FromBody] PharmacySystem pharmacySystem)
 {
     if (pharmacySystem.isValid())
     {
         var pharmacyOld = _pharmacySystemService.GetPharmacyByIdNoTracking(pharmacySystem.Id);
         if (pharmacyOld == null)
         {
             throw new ArgumentNullException("There is no pharmacy with id=" + pharmacySystem.Id);
         }
         if (pharmacySystem.ActionsBenefitsExchangeName == null)
         {
             pharmacySystem.ActionsBenefitsSubscribed = false;
         }
         if (await _actionBenefitService.SubscriptionEdit(pharmacyOld.ActionsBenefitsExchangeName,
                                                          pharmacyOld.ActionsBenefitsSubscribed,
                                                          pharmacySystem.ActionsBenefitsExchangeName,
                                                          pharmacySystem.ActionsBenefitsSubscribed))
         {
             _pharmacySystemService.UpdatePharmacy(pharmacySystem);
             return(Ok());
         }
         return(BadRequest());
     }
     else
     {
         return(BadRequest());
     }
 }
        public async Task <IActionResult> ApiRegister(PharmacySystemDTO pharmacyDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (pharmacyDto.ActionsBenefitsExchangeName != null)
            {
                pharmacyDto.ActionsBenefitsSubscribed = true;
            }

            try
            {
                PharmacySystem pharmacy = new PharmacySystem()
                {
                    Name   = pharmacyDto.Name,
                    Url    = pharmacyDto.Url,
                    ApiKey = pharmacyDto.ApiKey,
                    Email  = pharmacyDto.Email,
                    ActionsBenefitsExchangeName = pharmacyDto.ActionsBenefitsExchangeName,
                    ActionsBenefitsSubscribed   = pharmacyDto.ActionsBenefitsSubscribed,
                    GrpcAdress = new GrpcAdress(pharmacyDto.GrpcHost, pharmacyDto.GrpcPort)
                };
                await _pharmacySystemService.Create(pharmacy);
            }
            catch (Exception ex)
            {
                return(BadRequest(ModelState));
            }

            TempData["Success"] = "Registration successful!";
            return(RedirectToAction("ApiRegister"));
        }
        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);
                    }
                }
            }
        }
Exemple #4
0
        public IPharmacySystemAdapter SetPharmacySystemAdapter(PharmacySystem pharmacySystem)
        {
            RemoveAdapter();
            _pharmacySystem = pharmacySystem;
            PharmacySystemAdapterParameters parameters = _mapper.Map <PharmacySystemAdapterParameters>(_pharmacySystem);

            parameters.HospitalName = _hospitalName;

            try
            {
                if (_environment == "Development")
                {
                    PharmacySystemAdapter = (IPharmacySystemAdapter)Activator.CreateInstance(Type.GetType($"IntegrationAdapters.Adapters.{_environment}.PharmacySystemAdapter_Id{_pharmacySystem.Id}"));
                    var config = Startup.Configuration.GetSection("SftpConfig");
                    parameters.SftpConfig = new SftpConfig()
                    {
                        Host     = config["Host"],
                        Port     = int.Parse(config["Port"]),
                        Username = config["Username"],
                        Password = config["Password"]
                    };

                    PharmacySystemAdapter.Initialize(parameters, _httpClientFactory.CreateClient());
                }
                else if (_environment == "Production" || _environment == "Test")
                {
                    PharmacySystemAdapter = (IPharmacySystemAdapter)Activator.CreateInstance(Type.GetType($"IntegrationAdapters.Adapters.{_environment}.PharmacySystemAdapter_Id{_pharmacySystem.Id}"));
                    PharmacySystemAdapter.Initialize(parameters, _httpClientFactory.CreateClient());
                }
                else if (_environment == "Production-Multi")
                {
                    if (_pharmacySystem.Id == 1)
                    {
                        PharmacySystemAdapter = (IPharmacySystemAdapter)Activator.CreateInstance(Type.GetType("IntegrationAdapters.Adapters.Development.PharmacySystemAdapter_Id1"));
                        var config = Startup.Configuration.GetSection("SftpConfig");
                        parameters.SftpConfig = new SftpConfig()
                        {
                            Host     = config["Host"],
                            Port     = int.Parse(config["Port"]),
                            Username = config["Username"],
                            Password = config["Password"]
                        };
                    }
                    else if (_pharmacySystem.Id == 2)
                    {
                        PharmacySystemAdapter = (IPharmacySystemAdapter)Activator.CreateInstance(Type.GetType("IntegrationAdapters.Adapters.Production.PharmacySystemAdapter_Id1"));
                    }
                    PharmacySystemAdapter.Initialize(parameters, _httpClientFactory.CreateClient());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                PharmacySystemAdapter = null;
            }

            return(PharmacySystemAdapter);
        }
Exemple #5
0
        public void CreatePharmacy(PharmacySystem p)
        {
            if (p == null)
            {
                throw new ArgumentNullException(nameof(p));
            }

            _context.Pharmacies.Add(p);
            _context.SaveChanges();
        }
Exemple #6
0
        public void PharmacySystemAdapter_SetPharmacy1Adapter_UnimplementedAdapter()
        {
            var            mockFactory    = new Mock <IHttpClientFactory>();
            AdapterContext adapterContext = new AdapterContext(mockFactory.Object);
            PharmacySystem pharmacy       = new PharmacySystem {
                Id = 2, Name = "apoteka-2", ApiKey = "api-2", Url = "url-2", ActionsBenefitsExchangeName = "exchange-2", ActionsBenefitsSubscribed = true, GrpcAdress = new GrpcAdress("localhost", 30051)
            };

            adapterContext.SetPharmacySystemAdapter(pharmacy);

            Assert.True(adapterContext.PharmacySystemAdapter == null);
        }
Exemple #7
0
        public async Task <ActionResult <List <DrugListDTO> > > GetDrugsFromPharmacy(int id)
        {
            List <DrugListDTO> ret            = new List <DrugListDTO>();
            PharmacySystem     pharmacySystem = await _pharmacySystemService.Get(id);

            if (_adapterContext.SetPharmacySystemAdapter(pharmacySystem) != null)
            {
                ret.AddRange(_adapterContext.PharmacySystemAdapter.GetAllDrugs());
            }

            return(Ok(ret));
        }
Exemple #8
0
        private PharmacyController GetPharmacyController()
        {
            var pharmacy = CreatePharmacy.CreateValidTestObject();

            pharmacy.Id = 1;
            PharmacySystem pharmacyNull        = null;
            var            pharmacyServiceMock = new Mock <IPharmacySystemService>();

            pharmacyServiceMock.Setup(p => p.Get(It.Is <int>(i => i == 1))).Returns(Task.Run(() => pharmacy));
            pharmacyServiceMock.Setup(p => p.Get(It.Is <int>(i => i != 1))).Returns(Task.Run(() => pharmacyNull));

            return(new PharmacyController(pharmacyServiceMock.Object));
        }
        public async Task <bool> Update(PharmacySystem pharmacySystem)
        {
            var request = new HttpRequestMessage(HttpMethod.Put, "pharmacysystemservice");

            request.Content = new StringContent(JsonConvert.SerializeObject(pharmacySystem), Encoding.UTF8, "application/json");
            var response = await SendRequest(request);

            if (!response.IsSuccessStatusCode)
            {
                NotSuccessStatusCodeHandler(response.StatusCode);
            }

            return(true);
        }
 public IActionResult Create([FromBody] PharmacySystem pharmacySystem)
 {
     if (pharmacySystem.isValid())
     {
         _pharmacySystemService.CreatePharmacy(pharmacySystem);
         if (pharmacySystem.ActionsBenefitsSubscribed)
         {
             _actionBenefitService.Subscribe(pharmacySystem.ActionsBenefitsExchangeName);
         }
         return(CreatedAtRoute("get", pharmacySystem));
     }
     else
     {
         return(BadRequest());
     }
 }
        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();
        }
        public async Task <IActionResult> Edit(PharmacySystemDTO pharmacyDto)
        {
            PharmacySystem pharmacy = new PharmacySystem()
            {
                Id     = pharmacyDto.Id,
                Name   = pharmacyDto.Name,
                Url    = pharmacyDto.Url,
                ApiKey = pharmacyDto.ApiKey,
                Email  = pharmacyDto.Email,
                ActionsBenefitsExchangeName = pharmacyDto.ActionsBenefitsExchangeName,
                ActionsBenefitsSubscribed   = pharmacyDto.ActionsBenefitsSubscribed,
                GrpcAdress = new GrpcAdress(pharmacyDto.GrpcHost, pharmacyDto.GrpcPort)
            };

            if (await _pharmacySystemService.Update(pharmacy))
            {
                return(RedirectToAction("Index"));
            }
            return(BadRequest("Something went wrong"));
        }
        public async void Index_ReturnsSingleItem()
        {
            var mockaActionBenefitService = new Mock <IActionBenefitService>();
            var pharmacy = new PharmacySystem {
                Id = 1, Name = "a1", ApiKey = "a1", Url = "u1", ActionsBenefitsExchangeName = "e1", ActionsBenefitsSubscribed = true
            };
            var actionsBenefits = new List <ActionBenefit>
            {
                new ActionBenefit {
                    Id = 1, PharmacyId = 1, Pharmacy = pharmacy, Message = new ActionBenefitMessage("s", "m"), IsPublic = true
                }
            };

            mockaActionBenefitService.Setup(r => r.GetAll()).Returns(Task.Run(() => actionsBenefits));
            var controller = new ActionBenefitController(mockaActionBenefitService.Object);

            ViewResult result = (ViewResult)await controller.Index();

            Assert.True(((List <ActionBenefit>)result.Model).Count == 1);
        }
Exemple #15
0
        public DbContextInMemory()
        {
            _builder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            _options = _builder.Options;
            _context = new MyDbContext(_options);

            var pharmacy = new PharmacySystem {
                Id = 1, Name = "apoteka-1", ApiKey = "api-1", Url = "http://localhost:8080", GrpcAdress = new GrpcAdress("localhost", 9090), ActionsBenefitsExchangeName = "exchange", ActionsBenefitsSubscribed = true
            };

            _context.Add(pharmacy);
            _context.Add(new ActionBenefit {
                Id = 1, PharmacyId = 1, Pharmacy = pharmacy, Message = new ActionBenefitMessage("s", "m"), IsPublic = false
            });
            _context.Add(new ActionBenefit {
                Id = 2, PharmacyId = 1, Pharmacy = pharmacy, Message = new ActionBenefitMessage("ss", "mm"), IsPublic = true
            });

            _context.Add(new DrugType("tableta", "lek za glavu"));
            _context.Add(new Drug()
            {
                DrugType_Id = 1, Name = "Brufen", Code = "20033", Quantity = 0
            });

            _context.Add(new Tender()
            {
                Id = 1, Name = "tender-1", EndDate = new DateTime(2021, 5, 5), IsClosed = false, QueueName = "tender-1", RoutingKey = "routing-key-1", Drugs = new List <TenderDrug>()
                {
                    new TenderDrug()
                    {
                        DrugId = 1, Quantity = 10
                    }
                }
            });

            _context.SaveChanges();
        }
Exemple #16
0
 public void UpdatePharmacy(PharmacySystem p)
 {
     _pharmacyRepo.UpdatePharmacy(p);
 }
Exemple #17
0
 public void CreatePharmacy(PharmacySystem p)
 {
     _pharmacyRepo.CreatePharmacy(p);
 }
Exemple #18
0
 public void UpdatePharmacy(PharmacySystem p)
 {
     _context.Update(p);
     _context.SaveChanges();
 }