/// <summary> /// Adds a guest to the database and also adds it's likes and dislikes. /// </summary> /// <param name="guest">The Guest.</param> public void Add(Guest guest, Game game) { try { List<ColumnValue> columns = new List<ColumnValue> (); columns.Add (new ColumnValue () { Item1 = "name", Item2 = guest.Name }); columns.Add (new ColumnValue () { Item1 = "avatarId", Item2 = guest.AvatarId.ToString() }); columns.Add (new ColumnValue () { Item1 = "gameId", Item2 = game.Id.ToString() }); guest.Id = (int)this.Insert (columns).LastInsertedId; LikesTable likesTable = new LikesTable (this.entity); foreach (Theme theme in guest.Likes) { likesTable.AddLike (guest, theme, true); } foreach (Theme theme in guest.Dislikes) { likesTable.AddLike (guest, theme, false); } } catch (MySqlException ex) { switch (ex.Number) { case 0: throw new DatabaseException ("Cannot connect to server. Contact administrator", ex); case 1045: throw new DatabaseException ("Invalid username/password, please try again", ex); default: throw new DatabaseException (ex.Message, ex); } } }
public Guest Get(int id) { try { Guest guest = this.GetShallow (id); LikesTable likesTable = new LikesTable (this.entity); foreach (Utilities.Tuple<Theme, bool> like in likesTable.GetAll(guest)) { if (like.Item2) { guest.Likes.Add (like.Item1); } else { guest.Dislikes.Add (like.Item1); } } return guest; } catch (MySqlException ex) { switch (ex.Number) { case 0: throw new DatabaseException ("Cannot connect to server. Contact administrator", ex); case 1045: throw new DatabaseException ("Invalid username/password, please try again", ex); default: throw new DatabaseException (ex.Message, ex); } } }
/// <summary> /// Saves the specified guest to the database. /// </summary> /// <remarks> /// Also updates it's likes and dislikes (removing old ones and adding new ones) /// </remarks> /// <param name="guest">The guest.</param> public void Save(Guest guest, Game game) { try { if (guest.Id < 0) { return; } List<ColumnValue> columns = new List<ColumnValue> (); columns.Add (new ColumnValue () { Item1 = "name", Item2 = guest.Name }); columns.Add (new ColumnValue () { Item1 = "avatarId", Item2 = guest.AvatarId.ToString() }); columns.Add (new ColumnValue () { Item1 = "gameId", Item2 = game.Id.ToString() }); StatementValue where = new StatementValue (); where.Item1 = "id = @StatementValue0"; where.Item2 = new List<string> (); where.Item2.Add (guest.Id.ToString ()); this.Update (columns, where); LikesTable likesTable = new LikesTable (this.entity); List<Tuple<Theme, bool>> likes = likesTable.GetAll (guest); // Delete removed likes foreach (Tuple<Theme, bool> like in likes) { if (!guest.Likes.Contains (like.Item1) && !guest.Dislikes.Contains (like.Item1)) { likesTable.Delete (guest, like.Item1); } } // Update likes and add new likes foreach (Theme theme in guest.Likes) { Tuple<Theme, bool> findTheme = likes.Find (thmeLike => thmeLike.Item1.Equals (theme)); if (findTheme == null) { likesTable.AddLike (guest, theme, true); } else { if (!findTheme.Item2) likesTable.Save (guest, theme, true); } } foreach (Theme theme in guest.Dislikes) { Tuple<Theme, bool> findTheme = likes.Find (thmeLike => thmeLike.Item1.Equals (theme)); if (findTheme == null) { likesTable.AddLike (guest, theme, false); } else { if (findTheme.Item2) likesTable.Save (guest, theme, false); } } } catch (MySqlException ex) { switch (ex.Number) { case 0: throw new DatabaseException ("Cannot connect to server. Contact administrator", ex); case 1045: throw new DatabaseException ("Invalid username/password, please try again", ex); default: throw new DatabaseException (ex.Message, ex); } } }