public Content GetContent(string topic, string idContent) { var content = Db.Lookup(DataModelExtensions.ToContentKey(topic, idContent)).ToContent(); if (content == null) { content = new Content() { Topic = topic, IdContent = idContent } } ; return(content); }
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); } }