Пример #1
0
        public bool GiveAward(int userId, int awardId)
        {
            var awards = this.GetAll();

            User  userToUpdate = UserAwardDao.GetUserById(userId);
            Award awardToGive  = awards.FirstOrDefault(a => a.Id == awardId);

            if (userToUpdate == null || awardToGive == null)
            {
                return(false);
            }

            string userIdTemplate  = $"{userToUpdate.Id}{InfoSeparator}";
            string awardIdTemplate = $"{awardToGive.Id}{AwardsSeparator}";

            Action appendAllText = () => File.AppendAllText(UserAwardsFilePath, $"{userIdTemplate}{awardIdTemplate}{Environment.NewLine}");

            if (File.Exists(UserAwardsFilePath))
            {
                bool hasUser    = false;
                bool hasAward   = false;
                int  lineNumber = 0;

                CheckUserAwards(userIdTemplate, awardIdTemplate, ref hasUser, ref hasAward, ref lineNumber);

                if (!hasUser)
                {
                    appendAllText();
                }
                else if (hasUser && !hasAward)
                {
                    var userAwards = File.ReadAllLines(UserAwardsFilePath);
                    userAwards[lineNumber - 1] += awardIdTemplate;

                    File.WriteAllLines(UserAwardsFilePath, userAwards);
                }
            }
            else
            {
                appendAllText();
            }

            return(true);
        }
Пример #2
0
        public bool TakeAward(int userId, int awardId)
        {
            var awards = this.GetAll();

            User  userToUpdate = UserAwardDao.GetUserById(userId);
            Award awardToTake  = awards.FirstOrDefault(a => a.Id == awardId);

            if (userToUpdate == null || awardToTake == null)
            {
                return(false);
            }

            string userIdTemplate  = $"{userToUpdate.Id}{InfoSeparator}";
            string awardIdTemplate = $"{awardToTake.Id}{AwardsSeparator}";

            if (File.Exists(UserAwardsFilePath))
            {
                bool hasUser    = false;
                bool hasAward   = false;
                int  lineNumber = 0;

                CheckUserAwards(userIdTemplate, awardIdTemplate, ref hasUser, ref hasAward, ref lineNumber);

                if (hasUser && hasAward)
                {
                    var userAwards = File.ReadAllLines(UserAwardsFilePath);

                    userAwards[lineNumber - 1] = userAwards[lineNumber - 1]
                                                 .Replace(awardIdTemplate, string.Empty);

                    File.WriteAllLines(UserAwardsFilePath, userAwards);

                    return(true);
                }
            }

            return(false);
        }
Пример #3
0
 public User GetById(int id)
 {
     return(UserAwardDao.GetUserById(id));
 }