public async Task <bool> SyncUsers(Users user, DbSyncMethod syncMethod) { try { bool wroteRecord = false; await using (var db = new PseudoMarketsDbContext(DbConnectionString)) { var userExists = db.Users.Any(x => x.Username == user.Username); if (userExists) { var existingUser = db.Users.FirstOrDefault(x => x.Username == user.Username); switch (syncMethod) { case DbSyncMethod.Update: existingUser.Password = user.Password; existingUser.Salt = user.Salt; db.Entry(existingUser).State = EntityState.Modified; await db.SaveChangesAsync(); break; case DbSyncMethod.Delete: db.Entry(existingUser).State = EntityState.Deleted; await db.SaveChangesAsync(); break; } wroteRecord = true; } else { wroteRecord = true; } } return(wroteRecord); } catch (Exception e) { Console.WriteLine(e); throw; } }
public async Task <bool> SyncPositions(Positions position, Users user, DbSyncMethod syncMethod) { try { bool wroteRecord = false; await using (var db = new PseudoMarketsDbContext(DbConnectionString)) { var userExists = db.Users.Any(x => x.Username == user.Username); if (userExists) { var localUser = db.Users.FirstOrDefault(x => x.Username == user.Username); var accountId = db.Accounts.FirstOrDefault(x => x.UserID == localUser.Id).Id; var existingPosition = await db.Positions .Where(x => x.AccountId == accountId && x.Symbol == position.Symbol) .FirstOrDefaultAsync(); switch (syncMethod) { case DbSyncMethod.Insert: Positions localPosition = new Positions() { OrderId = position.OrderId, Symbol = position.Symbol, AccountId = accountId, Quantity = position.Quantity, Value = position.Value }; await db.Positions.AddAsync(localPosition); await db.SaveChangesAsync(); break; case DbSyncMethod.Update: existingPosition.Value = position.Value; existingPosition.Quantity = position.Quantity; db.Entry(existingPosition).State = EntityState.Modified; await db.SaveChangesAsync(); break; case DbSyncMethod.Delete: db.Entry(existingPosition).State = EntityState.Deleted; await db.SaveChangesAsync(); break; } wroteRecord = true; } else { wroteRecord = false; } } return(wroteRecord); } catch (Exception e) { Console.WriteLine(e); throw; } }