// POST: api/Spell public IHttpActionResult Post([FromBody]Spell spell) { using (SpellContext db = new SpellContext()) { var recordToUpdate = db.Spells.Where(x => x.id == spell.id).FirstOrDefault(); if (recordToUpdate != null) { recordToUpdate.name = spell.name; recordToUpdate.desc = spell.desc; recordToUpdate.higher_level = spell.higher_level; recordToUpdate.page = spell.page; recordToUpdate.range = spell.range; recordToUpdate.components = spell.components; recordToUpdate.material = spell.material; recordToUpdate.ritual = spell.ritual; recordToUpdate.duration = spell.duration; recordToUpdate.concentration = spell.concentration; recordToUpdate.casting_time = spell.casting_time; recordToUpdate.level = spell.level; recordToUpdate.school = spell.school; recordToUpdate.class_name = spell.class_name; db.SaveChanges(); return Ok("Spell " + spell.id + " (" + spell.name + ") Updated Successfully!"); } else { return InternalServerError(); } } }
// GET: api/Spell/5 public Spell Get(int id) { Spell spell = new Spell(); using (SpellContext db = new SpellContext()) { spell = db.Spells.Where(x=>x.id==id).FirstOrDefault(); } return spell; }
// DELETE: api/Spell/5 public IHttpActionResult Delete(int id) { using (SpellContext db = new SpellContext()) { try { Spell spell = db.Spells.Where(x => x.id == id).FirstOrDefault(); db.Spells.Remove(spell); db.SaveChanges(); return Ok("Spell " + spell.id + " (" + spell.name + ") Removed Successfully!"); } catch { return InternalServerError(); } } }
// GET: api/Spell //TODO: Make this return just the name, ID, level, and class of each spell to save on bandwidth and processing power. Get individual can get the rest of the details. public List<SpellSimple> Get() { List<Spell> spells = new List<Spell>(); List<SpellSimple> returnList = new List<SpellSimple>(); using (SpellContext db = new SpellContext()) { spells = db.Spells.ToList(); } foreach(Spell s in spells) { SpellSimple temp = new SpellSimple(); temp.id = s.id; temp.name = s.name; temp.level = s.level; temp.class_name = s.class_name; returnList.Add(temp); } return returnList; }
// PUT: api/Spell/5 public IHttpActionResult Put([FromBody]Spell spell) { using (SpellContext db = new SpellContext()) { try { spell.id = db.Spells.Count() + 1; db.Spells.Add(spell); db.SaveChanges(); return Ok("Spell " + spell.id + " (" + spell.name + ") Added Successfully!"); } catch { return InternalServerError(); } } }