示例#1
0
        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));
            }
        }
示例#2
0
        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]);
                }
            }
        }