public string UpdateStore(Guid userId, Guid appId, Diff diff) { using (var context = new SsoModel()) { var currentStore = context.SsoStores.SingleOrDefault(s => s.UserId == userId && s.AppId == appId); if (currentStore != null) { if (currentStore.Store.GetSHA1() != diff.Hash) throw ExceptionHelper.Conflict("hash is not relevant"); var store = string.Concat( currentStore.Store.Substring(0, diff.Start), diff.Value, currentStore.Store.Substring(currentStore.Store.Length - diff.End, diff.End)); currentStore.Store = store; context.SaveChanges(); return store.GetSHA1(); } context.SsoStores.Add(new SsoStore { AppId = appId, UserId = userId, Store = diff.Value }); context.SaveChanges(); return diff.Value.GetSHA1(); } }
public string UpdateStore(Guid userId, Guid appId, Diff diff) { try { _storeLoker.EnterWriteLock(); CheckDirectory(userId, appId); using (var file = File.Open(GetSotrePath(userId, appId), FileMode.OpenOrCreate, FileAccess.ReadWrite)) using (var reader = new StreamReader(file)) using (var writer = new StreamWriter(file)) { var currentStore = reader.ReadToEnd(); if (currentStore.GetSHA1() != diff.Hash) throw ExceptionHelper.Conflict("hash is not relevant"); var store = string.Concat( currentStore.Substring(0, diff.Start), diff.Value, currentStore.Substring(currentStore.Length - diff.End, diff.End)); file.SetLength(0); writer.Write(store); return store.GetSHA1(); } } finally { _storeLoker.ExitWriteLock(); } }