// POST rest/login public ErrorResponse Post([FromBody] Login login) { if (login == null || login.username == null || login.password == null || login.ContainsExtra) { return(new ErrorResponse(ErrorNumber.INVALID_USERNAME_OR_PASSWORD, "Invalid login object")); } if (User.Identity.Name == login.username || User.IsInRole("ROLE_SET_PASSWORD")) { kenzanEntities ke = new kenzanEntities(); Employee emp = ke.Employees .Where <Employee>(e => e.username == login.username && e.bStatus == Status.ACTIVE) .FirstOrDefault <Employee>(); if (emp == null) { return(new ErrorResponse(ErrorNumber.INVALID_USERNAME_OR_PASSWORD, "No user found")); } emp.password = crypto.BCrypt.HashPassword(login.password); if (ke.SaveChanges() != 1) { return(new ErrorResponse(ErrorNumber.UNKNOWN_ERROR, "Unable to save password")); } else { return(new ErrorResponse()); } } else { return(new ErrorResponse(ErrorNumber.NOT_AUTHORIZED_FOR_OPERATION, "Not authorized")); } }
// POST: rest/upd_emp public ErrorResponse Post([FromBody] JSONEmployee json_employee) { if (!ModelState.IsValid || json_employee._id == null) { return(new ErrorResponse(ErrorNumber.CANNOT_INSERT_MISSING_FIELDS, "No records added")); } if (json_employee.ContainsExtra) { return(new ErrorResponse(ErrorNumber.CANNOT_INSERT_UNKNOWN_FIELDS, "Extra fields in json")); } //Employee employee = new Employee(json_employee); kenzanEntities ke = new kenzanEntities(); ke.Configuration.ProxyCreationEnabled = false; Employee emp = ke.Employees .Where(e => e.id == json_employee._id && e.bStatus == Status.ACTIVE) .FirstOrDefault <Employee>(); if (emp == null) { return(new ErrorResponse(ErrorNumber.CANNOT_UPDATE_NONEXISTENT_RECORD, "Nonexistant record")); } emp.dateOfBirth = json_employee.dateOfBirth.Value; emp.dateOfEmployment = json_employee.dateOfEmployment; emp.firstName = json_employee.firstName; emp.lastName = json_employee.lastName; emp.middleInitial = json_employee.middleInitial; emp.bStatus = json_employee.bStatus.Value; emp.username = json_employee.username; try { if (ke.SaveChanges() != 1) { return(new ErrorResponse(emp.id, ErrorNumber.UNKNOWN_ERROR, "Error updating record")); } else { return(new ErrorResponse()); } } catch (Exception e) { while (e.InnerException != null) { e = e.InnerException; } if (e.Message.Contains("Duplicate")) { return(new ErrorResponse(ErrorNumber.DUPLICATE_RECORD, e.Message)); } else { return(new ErrorResponse(ErrorNumber.CANNOT_INSERT_MISSING_FIELDS, e.Message)); } } }
// POST: rest/add_emp public ErrorResponse Post([FromBody] JSONEmployee json_employee) { ErrorResponse err; if (!ModelState.IsValid || json_employee._id != null) { return(new ErrorResponse(ErrorNumber.CANNOT_INSERT_MISSING_FIELDS, "No records added")); } if (json_employee.ContainsExtra) { return(new ErrorResponse(ErrorNumber.CANNOT_INSERT_UNKNOWN_FIELDS, "Extra fields in json")); } Employee employee = new Employee(json_employee); kenzanEntities ke = new kenzanEntities(); ke.Employees.Add(employee); try { if (ke.SaveChanges() == 0) { err = new ErrorResponse(ErrorNumber.UNKNOWN_ERROR, "No records added"); } else { err = new ErrorResponse(employee.id); } } catch (Exception e) { while (e.InnerException != null) { e = e.InnerException; } if (e.Message.Contains("Duplicate")) { err = new ErrorResponse(ErrorNumber.DUPLICATE_RECORD, e.Message); } else { err = new ErrorResponse(ErrorNumber.CANNOT_INSERT_MISSING_FIELDS, e.Message); } } return(err); }
// GET: rest/delete_emp [System.Web.Http.HttpGet] public ErrorResponse Delete_Emp([FromUri] int id) { kenzanEntities ke = new kenzanEntities(); Employee emp = ke.Employees .Where(e => e.id == id && e.bStatus == Status.ACTIVE) .FirstOrDefault <Employee>(); if (emp == null) { return(new ErrorResponse(ErrorNumber.CANNOT_DELETE_NONEXISTENT_RECORD, "Nonexistent record")); } emp.bStatus = Status.INACTIVE; if (ke.SaveChanges() != 1) { return(new ErrorResponse(ErrorNumber.DUPLICATE_RECORD, "Duplicate record")); } else { return(new ErrorResponse()); } }