public static string ToDebugString(this UserPhoto arg) { var key = string.Format("Key: {{[{0}] '{1}' {2}}}", arg.Topic, arg.IdUser, arg.IdContent); StringBuilder flags = new StringBuilder(); int n = 0; if (arg.Likes) { flags.Append(n++ == 0 ? "Like" : ", Like"); } if (arg.Dislikes) { flags.Append(n++ == 0 ? "Dislike" : ", Dislike"); } if (arg.Stars) { flags.Append(n++ == 0 ? "Starred" : ", Starred"); } if (arg.Shares) { flags.Append(n++ == 0 ? "Shared" : ", Shared"); } return(key + ": " + flags); }
public UserPhoto GetUserPhoto(string topic, string idUser, string idContent) { var userPhoto = Db.Lookup(DataModelExtensions.ToUserPhotoKey(topic, idUser, idContent)).ToUserPhoto(); if (userPhoto == null) { userPhoto = new UserPhoto() { IdUser = idUser, IdContent = idContent } } ; return(userPhoto); }
public static Entity ToEntity(this UserPhoto userPhoto) { return(new Entity { Key = ToUserPhotoKey(userPhoto.Topic, userPhoto.IdUser, userPhoto.IdContent), ["Stars"] = new Value() { BooleanValue = userPhoto.Stars, ExcludeFromIndexes = true }, ["Likes"] = new Value() { BooleanValue = userPhoto.Likes, ExcludeFromIndexes = true }, ["Dislikes"] = new Value() { BooleanValue = userPhoto.Dislikes, ExcludeFromIndexes = true }, ["Shares"] = new Value() { BooleanValue = userPhoto.Shares, ExcludeFromIndexes = true }, }); }
// false means Storage does not need updates private bool ApplyAction(Content content, UserPhoto userPhoto, UserAction action) { int deltaStars = 0, deltaLike = 0, deltaDislike = 0, deltaShare = 0; switch (action) { case UserAction.Star: if (userPhoto.Stars) { return(false); } deltaStars = 1; userPhoto.Stars = true; break; case UserAction.Like: if (userPhoto.Likes) { return(false); } else if (userPhoto.Dislikes) { deltaDislike = -1; userPhoto.Dislikes = false; } deltaLike = 1; userPhoto.Likes = true; break; case UserAction.Dislike: if (userPhoto.Dislikes) { return(false); } else if (userPhoto.Likes) { deltaLike = -1; userPhoto.Likes = false; } deltaDislike = 1; userPhoto.Dislikes = true; break; case UserAction.Share: deltaShare = 1; userPhoto.Shares = true; break; default: throw new ArgumentException($"Action {action} is not supported"); } content.Dislikes += deltaDislike; content.Likes += deltaLike; content.Shares += deltaShare; content.Stars += deltaStars; return(true); }
public void AddUserAction(string topic, string idUser, string idContent, UserAction action) { // User 'Tester' on [Liked-content] topic 'One Topic' /* * if (Debugger.IsAttached && topic == "One Topic" && idUser == "Tester" && idContent == "Liked-content") * Debugger.Break(); */ Stopwatch sw = Stopwatch.StartNew(); String debug = $"{action,-7} by User '{idUser}' on [{idContent}] topic '{topic}'"; try { /* * var userPhotoKey = DataModelExtensions.ToUserPhotoKey(topic, idContent); * var contentIs = Filter.Equal("__key__", userPhotoKey); * var userIs = Filter.Equal("IdUser", idUser); * Query querySingleUserPhoto = new Query("UserPhoto") * { * Filter = Filter.And(contentIs, userIs) * }; * var userPhoto = Db.RunQuery(querySingleUserPhoto).Entities.FirstOrDefault().ToUserPhoto(); */ var userPhoto = Db.Lookup(DataModelExtensions.ToUserPhotoKey(topic, idUser, idContent)).ToUserPhoto(); bool isUserPhotoNew = userPhoto == null; if (userPhoto == null) { userPhoto = new UserPhoto() { Topic = topic, IdUser = idUser, IdContent = idContent } } ; var contentKey = DataModelExtensions.ToContentKey(topic, idContent); var content = Db.Lookup(contentKey).ToContent(); if (content == null) { content = new Content() { Topic = topic, IdContent = idContent } } ; if (!ApplyAction(content, userPhoto, action)) { Console.WriteLine($"None: {debug} in {sw.Elapsed}"); return; } Entity entityContent = content.ToEntity(); Entity entityUserPhoto = userPhoto.ToEntity(); Db.Upsert(entityContent, entityUserPhoto); Console.WriteLine($"DONE: {debug} in {sw.Elapsed}"); } catch (Exception ex) { Console.WriteLine($"FAIL: {debug} in {sw.Elapsed}:" + Environment.NewLine + " " + ex.GetExceptionDigest()); throw new Exception("AddAction failed " + debug, ex); } }