public async Task Update(User user) { var watch = new Stopwatch(); watch.Start(); try { var res = await client.ReplaceDocumentAsync(GetDocumentUri(user.userId), user, new RequestOptions { PartitionKey = new PartitionKey(user.pk), AccessCondition = new AccessCondition { Condition = user.ETag, Type = AccessConditionType.IfMatch }, }); Console.WriteLine(String.Format("Update / RU: {0} / Elapsed: {1}", res.RequestCharge, watch.Elapsed.TotalSeconds)); // update the version of current document user.SetPropertyValue("_etag", ((Document)res).ETag); } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.PreconditionFailed) { Console.WriteLine("ReplaceDocumentAsync : doc version expired!(concurrency)"); } throw e; } }
/// <summary> /// 更新或新增使用者 /// </summary> /// <param name="user"></param> /// <returns></returns> public bool upsertUser(UW.Core.Persis.Collections.User user) { try { var res = client.UpsertDocumentAsync(URI_USER, user).Result; return(res.StatusCode == HttpStatusCode.OK || res.StatusCode == HttpStatusCode.Created); } catch (System.Exception e) { Console.WriteLine(e.ToString()); } return(false); }
/// <summary> /// Create user /// </summary> /// <param name="uid"></param> /// <param name="jwtHash"></param> /// <param name="phoneno"></param> /// <param name="realname"></param> /// <param name="avatar"></param> /// <returns></returns> public async Task <User> Create(Pkuid uid, string jwtHash, string phoneno, string realname = "", string avatar = "") { await client.ClearCollectionAsync(User._URI_COL); var user = new User() { userId = uid.ToString(), pk = uid.PK, jwtHash = jwtHash, phoneno = phoneno, name = realname, realname = realname, nickname = realname, avatar = avatar, aliasId = uid.PkIdx + "-" + F.NewShortGuid(), currencies = DefCurrencies, }; try { var res = await client.CreateDocumentAsync(User._URI_COL, user, new RequestOptions { PartitionKey = new PartitionKey(uid.PK) }); Console.WriteLine(String.Format("Create User / RU: {0}", res.RequestCharge)); } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.Conflict) { Console.WriteLine("Conflict!! data:{0}", user.ToJson()); } else { Console.WriteLine("==CreateDocumentAsync Unknown Exception================"); Console.WriteLine(e.Message); } throw e; } return(user); }
private void Mockup() { var userEM = new { phoneno = "BANK", name = "BANK", avatar = "https://images.vexels.com/media/users/3/135318/isolated/preview/45939241515a561751ed6222f2012003-bank-square-icon-by-vexels.png" }; var userGPK = new { phoneno = "GPK", name = "GPK", avatar = "https://www.bankatunited.com/UnitedBank/media/Homepage-Promos/small-business-icon.png?width=200&height=200&ext=.png" }; var user1 = new { phoneno = "886986123456", name = "buzz", avatar = "https://ionicframework.com/dist/preview-app/www/assets/img/avatar-ts-buzz.png" }; var user2 = new { phoneno = "886986123457", name = "jessie", avatar = "https://ionicframework.com/dist/preview-app/www/assets/img/avatar-ts-jessie.png" }; foreach (var u in new dynamic[] { userEM, user1, user2 }) { var user = new UW.Core.Persis.Collections.User() { userId = "tempid-" + u.phoneno, //todo : 暫時以phoneno綁定id 便於識別 (日後移除) phoneno = u.phoneno, name = u.name, avatar = u.avatar, currencies = new List <CurrencySettings> { new CurrencySettings { name = D.CNY, order = 0, isDefault = true, isVisible = false }, new CurrencySettings { name = D.USD, order = 1, isDefault = false, isVisible = false }, new CurrencySettings { name = D.BTC, order = 2, isDefault = false, isVisible = false }, new CurrencySettings { name = D.ETH, order = 3, isDefault = false, isVisible = false } } }; if (upsertUser(user)) { user = getUserByPhone(u.phoneno); } var friends = new List <Friend> { }; addFriends(user.userId, friends); updateBalance(user.userId, new Dictionary <string, decimal>()); } }