public void EditProfile(string firstName, string lastName, string email, DateTime?date, string password, string genderValue) { using (var context = new RPGHelperContext()) { int userId = AuthenticationService.GetCurrentUser().Id; var user = context.Users.FirstOrDefault(u => u.Id == userId); user.FirstName = firstName; user.LastName = lastName; user.Email = email; user.Birthdate = date; user.Password = password; Gender gen = Gender.NotSpecified; Enum.TryParse(genderValue, out gen); user.Gender = gen; context.SaveChanges(); AuthenticationService.Logout(); AuthenticationService.Login(user.Username, user.Password); } }
public void ChangeProfilePicture(string imageName) { using (var context = new RPGHelperContext()) { var userId = AuthenticationService.GetCurrentUser().Id; var user = context.Users.FirstOrDefault(u => u.Id == userId); string oldImage = user.ImgPath; if (oldImage != "anonymous-person.png" && !oldImage.Contains("stock")) { try { File.Delete($@"..\..\Media\ProfilePictures\{oldImage}"); } catch (Exception) { } } user.ImgPath = imageName; context.SaveChanges(); AuthenticationService.Refresh(); } }
public static void SaveItem(Item item, ItemStats itemStat) { using (var context = new RPGHelperContext()) { itemStat.Item = item; context.ItemStatistics.Add(itemStat); context.Items.Add(item); context.SaveChanges(); } }
public void MarkAsDeleted(int msgId) { using (var context = new RPGHelperContext()) { var msg = context.Messages.Include("Sender").Include("Recipient").FirstOrDefault(m => m.Id == msgId); msg.IsDeleted = true; context.SaveChanges(); } }
public Message GetMessageById(int msgId) { using (var context = new RPGHelperContext()) { var msg = context.Messages.Include("Sender").Include("Recipient").FirstOrDefault(m => m.Id == msgId); msg.IsRead = true; context.SaveChanges(); return(msg); } }
public static void SaveChanges(Item item, int id, RPGHelperContext context) { var itemToSave = context.Items.Find(id); itemToSave.Cost = item.Cost; itemToSave.Description = item.Description; itemToSave.ItemStats = item.ItemStats; itemToSave.Name = item.Name; itemToSave.Rarity = item.Rarity; itemToSave.Slot = item.Slot; itemToSave.ItemType = item.ItemType; context.SaveChanges(); }
public void Register(string username, string password) { using (var context = new RPGHelperContext()) { User user = new User { Username = username, Password = password }; context.Users.Add(user); context.SaveChanges(); } }
public static void Remove(Item item) { using (var context = new RPGHelperContext()) { Item itemToRemove = context.Items.FirstOrDefault(i => i.Id == item.Id); ItemStats itemStats = context.ItemStatistics.FirstOrDefault(st => st.Item.Id == item.Id); if (itemStats != null) { context.ItemStatistics.Remove(itemStats); context.SaveChanges(); } else { Console.WriteLine("gluposti"); } if (itemToRemove != null) { context.Items.Remove(itemToRemove); context.SaveChanges(); } } }
public void RemoveFriend(string username) { using (var context = new RPGHelperContext()) { int userId = AuthenticationService.GetCurrentUser().Id; var user = context.Users.FirstOrDefault(u => u.Id == userId); var friend = context.Users.FirstOrDefault(u => u.Username.ToLower() == username.ToLower()); user.Friends.Remove(friend); context.SaveChanges(); } }
private void DeleteHero_Click(object sender, RoutedEventArgs e) { var context = new RPGHelperContext(); Button b = sender as Button; var currentHeroId = (int)b.DataContext; var currentHero = context.Heroes.FirstOrDefault(h => h.Id == currentHeroId); var result = MessageBox.Show("Are you sure you want to delete this Hero?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes && currentHero != null) { using (context = new RPGHelperContext()) { Hero heroToRemove = context.Heroes.FirstOrDefault(h => h.Id == currentHero.Id); HeroStats heroStats = context.HeroStatistics.FirstOrDefault(hs => hs.Hero.Id == currentHero.Id); if (heroStats != null) { context.HeroStatistics.Remove(heroStats); context.SaveChanges(); } if (heroToRemove != null) { context.Heroes.Remove(heroToRemove); context.SaveChanges(); } } MessageBox.Show("Hero removed successfully!"); LoadData(); } if (currentHero == null) { MessageBox.Show("This Hero is not added yet!"); } }
private void HeroRemoveOwnedItem_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; Item currentItem = button.DataContext as Item; int itemId = currentItem.Id; var currentHeroId = (HeroesList.SelectedItem as Hero).Id; using (var context = new RPGHelperContext()) { var hero = context.Heroes.FirstOrDefault(h => h.Id == currentHeroId); var item = context.Items.FirstOrDefault(i => i.Id == itemId); if (hero != null) { hero.Items.Remove(item); context.SaveChanges(); } LoadData(); } }
private void SaveChanges_Click(object sender, RoutedEventArgs e) { currentItem = ItemsView.GetCurrentItem(); string name = Name.Text; decimal cost = decimal.Parse(Cost.Text); string description = Description.Text; string itemType = ItemType.Text; string rarity = Rarity.Text; string slot = Slot.Text; double attack = double.Parse(Attack.Text); double defence = double.Parse(Defence.Text); Item item = context.Items.Find(currentItem.Id); item.Cost = cost; item.Name = name; item.Description = description; item.ItemType = (ItemType)Enum.Parse(typeof(ItemType), itemType); item.Slot = (Slot)Enum.Parse(typeof(Slot), slot); item.Rarity = (Rarity)Enum.Parse(typeof(Rarity), rarity); ItemStats itemStat = context.ItemStatistics.FirstOrDefault(st => st.Item.Id == currentItem.Id); itemStat.Attack = attack; itemStat.Defence = defence; item.ItemStats = itemStat; var result = MessageBox.Show("Are you sure you want to edit this item?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { context.SaveChanges(); } ItemsView.items = ItemService.GetAllItems(); this.Close(); }
public void SendMessage(string usernameSelected, string subject, string content) { using (var context = new RPGHelperContext()) { int userId = AuthenticationService.GetCurrentUser().Id; var sender = context.Users.FirstOrDefault(u => u.Id == userId); var recipient = context.Users.FirstOrDefault(u => u.Username == usernameSelected); Message msg = new Message { Subject = subject, Content = content, Sender = sender, Recipient = recipient, SentOn = DateTime.Now }; context.Messages.Add(msg); context.SaveChanges(); } }
private void SaveHeroItem_Button(object sender, RoutedEventArgs e) { if (ItemsBox.SelectionBoxItem != null && ItemsBox.SelectionBoxItem != "") { var currentItem = (Item)ItemsBox.SelectionBoxItem; var heroFromDb = context.Heroes.FirstOrDefault(h => h.Id == this.currentHero.Id); var itemFromDB = context.Items.FirstOrDefault(i => i.Id == currentItem.Id); if (itemFromDB != null) { heroFromDb.Items.Add(itemFromDB); context.SaveChanges(); ItemsSourceLoad(); stsBarTextBlock.Foreground = new SolidColorBrush(Colors.Green); stsBarTextBlock.Text = "Success!"; } } else { stsBarTextBlock.Foreground = new SolidColorBrush(Colors.MediumVioletRed); stsBarTextBlock.Text = "Select some Item!"; } }
private void SaveHero_Button(object sender, RoutedEventArgs e) { var currentHero = (Hero)DataContext; stsBarTextBlock.Foreground = new SolidColorBrush(Colors.MediumVioletRed); if (HeroNameBox.Text == null || HeroNameBox.Text.Length < 3 || HeroNameBox.Text.Length > 60) { stsBarTextBlock.Text = "Hero name length allowed: 3-60!"; HeroNameBox.Focus(); HeroNameBox.SelectAll(); return; } if (GoldBox.Text == null || GoldBox.Text == "" || decimal.Parse(GoldBox.Text) < 0) { stsBarTextBlock.Text = "Gold cannot be negative or null!"; GoldBox.Focus(); GoldBox.SelectAll(); return; } if (HpBox.Text == null || HpBox.Text == "" || double.Parse(HpBox.Text) < 0) { stsBarTextBlock.Text = "Hp cannot be negative or null!"; HpBox.Focus(); HpBox.SelectAll(); return; } if (ManaBox.Text == null || ManaBox.Text == "" || double.Parse(ManaBox.Text) < 0) { stsBarTextBlock.Text = "Mana cannot be negative or null!"; ManaBox.Focus(); ManaBox.SelectAll(); return; } if (DefenceBox.Text == null || DefenceBox.Text == "" || double.Parse(DefenceBox.Text) < 0) { stsBarTextBlock.Text = "Defence cannot be negative or null!"; DefenceBox.Focus(); DefenceBox.SelectAll(); return; } if (AttackPowerBox.Text == null || AttackPowerBox.Text == "" || double.Parse(AttackPowerBox.Text) < 0) { stsBarTextBlock.Text = "AttackPower cannot be negative or null!"; AttackPowerBox.Focus(); AttackPowerBox.SelectAll(); return; } var hero = context.Heroes.FirstOrDefault(h => h.Id == currentHero.Id); hero.Name = HeroNameBox.Text; hero.Gold = decimal.Parse(GoldBox.Text); hero.HeroStats.Hp = double.Parse(HpBox.Text); hero.HeroStats.Mana = double.Parse(ManaBox.Text); hero.HeroStats.Defence = double.Parse(DefenceBox.Text); hero.HeroStats.AttackPower = double.Parse(AttackPowerBox.Text); context.SaveChanges(); stsBarTextBlock.Foreground = new SolidColorBrush(Colors.Green); stsBarTextBlock.Text = "Success!"; }
private void SaveHero_Button(object sender, RoutedEventArgs e) { stsBarTextBlock.Foreground = new SolidColorBrush(Colors.MediumVioletRed); if (HeroNameBox.Text == null || HeroNameBox.Text.Length < 3 || HeroNameBox.Text.Length > 60) { stsBarTextBlock.Text = "Hero name length allowed: 3-60!"; HeroNameBox.Focus(); HeroNameBox.SelectAll(); return; } if (GoldBox.Text == null || GoldBox.Text == "" || decimal.Parse(GoldBox.Text) < 0) { stsBarTextBlock.Text = "Gold cannot be negative or null!"; GoldBox.Focus(); GoldBox.SelectAll(); return; } if (HpBox.Text == null || HpBox.Text == "" || double.Parse(HpBox.Text) < 0) { stsBarTextBlock.Text = "Hp cannot be negative or null!"; HpBox.Focus(); HpBox.SelectAll(); return; } if (ManaBox.Text == null || ManaBox.Text == "" || double.Parse(ManaBox.Text) < 0) { stsBarTextBlock.Text = "Mana cannot be negative or null!"; ManaBox.Focus(); ManaBox.SelectAll(); return; } if (DefenceBox.Text == null || DefenceBox.Text == "" || double.Parse(DefenceBox.Text) < 0) { stsBarTextBlock.Text = "Defence cannot be negative or null!"; DefenceBox.Focus(); DefenceBox.SelectAll(); return; } if (AttackPowerBox.Text == null || AttackPowerBox.Text == "" || double.Parse(AttackPowerBox.Text) < 0) { stsBarTextBlock.Text = "AttackPower cannot be negative or null!"; AttackPowerBox.Focus(); AttackPowerBox.SelectAll(); return; } var user = AuthenticationService.GetCurrentUser(); var newHero = new Hero() { Name = HeroNameBox.Text, Gold = decimal.Parse(GoldBox.Text), UserId = user.Id }; var newHeroStats = new HeroStats() { Hero = newHero, Hp = double.Parse(HpBox.Text), Mana = double.Parse(ManaBox.Text), Defence = double.Parse(DefenceBox.Text), AttackPower = double.Parse(AttackPowerBox.Text) }; newHero.HeroStats = newHeroStats; context.Heroes.Add(newHero); context.HeroStatistics.Add(newHeroStats); context.SaveChanges(); stsBarTextBlock.Foreground = new SolidColorBrush(Colors.Green); stsBarTextBlock.Text = "Success!"; }