//I probably should make someething elegant that detects what's changed and updated it individually //may be a dead end public static bool UpdateCharacter(CharacterSheetViewModel sheet) { //for each property of this view model, which is a model object in itself //try to update that object if not null foreach (var item in sheet.GetType().GetProperties()) { try { //check that property/Model isn't null if (item is null) { return(false); } using (var context = new Context()) { //see helper class Helper.SaveSheetProperty(item, context); context.SaveChanges(); return(true); } } catch { return(false); } } return(false); }
//gets the selected character and hopefully all related tables. might need to use include public static CharacterSheetViewModel GetCharacter(int userID, int charID) { //couldn't think of a better way than to make multiple calls at the moment. using (var context = new Context()) { //make one query that hopefully gets all the objects I need. CharacterSheet sheet = context.CharacterSheet .Where(d => d.UserID == userID && d.CharacterID == charID) .Include(d => d.SavingThrows) .Include(d => d.Skills) .Include(d => d.Money) .Include(d => d.Proficiencies) .Include(d => d.Appearance) .Include(d => d.Spellbook) .First(); //first idea //SavingThrows saving = context.SavingThrows // .Where(d => d.CharacterID == charID).First(); //Skills skills = context.Skills // .Where(d => d.CharacterID == charID).First(); //Money money = context.Money // .Where(d => d.CharacterID == charID).First(); //Proficiencies proficiencies = context.Proficiencies // .Where(d => d.CharacterID == charID).First(); //Appearance appearance = context.Appearance // .Where(d => d.CharacterID == charID).First(); //Spellbook spellbook = context.Spellbook // .Where(d => d.CharacterID == charID).First(); //creats the character sheet view model CharacterSheetViewModel character = new CharacterSheetViewModel() { CharacterSheet = sheet, SavingThrows = sheet.SavingThrows, Skills = sheet.Skills, Money = sheet.Money, Proficiencies = sheet.Proficiencies, Appearance = sheet.Appearance, Spellbook = sheet.Spellbook }; return(character); } }
//insert new character public static bool CreateCharacter(CharacterSheetViewModel sheet) { try { using (var context = new Context()) { //need an add for each table? context.Add(sheet.CharacterSheet); context.Add(sheet.SavingThrows); context.Add(sheet.Skills); context.Add(sheet.Money); context.Add(sheet.Proficiencies); context.Add(sheet.Appearance); context.Add(sheet.Spellbook); context.SaveChanges(); return(true); } } catch { return(false); } }