public MemoryStream Execute() { var productsDict = SupplyOrderCalc.CreateProductList(context); XmlMedicineSupplyModel supply = new XmlMedicineSupplyModel(); foreach (var p in productsDict) { var model = AutoMapper.Mapper.Map <XmlMedicineModel>(context.Medicines.Single(s => s.ID == p.Key)); model.Quantity = (uint)p.Value; supply.Medicines.Add(model); } MemoryStream stream = null; try { stream = serializer.Serialize(supply); fileCopy.Create(stream, "store_order_", ".xml", "XML", "created_orders"); stream.Position = 0; } catch (Exception ex) { logger.LogError(ex.Message, ex); } return(stream); }
public void Should_Not_Add_Anything() { // given var supply = new XmlMedicineSupplyModel { Medicines = new List <XmlMedicineModel> { new XmlMedicineModel { IsNew = true, Category = MedicineCategory.Normal, PricePerOne = 8.50, Quantity = 100, Refundation = 0.80, Name = "Aspiryna Light" }, new XmlMedicineModel { StockId = context.Medicines.First().ID, Quantity = 100, }, new XmlMedicineModel { IsNew = false, Category = MedicineCategory.Special, PricePerOne = 22.22, Quantity = 100, Refundation = 0.80, Name = "Aspiryna MAX" } } }; var fileFormMock = new Mock <IFormFile>(); fileFormMock.Setup(f => f.ContentType).Returns("text/xml"); var fileCopyMock = new Mock <ICopy>(); var loggerMock = new Mock <ILogger <GetXMLStoreUpdateUseCase> >(); var serializerMock = new Mock <ISerializer <MemoryStream, XmlMedicineSupplyModel> >(); serializerMock.Setup(s => s.Deserialize(It.IsAny <MemoryStream>())).Returns(supply); const int expectedStockMedsCount = 1; const int expectedExternalMedsCount = 0; var useCase = new GetXMLStoreUpdateUseCase( context, loggerMock.Object, serializerMock.Object, fileCopyMock.Object); // when var result = useCase.Execute(fileFormMock.Object); var stockMedicinesCount = context.Medicines.Count(); var externalMedicineCount = context.ExternalDrugstoreMedicines.Count(); // then Assert.AreEqual(false, result.Succes); Assert.AreEqual(stockMedicinesCount, expectedStockMedsCount); Assert.AreEqual(externalMedicineCount, expectedExternalMedsCount); Assert.IsTrue(context.Medicines.First().Name == "Lek testowy"); }
private ResultViewModel <Dictionary <string, object> > UpdateStore(XmlMedicineSupplyModel supply) { double pricePerOne; double totalMedicinesCost = 0.0f; int totalMedicineCount = 0; var medicines = context.ExternalDrugstoreMedicines; foreach (var med in supply.Medicines) { if (med.StockId.HasValue) { var medicine = medicines .Include(m => m.StockMedicine) .SingleOrDefault(m => m.StockMedicine.ID == med.StockId); if (medicine == null) { throw new MedicineNotFoundException($"Id == {med.StockId}"); } if ((int)((int)medicine.Quantity - (int)med.Quantity) < 0) { throw new OnStockMedicineQuantityException($"External Drugstore, " + $"ID: {med.StockId} Available quantity: {medicine.Quantity} Sold Quantity: {med.Quantity}"); } medicine.Quantity -= med.Quantity; pricePerOne = med.PricePerOne ?? medicine.StockMedicine.PricePerOne; context.ExternalDrugstoreSoldMedicines.Add(new ExternalDrugstoreSoldMedicine { Date = DateTime.Now, SoldQuantity = (int)med.Quantity, StockMedicine = medicine.StockMedicine, PricePerOne = pricePerOne }); totalMedicinesCost += med.Quantity * pricePerOne; totalMedicineCount += (int)med.Quantity; } else { throw new MedicineNotFoundException("Medicine's Id is null"); } } context.SaveChanges(); return(new ResultViewModel <Dictionary <string, object> > { Succes = true, Data = new Dictionary <string, object> { { "TotalCount", totalMedicineCount }, { "TotalCost", totalMedicinesCost } }, Message = "" }); }
public void SetUp() { MapperDependencyResolver.Resolve(); context = new DrugstoreDbContext(options); var med1 = new Core.MedicineOnStock { MedicineCategory = Core.MedicineCategory.Normal, Name = "F;irst Medicine", PricePerOne = 25.66, Quantity = 100, Refundation = 0.2 }; var med2 = new Core.MedicineOnStock { MedicineCategory = Core.MedicineCategory.Normal, Name = "Second Medicine", PricePerOne = 6.66, Quantity = 100, Refundation = 0.1 }; var med3 = new Core.MedicineOnStock { MedicineCategory = Core.MedicineCategory.Normal, Name = "Third Medicine", PricePerOne = 22.66, Quantity = 100, Refundation = 0.25 }; context.Medicines .AddRange(new Core.MedicineOnStock [] { med1, med2, med3 }); context.SaveChanges(); var exMed1 = new Core.ExternalDrugstoreMedicine { StockMedicine = med1, Name = med1.Name, PricePerOne = med1.PricePerOne, Quantity = initialQuantity, }; var exMed2 = new Core.ExternalDrugstoreMedicine { StockMedicine = med2, Name = med2.Name, PricePerOne = med2.PricePerOne, Quantity = initialQuantity, }; var exMed3 = new Core.ExternalDrugstoreMedicine { StockMedicine = med3, Name = med3.Name, PricePerOne = med3.PricePerOne, Quantity = initialQuantity, }; context.ExternalDrugstoreMedicines .AddRange(new Core.ExternalDrugstoreMedicine [] { exMed1, exMed2, exMed3 }); context.SaveChanges(); var sold1 = new ExternalDrugstoreSoldMedicine { StockMedicine = med1, PricePerOne = med1.PricePerOne, Date = DateTime.Now, SoldQuantity = 10 }; var sold2 = new ExternalDrugstoreSoldMedicine { StockMedicine = med2, PricePerOne = med2.PricePerOne, Date = DateTime.Now, SoldQuantity = 10 }; var sold3 = new ExternalDrugstoreSoldMedicine { StockMedicine = med3, PricePerOne = med3.PricePerOne, Date = DateTime.Now, SoldQuantity = 10 }; context.ExternalDrugstoreSoldMedicines .AddRange(new ExternalDrugstoreSoldMedicine [] { sold1, sold2, sold3 }); context.SaveChanges(); supply = new XmlMedicineSupplyModel { Medicines = new List <XmlMedicineModel> { new XmlMedicineModel { StockId = med1.ID, Quantity = 10, }, new XmlMedicineModel { StockId = med2.ID, Quantity = 10, }, new XmlMedicineModel { StockId = med3.ID, Quantity = 20, } } }; }
private UploadResultViewModel UpdateStore(XmlMedicineSupplyModel supply) { int totalUpdated = 0; int totalAdded = 0; int totalNewInExDrugstore = 0; int totalNewOnStock = 0; foreach (var med in supply.Medicines) { if (med.StockId == null) { if (!med.IsNew) { throw new MedicineNotFoundException($"No Id found and is not marked as new"); } var existingMedicine = context.Medicines .FirstOrDefault(m => m.Name.Contains(med.Name, StringComparison.OrdinalIgnoreCase)); if (existingMedicine != null) { throw new MedicineNameAlreadyExistsExcpetion(med.Name); } totalNewOnStock++; totalNewInExDrugstore++; totalAdded += (int)med.Quantity; var onStockMedicine = AutoMapper.Mapper.Map <MedicineOnStock>(med); onStockMedicine.Quantity = 0; context.Medicines.Add(onStockMedicine); context.ExternalDrugstoreMedicines.Add(new Core.ExternalDrugstoreMedicine { Name = onStockMedicine.Name, PricePerOne = onStockMedicine.PricePerOne, Quantity = med.Quantity, StockMedicine = onStockMedicine }); } else { var medicineOnStock = context.Medicines.SingleOrDefault(m => m.ID == med.StockId); if (medicineOnStock == null) { throw new MedicineNotFoundException($"Id = {med.StockId}"); } var medicine = context.ExternalDrugstoreMedicines .SingleOrDefault(m => m.StockMedicine.ID == medicineOnStock.ID); if (medicine == null) { totalNewInExDrugstore++; context.ExternalDrugstoreMedicines.Add(new ExternalDrugstoreMedicine { Name = medicineOnStock.Name, PricePerOne = med.PricePerOne ?? 0.0d, Quantity = med.Quantity, StockMedicine = medicineOnStock }); } else { totalUpdated++; medicine.Quantity += med.Quantity; medicine.PricePerOne = med.PricePerOne ?? medicine.PricePerOne; } totalAdded += (int)med.Quantity; } } context.SaveChanges(); return(new UploadResultViewModel { Succes = true, Message = "", Data = new Dictionary <string, object> { { "Updated", totalUpdated }, { "Added", totalAdded }, { "NewInEx", totalNewInExDrugstore }, { "NewOnStock", totalNewOnStock } } }); }