public Boolean ChangeDiginoteOwner(Diginote diginote, int newId) { Boolean success = false; try { //DATABASE System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(); cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = "UPDATE Diginote SET Owner = " + newId.ToString() + " WHERE SerialNumber = '" + diginote.SerialNumber + "'; "; cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); con.Close(); success = true; } catch (Exception e) { if (con.State == System.Data.ConnectionState.Open) { con.Close(); } Console.WriteLine("[ChangeDiginoteOwner] Exeception Caught: " + e.Message); } return(success); }
public List <Diginote> GetUserDiginotes(User user) { List <Diginote> diginotes = new List <Diginote>(); int userId = GetUserId(user); Diginote diginote; if (userId == -1) { Console.WriteLine("[GetUserDiginotes] User does not exist"); return(diginotes); } try { //DATABASE System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(); cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = "SELECT SerialNumber, Owner FROM [Diginote] WHERE Owner = '" + userId + "';"; cmd.Connection = con; con.Open(); SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { diginote = new Diginote(reader.GetString(0), user); diginotes.Add(diginote); } reader.Close(); } con.Close(); } catch (Exception e) { if (con.State == System.Data.ConnectionState.Open) { con.Close(); } Console.WriteLine("[GetUserDiginotes] Exeception Caught: " + e.Message); } Console.WriteLine("GetUser diginotes of " + user.Username + " total: " + diginotes.Count()); return(diginotes); }
// function that adds a new diginote to user and return info about the new diginote public DiginoteInfo DigDiginote(User user) { // create new diginote with a value that depends on the number of diginotes that already exist Diginote dig = new Diginote(user, 1.0 + diginoteDB.Count * 0.05); // add diginote to list diginoteDB.Add(dig); Log(user.Username + " digged a diginote"); // save state saveState(); // warn clients about the new diginote count SafeInvoke(new ChangeArgs(ChangeType.SysDiginotes, diginoteDB.Count)); return(new DiginoteInfo(dig.Id, dig.Value, dig.LastAquiredOn)); }
// function that performs a transaction private void Transaction(Order from, Order to, int quantity) { List <DiginoteInfo> digInfo = new List <DiginoteInfo>(); // get all diginotes from the user that is selling diginotes List <Diginote> diginotesFrom = diginoteDB.FindAll(d => d.Owner.Username == from.User.Username); if (diginotesFrom.Count >= quantity) { // iterate over 'quantity' diginotes and change owner to the user that is buying diginotes for (int i = 0; i < quantity; i++) { Diginote dig = diginotesFrom[i]; dig.Owner = to.User; digInfo.Add(new DiginoteInfo(dig.Id, dig.Value, dig.LastAquiredOn)); } // update quantity in both orders from.Quantity -= quantity; to.Quantity -= quantity; Log(quantity + " Diginotes transacted from " + from.User.Username + " to " + to.User.Username + " at " + quotation + "$"); // set transaction statistics if (transactionsPerMin.Count > 0) { Pair <DateTime, int> lastStatItem = transactionsPerMin[transactionsPerMin.Count - 1]; if (lastStatItem.first.ToString("dd/MM/yyyy hh:mm") == DateTime.Now.ToString("dd/MM/yyyy hh:mm")) { lastStatItem.second++; } else { transactionsPerMin.Add(new Pair <DateTime, int>(DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy hh:mm")), 1)); } } else { transactionsPerMin.Add(new Pair <DateTime, int>(DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy hh:mm")), 1)); } // warn clients about the transaction to update information in the client SafeInvoke(new ChangeArgs(from, to, digInfo, transactionsPerMin[transactionsPerMin.Count - 1])); } }
public void TransferDiginotesTest() { //ARRANGE string userNickname1 = "jc"; string userNickname2 = "mn"; User u1 = new User("Jose C", userNickname1, "1234"); User u2 = new User("Manuel C", userNickname2, "1234"); Dictionary <string, User> usersList = new Dictionary <string, User>(); usersList.Add(u1.Nickname, u1); usersList.Add(u2.Nickname, u2); Diginote d1 = new Diginote(1, u1.Nickname); Diginote d2 = new Diginote(2, u2.Nickname); Dictionary <long, Diginote> notesList = new Dictionary <long, Diginote>(); Dictionary <long, string> ownershipTable = new Dictionary <long, string>(); ownershipTable.Add(d1.SerialNumber, u1.Nickname); ownershipTable.Add(d2.SerialNumber, u2.Nickname); notesList.Add(d1.SerialNumber, d1); notesList.Add(d2.SerialNumber, d2); Coordinator c = new Coordinator(usersList, notesList, ownershipTable, true); c.Db.insertUser(u1); //TODO THIS DEPENDS ON registerUser function. Should not depend, and be more "hardcoded" c.Db.insertUser(u2); c.Db.insertDiginote(d1.SerialNumber, d1.OwnerNickname); c.Db.insertDiginote(d2.SerialNumber, d2.OwnerNickname); //ACT AND ASSERT Assert.IsTrue(c.TransferDiginotes(u1.Nickname, u2.Nickname, 1)); Assert.IsTrue(d1.OwnerNickname == u2.Nickname); Assert.IsTrue(d2.OwnerNickname == u2.Nickname); Assert.IsFalse(c.OwnershipTable.Values.Contains(u1.Nickname)); Assert.IsTrue(c.OwnershipTable.Count == 2); Assert.IsFalse(c.TransferDiginotes(u1.Nickname, u2.Nickname, 1)); CloseDB(c); }
public void GetAllDiginotesTest() { DiginoteDB db = new DiginoteDB(true); int userid = 1; User u1 = new User("Jose C", "jc", "1234"); Diginote d1 = new Diginote(1, "jc"); Diginote d2 = new Diginote(2, "jc"); SQLiteCommand insertUser1 = new SQLiteCommand( "insert into users(id, name, nickname, password) values(" + userid.ToString() + ",\"" + u1.Name + "\",\"" + u1.Nickname + "\",\"" + u1.Password + "\");", db.Db); SQLiteCommand insertNote1 = new SQLiteCommand( "insert into diginotes(serialNumber, facialValue, idUser) values(" + d1.SerialNumber.ToString() + ",1," + userid.ToString() + ");", db.Db); SQLiteCommand insertNote2 = new SQLiteCommand( "insert into diginotes(serialNumber, facialValue, idUser) values(" + d2.SerialNumber.ToString() + ",1," + userid.ToString() + ");", db.Db); insertUser1.ExecuteNonQuery(); insertNote1.ExecuteNonQuery(); insertNote2.ExecuteNonQuery(); List <Diginote> list = db.getAllDiginotes(); Assert.IsTrue(list.Count == 2); Assert.IsTrue(list[0].Equals(d1)); Assert.IsTrue(list[1].Equals(d2)); insertUser1.Dispose(); insertNote1.Dispose(); insertNote2.Dispose(); CloseDb(db); }
public void CreateDiginote(string serialNumber, User owner) { Diginote diginote = new Diginote(serialNumber, owner); int userId; Console.WriteLine("CreateDiginote() invoked"); try { //DATABASE System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(); cmd.CommandType = System.Data.CommandType.Text; cmd.Connection = con; userId = GetUserId(owner); if (userId == -1) { Console.WriteLine("[CreateDiginote] User does not exist"); return; } Console.WriteLine("[CreateDiginote] Adding Diginote to User id " + userId); con.Open(); cmd.CommandText = "INSERT [Diginote] (SerialNumber, FacialValue, Owner) VALUES ('" + diginote.SerialNumber + "', '" + diginote.FacialValue + "', '" + userId + "');"; cmd.ExecuteNonQuery(); con.Close(); } catch (Exception e) { if (con.State == System.Data.ConnectionState.Open) { con.Close(); } Console.WriteLine("[CreateDiginote] Exeception Caught: " + e.Message); } }
public void LoadDataFromDatabaseTest() { string userNickname1 = "jc"; string userNickname2 = "mn"; User u1 = new User("Jose C", userNickname1, "1234"); User u2 = new User("Manuel C", userNickname2, "1234"); Diginote d1 = new Diginote(1, u1.Nickname); Diginote d2 = new Diginote(2, u2.Nickname); Dictionary <string, User> usersList = new Dictionary <string, User>(); Dictionary <long, Diginote> notesList = new Dictionary <long, Diginote>(); Dictionary <long, string> ownershipTable = new Dictionary <long, string>(); usersList.Add(userNickname1, u1); usersList.Add(userNickname2, u2); notesList.Add(d1.SerialNumber, d1); notesList.Add(d2.SerialNumber, d2); ownershipTable.Add(d1.SerialNumber, userNickname1); ownershipTable.Add(d2.SerialNumber, userNickname2); Coordinator c = new Coordinator(true); c.Db.insertUser(u1); //TODO THIS DEPENDS ON registerUser function. Should not depend, and be more "hardcoded" c.Db.insertUser(u2); c.Db.insertDiginote(d1); c.Db.insertDiginote(d2); c.LoadDataFromDatabase(); Assert.IsTrue(usersList.Count == c.UsersList.Count); Assert.IsTrue(!usersList.Except(c.UsersList).Any()); //There is no element different in both of the dictionaries Assert.IsTrue(notesList.Count == c.NotesList.Count); Assert.IsTrue(!notesList.Except(c.NotesList).Any()); Assert.IsTrue(ownershipTable.Count == c.OwnershipTable.Count); Assert.IsTrue(!ownershipTable.Except(c.OwnershipTable).Any()); CloseDB(c); }