public Award Add(Award award) { if (!_awards.Values.Any(x => x.Title == award.Title)) { int lastId = _awards.Count == 0 ? 0 : _awards.Keys.Max(); award.Id = lastId + 1; _awards.Add(award.Id, award); foreach (int awardId in UsersAwardsDao.GetByAwardId(award.Id)) { award.Users.Add(UserDao._users[awardId]); } ExportListToFile(); return(award); } else { return(_awards.Values.FirstOrDefault(x => x.Title == award.Title)); } }
public void Remove(int id) { _awards.Remove(id); UsersAwardsDao.RemoveByAwardId(id); UserDao.RemoveInternal(id); ExportListToFile(); }
/// <summary> /// Adds user and returns reference it. If the user already exists /// return the reference to the existing user instead. /// </summary> /// <param name="user"> The user. </param> /// <returns> Added User. </returns> public User Add(User user) { // Check if the collection contains the user. if (!_users.Values.Any(x => x.Id == user.Id && x.Name == user.Name && x.DateOfBirth == user.DateOfBirth)) { int lastId = _users.Count == 0 ? 0 : _users.Keys.Max(); user.Id = lastId + 1; _users.Add(user.Id, user); foreach (int awardId in UsersAwardsDao.GetByUserId(user.Id)) { user.Awards.Add(AwardDao._awards[awardId]); } ExportListToFile(); return(user); } else { return(_users.Values.FirstOrDefault(x => x.Equals(user))); } }
/// <summary> /// Adds the awards to the user. /// </summary> /// <param name="userId"> The user's id. </param> /// <param name="awardIds"> 32-bit int array of award ids. </param> public void AddAwards(int userId, int[] awardIds) { for (int i = 0; i < awardIds.Length; i++) { if (UsersAwardsDao.Add(userId, awardIds[i])) { } } }
public void RemoveUsers(int awardId, int[] userIds) { for (int i = 0; i < userIds.Length; i++) { if (UsersAwardsDao.Remove(userIds[i], awardId)) { _awards[awardId].Users.Remove(UserDao._users[userIds[i]]); } } }
/// <summary> /// Removes the awards from the user. /// </summary> /// <param name="userId"> The user's id. </param> /// <param name="awardIds"> 32-bit int array of award ids. </param> public void RemoveAwards(int userId, int[] awardIds) { for (int i = 0; i < awardIds.Length; i++) { if (UsersAwardsDao.Remove(userId, awardIds[i])) { _users[userId].Awards.Remove(AwardDao._awards[awardIds[i]]); } } }
/// <summary> /// Initializes the dictionary of users. /// </summary> static UserDao() { // Check if containing users file exists. if (File.Exists(_usersFileName)) { string temp = string.Empty; using (var sr = new StreamReader(_usersFileName)) { temp = sr.ReadToEnd(); } _users = JsonConvert.DeserializeObject <Dictionary <int, User> >(temp); if (_users == null) { _users = new Dictionary <int, User>(); } else { using (var sw = new StreamWriter(_usersFileName)) { sw.WriteLine(JsonConvert.SerializeObject(_users)); } } } else { // Create new dictionary of users. _users = new Dictionary <int, User>(); } // Add all users awards. foreach (var user in _users.Values) { foreach (int awardId in UsersAwardsDao.GetByUserId(user.Id)) { user.Awards.Add(AwardDao._awards[awardId]); } } }
static AwardDao() { // Check if file containg awards exists if (File.Exists(_awardsFileName)) { string temp = string.Empty; using (var sr = new StreamReader(_awardsFileName)) { temp = sr.ReadToEnd(); } _awards = JsonConvert.DeserializeObject <Dictionary <int, Award> >(temp); if (_awards == null) { _awards = new Dictionary <int, Award>(); } else { using (var sw = new StreamWriter(_awardsFileName)) { sw.WriteLine(JsonConvert.SerializeObject(_awards)); } } } else { _awards = new Dictionary <int, Award>(); } foreach (var award in _awards.Values) { foreach (int awardId in UsersAwardsDao.GetByAwardId(award.Id)) { award.Users.Add(UserDao._users[awardId]); } } }
/// <summary> /// Removes the user by the id. /// </summary> /// <param name="id"> The user's id. </param> public void Remove(int id) { _users.Remove(id); UsersAwardsDao.RemoveByUserId(id); ExportListToFile(); }