public void DeleteLikeMatching(LikeMatching pLikeMatching)
 {
     using (TransactionScope lScope = new TransactionScope())
     using (VideoStoreEntityModelContainer lContainer = new VideoStoreEntityModelContainer())
     {
         lContainer.LikeMatchings.DeleteObject(pLikeMatching);
         lContainer.SaveChanges();
         lScope.Complete();
     }
 }
 public void UpdateLikeMatching(LikeMatching pLikeMatching)
 {
     using (TransactionScope lScope = new TransactionScope())
     using (VideoStoreEntityModelContainer lContainer = new VideoStoreEntityModelContainer())
     {
         lContainer.LikeMatchings.Attach(pLikeMatching);
         lContainer.ObjectStateManager.ChangeObjectState(pLikeMatching, System.Data.EntityState.Modified);
         lContainer.SaveChanges();
         lScope.Complete();
     }
 }
Esempio n. 3
0
        private void FixupMostLikeMatching(LikeMatching previousValue)
        {
            if (IsDeserializing)
            {
                return;
            }

            if (ChangeTracker.ChangeTrackingEnabled)
            {
                if (ChangeTracker.OriginalValues.ContainsKey("MostLikeMatching")
                    && (ChangeTracker.OriginalValues["MostLikeMatching"] == MostLikeMatching))
                {
                    ChangeTracker.OriginalValues.Remove("MostLikeMatching");
                }
                else
                {
                    ChangeTracker.RecordOriginalValue("MostLikeMatching", previousValue);
                }
                if (MostLikeMatching != null && !MostLikeMatching.ChangeTracker.ChangeTrackingEnabled)
                {
                    MostLikeMatching.StartTracking();
                }
                FixupMostLikeMatchingKeys();
                if (previousValue != null)
                {
                    previousValue.FixupVideoStoreEntityModel_RecommendationLikeMatching1_RecommendationKeys(null, false);
                }
                if (MostLikeMatching != null)
                {
                    MostLikeMatching.FixupVideoStoreEntityModel_RecommendationLikeMatching1_RecommendationKeys(this, false);
                }
            }
        }
        public void UserLikeAnMedia(int pUserId, int pMediaId)
        {
            if (pUserId == null || pMediaId == null) {
                return;
            }

            using (TransactionScope lScope = new TransactionScope())
            using (VideoStoreEntityModelContainer lContainer = new VideoStoreEntityModelContainer())
            {
                lContainer.ContextOptions.LazyLoadingEnabled = false;
                var pUser = lContainer.Users.Include("Medium").FirstOrDefault(lUser => lUser.Id == pUserId);
                var pMedia = lContainer.Media.Include("Recommendation").Where(lMedia => lMedia.Id == pMediaId).FirstOrDefault();

                if (pUser == null || pMedia == null) {
                    return;
                }

                //get the List of the medias which are liked by this user
                List<Media> LikedMedium  = pUser.Medium.ToList();
                foreach( Media tMedia in LikedMedium ){
                    if (tMedia.Id == pMedia.Id) {
                        return;
                    }
                }

                // when no one has liked this Media before, create a recommendation model attached to this media
                if (pMedia.Recommendation == null)
                {
                    Recommendation newRec = new Recommendation();
                    newRec.Medium = pMedia;
                    newRec.MostLikeMatching = null;
                    lContainer.Recommendations.AddObject(newRec);
                    lContainer.SaveChanges();
                }

                var pRecommendation = new Recommendation();
                var curRecommendation = pMedia.Recommendation;// the recommendation of the new liked Media
                List<LikeMatching> pLikeMatchings  = new List<LikeMatching>();

                //iterate the media list & update all the LikeMatchings
                foreach (Media tMedia in LikedMedium)
                {
                    // update LikeWatching list attached to every of the Recommendation of this media
                    pRecommendation = lContainer.Recommendations.Include("LikeMatchings")
                        .Where( (lRecommendation) => lRecommendation.Medium.Id == tMedia.Id ).FirstOrDefault();

                    //pLikeMatchings = pRecommendation.LikeMatchings.ToList();
                    pLikeMatchings = lContainer.LikeMatchings.Include("Medium").Where(lLikeMatching => lLikeMatching.Recommendation.Id == pRecommendation.Id).ToList();
                    LikeMatching MatchingToUpdate = GetLikeMatchingMediaIn(pMedia,pLikeMatchings);
                    if( MatchingToUpdate != null ){
                        // if this media is in the previous LikeMatching list of
                        //this recommendation, increment the count by 1
                        MatchingToUpdate.count++;
                        lContainer.LikeMatchings.Attach(MatchingToUpdate);
                        lContainer.ObjectStateManager.ChangeObjectState(MatchingToUpdate, System.Data.EntityState.Modified);
                    }
                    else{//add this media to the LikeMatching List of this Recommendation and set the count as 1
                        MatchingToUpdate = new LikeMatching();
                        MatchingToUpdate.Recommendation = pRecommendation;
                        MatchingToUpdate.Medium = pMedia;
                        MatchingToUpdate.count = 1;
                        lContainer.LikeMatchings.AddObject(MatchingToUpdate);
                        lContainer.SaveChanges();
                    }

                    // recalculate the most frequently liked media of this Recomendtaion
                    pRecommendation.MostLikeMatching = (from LikingItem in pRecommendation.LikeMatchings
                                                            orderby LikingItem.count descending
                                                            select LikingItem).FirstOrDefault();
                    lContainer.Recommendations.Attach(pRecommendation);
                    lContainer.ObjectStateManager.ChangeObjectState(pRecommendation, System.Data.EntityState.Modified);
                    //lContainer.SaveChanges();

                    //update the LikeMatching list for the new liked media
                    List<LikeMatching> tLikeMatchings = lContainer.LikeMatchings.Include("Medium").Where(lLikeMatching => lLikeMatching.Recommendation.Id == curRecommendation.Id).ToList();
                    MatchingToUpdate = (from LikeItem in tLikeMatchings
                                        where LikeItem.Medium.Id == tMedia.Id
                                        select LikeItem).FirstOrDefault();
                    if (MatchingToUpdate != null)
                    {
                        // if this media is in the previous LikeMatching list of
                        // this recommendation, increment the count by 1
                        MatchingToUpdate.count++;
                        lContainer.LikeMatchings.Attach(MatchingToUpdate);
                        lContainer.ObjectStateManager.ChangeObjectState(MatchingToUpdate, System.Data.EntityState.Modified);
                    }
                    else {
                        // add this media to the LikeMatching List of this Recommendation and set the count as 1
                        MatchingToUpdate = new LikeMatching();
                        MatchingToUpdate.Recommendation = curRecommendation;
                        MatchingToUpdate.Medium = tMedia;
                        MatchingToUpdate.count = 1;
                        lContainer.LikeMatchings.AddObject(MatchingToUpdate);
                        lContainer.SaveChanges();
                    }

                }

                // recalculate the most frequently liked media of this Recomendtaion
                curRecommendation.MostLikeMatching = (from LikingItem in curRecommendation.LikeMatchings
                                                    orderby LikingItem.count descending
                                                    select LikingItem).FirstOrDefault();
                lContainer.Recommendations.Attach(curRecommendation);
                lContainer.ObjectStateManager.ChangeObjectState(curRecommendation, System.Data.EntityState.Modified);

                //put the media in this user's Media-like list
                pUser.Medium.Add(pMedia);
                lContainer.Users.Attach(pUser);
                lContainer.ObjectStateManager.ChangeObjectState(pUser, System.Data.EntityState.Modified);

                lContainer.SaveChanges();
                lScope.Complete();
            }
        }