/// <summary> /// Reads all locations from the file and adds them to the list /// </summary> /// <param name="location">list of locations</param> public void ReadLocationFromFile(List <tblLocation> location) { // Create folder if it does not exist Directory.CreateDirectory(locationFolder); string file = locationFolder + @"\Locations.txt"; int id = 0; using (EmployeeDBEntities context = new EmployeeDBEntities()) { if (File.Exists(file)) { string[] readFile = File.ReadAllLines(file); for (int i = 0; i < readFile.Length; i++) { if (!string.IsNullOrEmpty(readFile[i])) { string[] trim = readFile[i].Split(','); string address = trim[0]; string city = trim[1]; string country = trim[2]; id++; tblLocation loc = new tblLocation() { LocationID = id, LocationAddress = address, City = city, Country = country }; location.Add(loc); context.tblLocations.Add(loc); context.SaveChanges(); } } } } }
public bool UpdateEmployee(int id, EmployeeModel employeeModel) { //using (var context = new EmployeeDBEntities()) //{ // var employee = context.Employee.FirstOrDefault(x => x.Id == id); // if (employee != null) // { // employee.FirstName = employeeModel.FirstName; // employee.LastName = employeeModel.LastName; // employee.Code = employeeModel.Code; // employee.Email = employeeModel.Email; // // employee.Address = employeeModel.Email; // } // context.SaveChanges(); // return true; //} //update using entity state in single hit to database using (var context = new EmployeeDBEntities()) { var employee = new Employee(); if (employee != null) { employee.Id = employeeModel.Id; employee.FirstName = employeeModel.FirstName; employee.LastName = employeeModel.LastName; employee.Code = employeeModel.Code; employee.Email = employeeModel.Email; employee.AddressId = employeeModel.AddressId; } //changing the state to Modified context.Entry(employee).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return(true); } }
//postavljanje POST //FromBody atribut kaže da će podaci biti doveni iz request bodya public HttpResponseMessage Post([FromBody] Employees employee) { try { using (EmployeeDBEntities entites = new EmployeeDBEntities()) { entites.Employees.Add(employee); entites.SaveChanges(); //da dobijemo status kod 201 za Rest kovenciju var message = Request.CreateResponse(HttpStatusCode.Created, employee); message.Headers.Location = new Uri(Request.RequestUri + employee.ID.ToString()); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Get(string gender = "All") { string username = Thread.CurrentPrincipal.Identity.Name; using (EmployeeDBEntities entities = new EmployeeDBEntities()) { switch (username.ToLower()) { // case "all": // return Request.CreateResponse(HttpStatusCode.OK, entities.Employees.ToList()); case "male": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.Where(x => x.Gender.ToLower() == "male").ToList())); case "female": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.Where(x => x.Gender.ToLower() == gender).ToList())); default: return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "value for gender must be all, male or female. It is invalid to use: " + gender)); } } }
public static bool InsertEmployee() { bool bReturn = false; try { EmployeeDBEntities myEntityDb = new EmployeeDBEntities(); Employee emp = new Employee(); emp.Id = Guid.NewGuid(); emp.Name = "ABC + " + new Random().Next().ToString(); emp.DOB = DateTime.Now; emp.EmailId = emp.Name + "@abc.com"; myEntityDb.Employees.Add(emp); myEntityDb.SaveChanges(); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex); } return(bReturn); }
public HttpResponseMessage Delete(int id) { try { using (EmployeeDBEntities entities = new EmployeeDBEntities()) { var employee = entities.Employees.FirstOrDefault(e => e.ID == id); if (employee != null) { entities.Employees.Remove(employee); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, employee)); } return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id = " + id + " not found to delete")); } } catch (Exception exception) { return(Request.CreateResponse(HttpStatusCode.BadRequest, exception)); } }
public IHttpActionResult SaveEmployee(EmployeeModel model) { if (!ModelState.IsValid) { return(BadRequest("Invalid employee model !!")); } using (var ctx = new EmployeeDBEntities()) { ctx.Employees.Add(new Employee() { EmpCode = model.EmpCode, FirstName = model.FirstName, LastName = model.LastName, Designation = model.Designation, Salary = model.Salary }); ctx.SaveChanges(); } return(Ok()); }
public IHttpActionResult GetEmployeeByCode(int empCode) { EmployeeModel model = new EmployeeModel(); using (var ctx = new EmployeeDBEntities()) { var employee = ctx.Employees.Where(e => e.EmpCode == empCode) .FirstOrDefault <Employee>(); if (employee != null) { model.EmpCode = employee.EmpCode; model.FirstName = employee.FirstName; model.LastName = employee.LastName; model.Designation = employee.Designation; model.Salary = employee.Salary; } } return(Ok(model)); }
/// <summary> /// Delete Employee Information /// </summary> /// <param name="Emp"></param> /// <returns></returns> public string Delete_Employee(Employee Emp) { if (Emp != null) { using (EmployeeDBEntities Obj = new EmployeeDBEntities()) { var Emp_ = Obj.Entry(Emp); if (Emp_.State == System.Data.Entity.EntityState.Detached) { Obj.Employees.Attach(Emp); Obj.Employees.Remove(Emp); } Obj.SaveChanges(); return("Employee Deleted Successfully"); } } else { return("Employee Not Deleted! Try Again"); } }
public HttpResponseMessage EmployeeDetails(string gender = "All") { using (EmployeeDBEntities entities = new EmployeeDBEntities()) { switch (gender.ToLower()) { case "all": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.ToList())); case "male": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.Where(e => e.Gender.ToLower() == "male").ToList())); case "female": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.Where(e => e.Gender.ToLower() == "female").ToList())); default: return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Value for gender given " + gender + " is invalid")); } } }
public tblEmployee GetEmployeeByRegnumber(string registrationNumber) { try { using (EmployeeDBEntities context = new EmployeeDBEntities()) { tblEmployee emoloyee = (from e in context.tblEmployees where e.RegistrationNumber.Equals(registrationNumber) select e).First(); return(emoloyee); //FileLoging fileLog = FileLoging.Instance(); //fileLog.LogDeleteUserToFile(userToDelete); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
public HttpResponseMessage Post([FromBody] Employee employee) { try { using (EmployeeDBEntities entities = new EmployeeDBEntities()) { entities.Employees.Add(employee); entities.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, employee); message.Headers.Location = new Uri(Request.RequestUri + employee.ID.ToString()); System.Diagnostics.Debug.WriteLine(Request.RequestUri); System.Diagnostics.Debug.WriteLine(message.Headers.Location); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Get(int Id) { try { using (EmployeeDBEntities employeeDBRntities = new EmployeeDBEntities()) { var entity = employeeDBRntities.Employees.FirstOrDefault(e => e.ID == Id); if (entity != null) { var message = Request.CreateResponse(HttpStatusCode.OK, entity); message.Headers.Location = new Uri(Request.RequestUri.ToString()); return(message); } return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee not found with id: {0}" + Id.ToString())); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } }
//When we create the ADO class and had it create the model from our database //VS automatically create several class for us in the background including the //EmployeeDBEntities which handles the connections to our database //When the controller gets the data from the database, the job of the controller is done //it then sends the data to the API pipeline and depending on the formant the client has requested //it will then format the data according i.e. XML, JSON public HttpResponseMessage Get(string gender = "All") { var employees = new EmployeeDBEntities(); switch (gender.ToLower()) { case "all": return(Request.CreateResponse(HttpStatusCode.OK, employees)); case "male": return(Request.CreateResponse(HttpStatusCode.OK, employees.Employees.Where(e => e.Gender == "male"))); case "female": return(Request.CreateResponse(HttpStatusCode.OK, employees.Employees.Where(e => e.Gender == "female"))); default: return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, $"Value for gender must be all, male, or female, {gender} is invalid.")); } }
public bool DeleteEmployee(int id) { using (var context = new EmployeeDBEntities()) { var result = new Employee() { Id = id }; context.Entry(result).State = System.Data.Entity.EntityState.Deleted; //var result = context.Employee.FirstOrDefault(x => x.Id == id); //{ // if(result != null) // { // context.Employee.Remove(result); context.SaveChanges(); return(true); // } // return false; //} } }
public HttpResponseMessage Get(string gender = "ALL") { string username = Thread.CurrentPrincipal.Identity.Name; using (EmployeeDBEntities entities = new EmployeeDBEntities()) { switch (username.ToLower()) { case "male": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.Where(emp => emp.Gender.ToLower().Equals(username)).ToList())); case "female": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.Where(emp => emp.Gender.ToLower().Equals(username)).ToList())); default: return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "uid pass not correct")); } } }
public tblGender GetGenderByName(string gender) { try { using (EmployeeDBEntities context = new EmployeeDBEntities()) { tblGender genderFromDB = (from g in context.tblGenders where g.Gender.Equals(gender) select g).First(); return(genderFromDB); //FileLoging fileLog = FileLoging.Instance(); //fileLog.LogDeleteUserToFile(userToDelete); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
public HttpResponseMessage Get(string gender = "All") { string username = Thread.CurrentPrincipal.Identity.Name; using (EmployeeDBEntities entities = new EmployeeDBEntities()) { switch (username.ToLower()) { case "male": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.Where(e => e.Gender.ToLower() == "male").ToList())); case "female": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.Where(e => e.Gender.ToLower() == "female").ToList())); default: return(Request.CreateResponse(HttpStatusCode.BadRequest)); } } }
//This method is modified below with query string //public IEnumerable<Employee> Get() //{ // EmployeeDBEntities entities = new EmployeeDBEntities(); // return entities.Employees.ToList(); //} //Below method is for creating a response for input query string for gender either "All", "Male", "Female" //Anything other than this will throw an error 404 bad request. //[DisableCors] //this is disable cors for this method //[RequireHttps] //this is to mention that this perticular action requires https request. public HttpResponseMessage Get(string gender = "All") { EmployeeDBEntities entities = new EmployeeDBEntities(); string username = Thread.CurrentPrincipal.Identity.Name; switch (username.ToLower()) { case "all": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.ToList())); case "female": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.Where(e => e.Gender.ToLower() == "female").ToList())); case "male": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.Where(e => e.Gender.ToLower() == "male").ToList())); default: return(Request.CreateResponse(HttpStatusCode.BadRequest)); // return Request.CreateResponse(HttpStatusCode.BadRequest, "Mentioned gender " +gender + " is invalid. It should be either Male,Female or All"); } }
/// <summary> /// Update Employee Information /// </summary> /// <param name="Emp"></param> /// <returns></returns> public string Update_Employee(Employee Emp) { if (Emp != null) { using (EmployeeDBEntities Obj = new EmployeeDBEntities()) { var Emp_ = Obj.Entry(Emp); Employee EmpObj = Obj.Employees.Where(x => x.ID == Emp.ID).FirstOrDefault(); EmpObj.FirstName = Emp.FirstName; EmpObj.LastName = Emp.LastName; EmpObj.Gender = Emp.Gender; EmpObj.Salary = Emp.Salary; Obj.SaveChanges(); return("Employee Updated Successfully"); } } else { return("Employee Not Updated! Try Again"); } }
public vwMenager GetMenagerByName(string name) { try { using (EmployeeDBEntities context = new EmployeeDBEntities()) { vwMenager menager = (from m in context.vwMenagers where m.Menager.Equals(name) select m).First(); return(menager); //FileLoging fileLog = FileLoging.Instance(); //fileLog.LogDeleteUserToFile(userToDelete); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
public tblSector GetSectorByName(string sector) { try { using (EmployeeDBEntities context = new EmployeeDBEntities()) { tblSector sectorFromDB = (from s in context.tblSectors where s.SectorName.Equals(sector) select s).First(); return(sectorFromDB); //FileLoging fileLog = FileLoging.Instance(); //fileLog.LogDeleteUserToFile(userToDelete); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
public HttpResponseMessage Delete(int id) { try { EmployeeDBEntities entities = new EmployeeDBEntities(); var employee = entities.Employees.FirstOrDefault(e => e.ID == id); if (employee != null) { entities.Employees.Remove(employee); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, $"Employee with {id} not found")); } } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }
public HttpResponseMessage Post([FromBody] Employee employee) { try { using (EmployeeDBEntities entities = new EmployeeDBEntities()) { entities.Employees.Add(employee); entities.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, employee); message.Headers.Location = new Uri(Request.RequestUri + employee.ID.ToString()); return(message); //{"firstName":"Sam","lastName":"Wicht","gender":"Male","salary":54000} } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Put(int id, [FromBody] Employee e) { using (EmployeeDBEntities entities = new EmployeeDBEntities()) { var entity = entities.Employees.FirstOrDefault(x => x.ID == id); if (entity != null) { entity.FirstName = e.FirstName; entity.LastName = e.LastName; entity.Gender = e.Gender; entity.Salary = e.Salary; entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Id " + id + " not found to update")); } } }
public HttpResponseMessage Post([FromBody] Employee e) { try { using (EmployeeDBEntities entities = new EmployeeDBEntities()) { entities.Database.Connection.Open(); entities.Employees.Add(e); entities.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, e); message.Headers.Location = new Uri(Request.RequestUri + "/" + e.ID.ToString()); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
/// <summary> /// method that deletes employee from Employee table in database /// it logs this action to logs.txt file /// </summary> /// <param name="employeeID"></param> public void DeleteEmployee(int employeeID) { try { using (EmployeeDBEntities context = new EmployeeDBEntities()) { tblEmployee employeeToDelete = (from e in context.tblEmployees where e.EmployeeID == employeeID select e).First(); context.tblEmployees.Remove(employeeToDelete); context.SaveChanges(); //FileLoging fileLog = FileLoging.Instance(); //fileLog.LogDeleteUserToFile(userToDelete); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString()); } }
public HttpResponseMessage Get(int id) { try { using (EmployeeDBEntities entities = new EmployeeDBEntities()) { var entity = entities.Employees.FirstOrDefault(e => e.ID == id); if (entity != null) { return(Request.CreateResponse(HttpStatusCode.OK, entity)); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id " + id.ToString() + " Not found.")); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public static bool Login(string username, string password, out string role) { role = string.Empty; using (EmployeeDBEntities entities = new EmployeeDBEntities()) { var entity = entities.Users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.OrdinalIgnoreCase) && u.Password == password); if (entity == null) { return(false); } else { role = entity.Role; return(true); } } //using (EmployeeDBEntities entities = new EmployeeDBEntities()) //{ // return entities.Users.Any(u => u.UserName.Equals(username, StringComparison.OrdinalIgnoreCase) && u.Password == password); //} }
public HttpResponseMessage Post([FromBody] Employee employee) { try { using (EmployeeDBEntities entities = new EmployeeDBEntities()) { entities.Employees.Add(employee); entities.SaveChanges(); HttpResponseMessage message = Request.CreateResponse(HttpStatusCode.Created, employee); //there are two ways to produce the location. use "Route name" is better. //since in the other way, you are not sure if there is a "/" at the end of request url. and can cause problem. //message.Headers.Location = new Uri(Request.RequestUri + employee.ID.ToString()); message.Headers.Location = new Uri(Url.Link("GetEmployeeById", new { id = employee.ID })); return(message); } }catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }