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 DropReportees(int id, Employee[] reportees) { if (id > 0 && reportees != null) { using (MinerContext db = new MinerContext()) { var manager = db.Managers.Where(m => m.Id == id).FirstOrDefault(); if (manager == null) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException(String.Format( "the manager of the id:{0} not found", id))); } Array.ForEach(reportees, r => { manager.Reportees.Remove(db.Employees.Where(e => e.Id == r.Id).FirstOrDefault()); db.SaveChanges();//this will ensure we are persisting the changes to the database }); return Request.CreateResponse(HttpStatusCode.NoContent); } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("either the manager id is invalid or the reportees are null")); } }
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); } }
public HttpResponseMessage AddReporteesOf(int id, Employee[] reportees) { if (id > 0 && reportees != null) { using (MinerContext db = new MinerContext()) { Array.ForEach(reportees, x => { Employee emp = db.Employees.Where(e => e.Id == x.Id).FirstOrDefault(); emp.Manager = db.Managers.Where(m => m.Id == id).FirstOrDefault(); db.SaveChanges();//saves the new manager for the employee }); return Request.CreateResponse(HttpStatusCode.NoContent); } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("either the manager id is invalid or the reportees are null")); } }