public IHttpActionResult delMaps(JsonMap[] maps) { return ResponseMessage(Repo.DeleteMaps(maps)); }
public IHttpActionResult addMaps(JsonMap[] maps) { return ResponseMessage(Repo.AddMaps(maps)); }
public HttpResponseMessage DeleteMaps(JsonMap[] maps) { if (maps==null) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("maps to remove cannot be null")); } using (MinerContext db = new MinerContext()) { Array.ForEach(maps, m => { var skillmap = db.Maps.Where(x => x.Id == m.id).FirstOrDefault(); if (skillmap!=null) { db.Maps.Remove(skillmap); db.SaveChanges(); } }); return Request.CreateResponse(HttpStatusCode.NoContent); } }
public HttpResponseMessage AddMaps(JsonMap[] maps) { if (maps == null) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("maps to be altered cannot be null")); } using (MinerContext db = new MinerContext()) { bool hasFlag = false; Array.ForEach(maps, m => { var emp = db.Employees.Where(e => e.Id == m.emp.id).FirstOrDefault(); var skill = db.Skills.Where(s => s.Id == m.skill.id).FirstOrDefault(); var explevel = db.ExpLevels.Where(xp => xp.Id == m.exp.id).FirstOrDefault(); var level = db.SkillLevels.Where(l => l.Level == m.level.id).FirstOrDefault(); if (emp != null && skill != null && explevel != null && level !=null) { //this is the condition when the map is ok to be added to the database db.Maps.Add(new SkillMap() { Employee= emp, ExpLevel = explevel, Level = level, Skill = skill }); db.SaveChanges(); } else { hasFlag = true;//this is to denote that there was some error when saving one of the maps to the database } }); return Request.CreateResponse(HttpStatusCode.Created); } }
public HttpResponseMessage AlterMaps(JsonMap[] maps) { if (maps == null) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("maps to be altered cannot be null")); } using (MinerContext db = new MinerContext()) { Array.ForEach(maps, m => { var skillmap = db.Maps.Where(x => x.Id == m.id).FirstOrDefault(); if (skillmap != null) { var lvl = db.SkillLevels.Where(l => l.Level == m.level.id).FirstOrDefault(); var explvl = db.ExpLevels.Where(e => e.Id == m.exp.id).FirstOrDefault(); skillmap.Level = lvl != null ? lvl : skillmap.Level;//assigning only if the new level is found else restoring the same skillmap.ExpLevel = explvl != null ? explvl : skillmap.ExpLevel; //assigning the exp level if the new exp level is found else restoring db.SaveChanges();//persisting the records to the database } }); return Request.CreateResponse(HttpStatusCode.NoContent); } }
/// <summary> /// this is the helper function that would take the negotiated json content and emit the same object with hydrated complex object foreign keys /// </summary> /// <param name="maps">neogitiated json content</param> /// <returns></returns> private JsonMap[] HydrateComplexFks(JsonMap[] maps) { List<JsonMap> result = new List<JsonMap>();//this is the result preparation if (maps == null || maps.Count() == 0) { return new JsonMap[] { };//error condition } using (MinerContext db = new MinerContext()) { Array.ForEach(maps, m => { var inContext = db.Maps.Where(x => x.Id == m.id).FirstOrDefault(); m.emp = Negotiator.Employee(inContext.Employee);//hydrating the employee for the map m.skill = Negotiator.Skill(inContext.Skill);//hydrating the skill object for the map m.level = Negotiator.SkillLevel(inContext.Level);//hydrating the skill level object for the map m.exp = Negotiator.ExpLevel(inContext.ExpLevel);//hydrating the exp level in the map result.Add(m); }); } return result.ToArray<JsonMap>();//sending back the object }