コード例 #1
0
ファイル: StreamService.cs プロジェクト: huaminglee/FlexFWD
        public long SaveRecipe(long uid, long pid, Affine.Data.json.StreamData recipe)
        {
            aqufitEntities entities = new aqufitEntities();
            UserSettings settings = entities.UserSettings.FirstOrDefault(us => us.UserKey == uid && us.PortalKey == pid);
            Recipe r = null;
            RecipeExtended re = null;
            if (recipe.Id > 0)
            {
                r = entities.UserStreamSet.OfType<Recipe>().Include("RecipeExtendeds").FirstOrDefault(us => us.UserSetting.UserKey == uid && us.PortalKey == pid && us.Id == recipe.Id);
                re = r.RecipeExtendeds.First();
                RecipeIngredient[] riOld = entities.RecipeIngredients.Where(ri => ri.RecipeExtended.Id == re.Id).ToArray();
                foreach (RecipeIngredient ri in riOld)
                {
                    entities.DeleteObject(ri);  // delete and re add new ing
                }
            }else{
                r = new Recipe();
                re = new RecipeExtended();
                re.NumRatings = 1;
                re.NumStrictness = 1;
                re.UserStream = r;
                entities.AddToUserStreamSet(r);
                r.Date = DateTime.Now.ToUniversalTime();
            }
            r.PortalKey = pid;
            r.UserSetting = settings;
            r.TimeStamp = DateTime.Now.ToUniversalTime();
            r.Title = recipe.Title;
            r.Description = recipe.Description;
            r.AvrStrictness = recipe.AvrStrictness;
            r.AvrRating = recipe.AvrRating;
            r.NumServings = recipe.NumServings;
            r.Tags = recipe.Tags;
            r.TimeCook = recipe.TimeCook;
            r.TimePrep = recipe.TimePrep;
            re.Directions = recipe.RecipeExtended.Directions;
            RecipeIngredient[] riArray = recipe.RecipeExtended.Ingredients.Select(i => new RecipeIngredient() { Text = i.Name, RecipeExtended = re }).ToArray();
            entities.SaveChanges();

            // Update the metrics for this user
            if (recipe.Id == 0) // if thie is NOT an edit
            {
                Metric met = entities.Metrics.FirstOrDefault(m => m.UserSetting.UserKey == uid && m.UserSetting.PortalKey == pid && m.MetricType == (int)Affine.Utils.MetricUtil.MetricType.NUM_RECIPES);
                if (met == null)
                {
                    met = new Metric()
                    {
                        UserSetting = settings,
                        MetricType = (int)Affine.Utils.MetricUtil.MetricType.NUM_RECIPES,
                        MetricValue = "1"       // This is the first recipe they have saved
                    };
                    entities.AddToMetrics(met);
                }
                else
                {
                    met.MetricValue = "" + (Convert.ToInt32(met.MetricValue) + 1);
                }
                entities.SaveChanges();
                UserStream ret = entities.UserStreamSet.OfType<Recipe>().Where(u => u.PortalKey == pid && u.UserSetting.UserKey == uid).OrderByDescending(u => u.Id).FirstOrDefault();
                return ret.Id;

            }
            return recipe.Id;
        }
コード例 #2
0
ファイル: StreamService.cs プロジェクト: huaminglee/FlexFWD
        public string FollowUser(long UserSettingsId, long fid)
        {
            // ok so the user has accepted the friend request. ( Followed user is always DestUserKey )
            // 1) Lets add the user to the firends
            aqufitEntities entities = new aqufitEntities();
            // Make sure that this not the same user
            if (UserSettingsId == fid)
            {
                return "{ 'status':'invalid' }";
            }
            // make sure it is not already in the db
            UserSettings you = entities.UserSettings.FirstOrDefault(s => s.Id == UserSettingsId);
            UserSettings friend = entities.UserSettings.FirstOrDefault(s => s.Id == fid );
            // check if they are already friends or in the watch list
            UserFriends uf = entities.UserFriends.FirstOrDefault(f => f.SrcUserSettingKey == UserSettingsId && f.DestUserSettingKey == fid || f.SrcUserSettingKey == fid && f.DestUserSettingKey == UserSettingsId);
            if (uf != null && uf.Relationship == (int)Affine.Utils.ConstsUtil.Relationships.FRIEND )
            {   // you can not "follow" your friends...
                return "{ 'status':'invalid' }";
            }
            else if (uf != null && uf.SrcUserSettingKey == UserSettingsId && uf.Relationship == (int)Affine.Utils.ConstsUtil.Relationships.FOLLOW)
            {   // you are already following...
                throw new Exception("You are already following this user.");
            }
            UserFriends uf2 = new UserFriends()
            {
                SrcUserSettingKey = you.Id,
                DestUserSettingKey = friend.Id,
                PortalKey = you.PortalKey,
                Relationship = (int)Affine.Utils.ConstsUtil.Relationships.FOLLOW
            };
            entities.AddToUserFriends(uf2);
            entities.SaveChanges();

            // Add the Num people you follow metric
            Metric met = entities.Metrics.FirstOrDefault(m => m.UserSetting.Id == you.Id && m.MetricType == (int)Affine.Utils.MetricUtil.MetricType.NUM_YOU_FOLLOW);
            if (met != null)
            {
                met.MetricValue = "" + (Convert.ToInt32(met.MetricValue) + 1);
            }
            else
            {
                met = new Metric()
                {
                    MetricType = (int)Affine.Utils.MetricUtil.MetricType.NUM_YOU_FOLLOW,
                    MetricValue = "1",
                    UserSetting = you
                };
                entities.AddToMetrics(met);
            }
            // Add the Number of follwers to the other user metric
            Metric met2 = entities.Metrics.FirstOrDefault(m => m.UserSetting.Id == friend.Id && m.MetricType == (int)Affine.Utils.MetricUtil.MetricType.NUM_FOLLOWERS);
            if (met2 != null)
            {
                met2.MetricValue = "" + (Convert.ToInt32(met2.MetricValue) + 1);
            }
            else
            {
                met2 = new Metric()
                {
                    MetricType = (int)Affine.Utils.MetricUtil.MetricType.NUM_FOLLOWERS,
                    MetricValue = "1",
                    UserSetting = friend
                };
                entities.AddToMetrics(met2);
            }
            entities.SaveChanges();

            // TODO:
            //sendUserFollowEmailAsync(src, dst);

            return "{ 'status':'success' }";
        }