Inheritance: IUnitOfWork, IDisposable
 /// <summary>
 /// Removes the user from a given hub
 /// </summary>
 /// <param name="warehouseID">The Hub ID.</param>
 /// <param name="userID">The user ID.</param>
 public void RemoveUser(int warehouseID, int userID)
 {
     IUnitOfWork repository = new UnitOfWork();
     UserProfile uProfile = repository.UserProfile.FindById(userID);
     var associations = from v in uProfile.UserHubs
                        where v.HubID == warehouseID
                        select v;
     if (associations.Any())
     {
         UserHub userHub = associations.FirstOrDefault();
         if (userHub != null) repository.UserHub.DeleteByID(userHub.UserHubID);
     }
 }
 public static string Translate(this HtmlHelper helper, string text)
 {
     text = text.Trim();
        // IUnitOfWork repository = new UnitOfWork();
     UnitOfWork unitOfWork = new UnitOfWork();
     TranslationService translationService = new TranslationService(unitOfWork);
     if (helper.GetCurrentUser() != null)
     {
         var langauge = helper.GetCurrentUser().LanguageCode;
         return translationService.GetForText(text, langauge);
     }
     return translationService.GetForText(text, "en");
 }
 //TODO: Re-implement these two methods.
 /// <summary>
 /// Adds the user to a hub
 /// </summary>
 /// <param name="warehouseID">The warehouse ID.</param>
 /// <param name="userID">The user ID.</param>
 public void AddUser(int warehouseID, int userID)
 {
     IUnitOfWork repository = new UnitOfWork();
     UserProfile uProfile = repository.UserProfile.FindById(userID);
     var associations = from v in uProfile.UserHubs
                        where v.HubID == warehouseID
                        select v;
     if (associations.Count() == 0)
     {
         var userHub = new UserHub
         {
             UserProfileID = uProfile.UserProfileID,
             HubID = warehouseID
         };
         repository.UserHub.Add(userHub);
     }
 }
        public CommodityTypeRepositoryTest()
        {
            List<CommodityType> testCommoditTypes = new List<CommodityType>
                {

                    new CommodityType {  CommodityTypeID = 1,Name = "Food" },
                    new CommodityType {  CommodityTypeID = 2, Name = "Non Food"},
                    new CommodityType {  CommodityTypeID = 3, Name = "Equipments"}
                };

            // Mock the Commoditytype Repository using Moq
            Mock<IUnitOfWork> mockCommodityTypeRepository = new Mock<IUnitOfWork>();

            // Return all the Commodity types
            mockCommodityTypeRepository.Setup(mr => mr.CommodityType.GetAll()).Returns(testCommoditTypes);

            // return a Commoditytype by CommoditytypeId
            mockCommodityTypeRepository.Setup(mr => mr.CommodityType.FindById(
               It.IsAny<int>())).Returns((int i) => testCommoditTypes.Where(x => x.CommodityTypeID == i).Single());

            //return a commoditytype by it's name
            mockCommodityTypeRepository.Setup(mr => mr.CommodityType.GetCommodityByName(
                 It.IsAny<string>())).Returns((string i) => testCommoditTypes.Where(x => x.Name == i).Single());

            // delete a Commoditytype by CommodityId
            mockCommodityTypeRepository.Setup(mr => mr.CommodityType.DeleteByID(
               It.IsAny<int>())).Returns(
               (int i) =>
               {
                   var original = testCommoditTypes.Single
                       (q => q.CommodityTypeID == i);

                   //see if there is a reference in side the commodity table
                   var testCommodities = new UnitOfWork().Commodity.GetAll();

                   var childsCount = testCommodities.Count(c => c.CommodityTypeID == i);

                   if (original == null || childsCount != 0)
                   {
                       return false;
                   }
                   else
                   {
                       testCommoditTypes.Remove(original);
                       return true;
                   }

               });

            //test deletion of commodity
                    mockCommodityTypeRepository.Setup(mr => mr.CommodityType.Delete(
                       It.IsAny<CommodityType>())).Returns(
                       (CommodityType target) =>
                       {
                           var original = testCommoditTypes.Single
                               (q => q.CommodityTypeID == target.CommodityTypeID);

                           //var childsCount = testCommodities.Count(c => c.ParentID == target.CommodityID);

                           //see if there is a reference in side the commodity table
                           var testCommoditiesCollection = new UnitOfWork().Commodity.GetAll();

                           var childsCount = testCommoditiesCollection.Count(c => c.CommodityTypeID == target.CommodityTypeID);

                           if (original == null || childsCount != 0)
                           {
                               return false;
                           }
                           else
                           {
                               testCommoditTypes.Remove(original);
                               return true;
                           }

                       });

            //returns bool if we can save one (editing )scheme
            mockCommodityTypeRepository.Setup(mr => mr.CommodityType.SaveChanges(It.IsAny<CommodityType>())).Returns(
                (CommodityType target) =>
                {

                    var original = testCommoditTypes.Single
                            (q => q.CommodityTypeID == target.CommodityTypeID);

                    if (original == null)
                    {
                        return false;
                    }
                    original.Name = target.Name;
                    return true;
                });

            //TODO remove the lines below duplicate of the
            mockCommodityTypeRepository.Setup(mr => mr.CommodityType.Add(It.IsAny<CommodityType>())).Returns(
                (CommodityType target) =>
                {
                    if (target.CommodityTypeID.Equals(default(int)))
                    {
                        target.CommodityTypeID = testCommoditTypes.Count() + 1;
                        testCommoditTypes.Add(target);
                    }
                    else
                    {
                        var original = testCommoditTypes.Single
                            (q => q.CommodityTypeID == target.CommodityTypeID);

                        if (original == null)
                        {
                            return false;
                        }
                        original.Name = target.Name;
                    }

                    return true;
                });

            this.MockCommodityTypeRepository = mockCommodityTypeRepository.Object;
        }
        public void Find_Commodity_Type_By_Id_Test()
        {
            IUnitOfWork target = new UnitOfWork();
            // Try finding a product by id
            CommodityType testProduct = this.MockCommodityTypeRepository.CommodityType.FindById(2);

            Assert.IsNotNull(testProduct);
            Assert.IsInstanceOfType(testProduct, typeof(CommodityType)); // Test type
            Assert.AreEqual("Non Food", testProduct.Name); // Verify it is the right product
        }
 public void CommodityTypeRepositoryConstructorTest()
 {
     IUnitOfWork commodityRepository = new UnitOfWork();
 }
        public string Parse(int certificateId, int templateId)
        {
            IUnitOfWork repository = new UnitOfWork();
            LetterTemplateService letterService = new LetterTemplateService();
            GiftCertificateService giftService = new GiftCertificateService(repository);

            BLL.GiftCertificate gift = giftService.FindById(certificateId);

            BLL.LetterTemplate template = letterService.FindById(templateId);
            string raw = HttpContext.Current.Server.HtmlDecode(template.Template);
            string tableString = string.Empty;
            int startIndex = raw.IndexOf("<table id=\"detail\"");//("<table id=\"detail\" style=\"width:100%;\">")
            if (startIndex != -1)
            {
                int lastIndex = raw.IndexOf("</table>", startIndex) + 8;
                if (lastIndex != -1)
                {
                    int length = lastIndex - startIndex;
                    tableString = raw.Substring(startIndex, length);
                }
            }
            if(@tableString != "")
                raw = raw.Replace(@tableString, "{tablePlaceHolder}");
            raw = PopulateCertificate(raw, gift);
            string populatedTable = string.Empty;
            if(@tableString != "")
                populatedTable = PopulateTableData(gift.GiftCertificateDetails.ToList(), tableString.Replace("&nbsp;", ""));
            string finalString = raw.Replace("{tablePlaceHolder}", populatedTable);

            return finalString;
        }