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); }
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); }
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); } }
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); } }
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); }
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); }
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); }
public IEnumerable <Award> GetAll() { return(UserAwardDao.GetAwards()); }
public User GetById(int id) { return(UserAwardDao.GetUserById(id)); }
public IEnumerable <User> GetAll() { return(UserAwardDao.GetUsers()); }