示例#1
0
        public static bool RemoveAwardImage(int id, string folder)
        {
            if (RemoveImage(id, folder))
            {
                var awards      = UserAwardDao.GetAwards().ToList();
                var awardsAsTxt = awards.Select(AwardDao.AwardAsTxt);
                File.WriteAllLines(AwardDao.AwardsFilePath, awardsAsTxt);

                return(true);
            }

            return(false);
        }
示例#2
0
        public static bool RemoveUserImage(int id, string folder)
        {
            if (RemoveImage(id, folder))
            {
                var users      = UserAwardDao.GetUsers().ToList();
                var usersAsTxt = users.Select(UserDao.UserAsTxt);
                File.WriteAllLines(UserDao.UsersFilePath, usersAsTxt);

                return(true);
            }

            return(false);
        }
示例#3
0
        public static void AddAwardImage(int id, byte[] image, string folder)
        {
            var awards     = UserAwardDao.GetAwards().ToList();
            int awardIndex = awards.IndexOf(awards.FirstOrDefault(a => a.Id == id));

            if (awardIndex > -1)
            {
                AddImage(id, image, folder);

                awards[awardIndex].Image = image;
                var awardsAsTxt = awards.Select(AwardDao.AwardAsTxt);

                File.WriteAllLines(AwardDao.AwardsFilePath, awardsAsTxt);
            }
        }
示例#4
0
        public static void AddUserImage(int id, byte[] image, string folder)
        {
            var users     = UserAwardDao.GetUsers().ToList();
            int userIndex = users.IndexOf(users.FirstOrDefault(a => a.Id == id));

            if (userIndex > -1)
            {
                AddImage(id, image, folder);

                users[userIndex].Image = image;
                var usersAsTxt = users.Select(UserDao.UserAsTxt);

                File.WriteAllLines(UserDao.UsersFilePath, usersAsTxt);
            }
        }
示例#5
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);
        }
示例#6
0
        public bool Remove(int id)
        {
            var  users = this.GetAll().ToList();
            User user  = users.FirstOrDefault(u => u.Id == id);

            if (user == null)
            {
                return(false);
            }

            users.Remove(user);
            UserAwardDao.RemoveUserAwards(user.Id);

            var usersAsTxt = users.Select(UserAsTxt);

            File.WriteAllLines(UsersFilePath, usersAsTxt);

            return(true);
        }
示例#7
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);
        }
示例#8
0
 public IEnumerable <Award> GetAll()
 {
     return(UserAwardDao.GetAwards());
 }
示例#9
0
 public User GetById(int id)
 {
     return(UserAwardDao.GetUserById(id));
 }
示例#10
0
 public IEnumerable <User> GetAll()
 {
     return(UserAwardDao.GetUsers());
 }