public void TestMethod1() { using (MinerContext db = new MinerContext()) { var emps = db.Employees; Array.ForEach<Employee>(emps.ToArray<Employee>(), x => { Trace.WriteLine(x.Email); }); } }
public HttpResponseMessage EmployeeOfId(int id){ if (id > 0) { using (MinerContext db = new MinerContext()) { var emp = db.Employees.Where(x => x.Id == id).FirstOrDefault(); if (emp == null) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("employee requested could not be found")); } emp.SkillStatus = EmpSkillStatus(emp); return Request.CreateResponse(HttpStatusCode.OK, Negotiator.Employee(emp)); } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("the id of the employee to be found cannot be negative or zero")); } }
/// <summary> /// this gets the manager for the email, returns http failure message when the manager is not found /// </summary> /// <param name="email">email of the manager being sought</param> /// <returns>httpreponsemessage</returns> public HttpResponseMessage GetManagerOfEmail(string email) { if (!String.IsNullOrEmpty(email)) { using (MinerContext db = new MinerContext()) { var manager = db.Managers.Where(x => x.Email.ToLower() == email.ToLower()).FirstOrDefault(); if (manager == null) { //this is when though the employee is present he / she is not the manager return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("manager of the email not registered")); } return Request.CreateResponse<Object>(HttpStatusCode.OK, Negotiator.Manager(manager)); } } else { //send the error response from here return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid"); } }
/// <summary> /// this function takes in the employee object and returns the skill completion status /// </summary> /// <param name="emp">employee for which the status is to be procured</param> /// <returns>skill completion status</returns> private int EmpSkillStatus(Employee emp) { if (emp != null) { using (MinerContext db = new MinerContext()) { //getting the employee in the context again var inStore = db.Employees.Where(x => x.Id == emp.Id).FirstOrDefault(); int total = inStore.Maps.Count; if (inStore != null && total != 0) { int complete = inStore.Maps.Where(x => x.Level.Level != 0).Count(); double denom = (complete * 100) / total; return (int)Math.Round(denom); } else { return 0; } } } else { return 0; } }
public HttpResponseMessage GetSkillLevels() { using (MinerContext db = new MinerContext()) { return Request.CreateResponse(HttpStatusCode.OK, Negotiator.SkillLevels(db.SkillLevels.ToArray<SkillLevel>())); } }
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 }
/// <summary> /// gets the likely employees with the given phrase to search /// </summary> /// <param name="like">search phrase that we would be looking for</param> /// <returns></returns> public HttpResponseMessage Likely(string like) { if (!String.IsNullOrEmpty(like)) { int numResult = default(int); bool isNumeric = Int32.TryParse(like, out numResult); if (isNumeric == false) { string search = like.ToLower(); //case when the parameter is ok using (MinerContext db = new MinerContext()) { var emps = db.Employees.Where(x => x.Alias.ToLower().Contains(search) || x.Email.Contains(search)); Array.ForEach(emps.ToArray<Employee>(), e => { //filling out all the skill status e.SkillStatus = EmpSkillStatus(e); }); return Request.CreateResponse(HttpStatusCode.OK, Negotiator.Employees(emps.ToArray<Employee>())); } } else { //this is thecase when we are trying to get employee by the emp no using (MinerContext db = new MinerContext()) { var emp = db.Employees.Where(x => x.Id == numResult);//this is the single employee by default return Request.CreateResponse(HttpStatusCode.OK, Negotiator.Employees(emp.ToArray<Employee>())); } } } return this.Top(10);//so if the search phrase is null we sent back the top employees }
public HttpResponseMessage GetTopSkills(int value) { if (value > 0) { using (MinerContext db = new MinerContext()) { var skills = db.Skills.OrderBy(x => x.Id).Take(value); return Request.CreateResponse(HttpStatusCode.OK, Negotiator.Skills(skills.ToArray<Skill>())); } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("cannot get negative number fo top results")); } }
/// <summary> /// gets the likely skills from the set of skills we have /// </summary> /// <param name="like">search phrase for the likely skills</param> /// <returns></returns> public HttpResponseMessage GetLikelySkills(string like) { if (!string.IsNullOrEmpty(like)) { using (MinerContext db = new MinerContext()) { var skills = db.Skills.Where(x => x.Title.ToLower().Contains(like.ToLower()) || x.Group.Title.ToLower().Contains(like.ToLower()) || x.Group.Genre.Title.ToLower().Contains(like.ToLower())); return Request.CreateResponse(HttpStatusCode.OK, Negotiator.Skills(skills.ToArray<Skill>())); } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("the search phrase cannot be empty"));//rather this should be replaced with top ten resulst from the skills //it is unlikely it woul ever reach here since the the paramter is declared as non optional } }
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 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")); } }
public HttpResponseMessage GetReporteesOf(int id) { if (id > 0) { using (MinerContext db = new MinerContext()) { var manager = db.Managers.Where(x => x.Id == id).FirstOrDefault(); if (manager == null) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException(string.Format( "manager of the id:{0} not found" ))); } //here we continue getting the reportees of the manager var reportees = manager.Reportees; Array.ForEach(reportees.ToArray<Employee>(), e => { //filling out all the skill status e.SkillStatus = EmpSkillStatus(e); }); return Request.CreateResponse(HttpStatusCode.OK, Negotiator.Employees(reportees.ToArray<Employee>())); } } else { //this is the case of the invalid id return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("the id of the manager cannot be zero or less than")); } }
/// <summary> /// this methd return the top employees when arraned ascending with their employee numbers /// </summary> /// <param name="value">number of top employees to be returned</param> /// <returns>httpresponse message</returns> public HttpResponseMessage Top(int value) { if (value > 0) { using (MinerContext db = new MinerContext()) { var emps = db.Employees.OrderBy(x => x.Id).Take(value); Array.ForEach(emps.ToArray<Employee>(), e => { //filling out all the skill status e.SkillStatus = EmpSkillStatus(e); }); //returns from the repository - everything is ok return Request.CreateResponse(HttpStatusCode.OK, Negotiator.Employees(emps.ToList<Employee>())); } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("the number of top entries requested expected to be greater than zero")); } }
public HttpResponseMessage MapsOfEmployee(int id) { if (id > 0) { using (MinerContext db = new MinerContext()) { var emp = db.Employees.Where(x => x.Id == id).FirstOrDefault(); if (emp == null) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException(String.Format("employee of the id {0} could not be foudn", id))); } var maps = emp.Maps; JsonMap[] jsonMaps = HydrateComplexFks(Negotiator.Maps(maps.ToArray<SkillMap>())); return Request.CreateResponse(HttpStatusCode.OK, jsonMaps); } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("maps of the employee with negative id cannot be found")); } }