// PUT api/EmployeeProfileApi/5
        public async Task<IHttpActionResult> PutEmployeeProfile(int id, EmployeeProfile employeeprofile)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != employeeprofile.EmployeeId)
            {
                return BadRequest();
            }

            db.Entry(employeeprofile).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeProfileExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        public async Task<IHttpActionResult> PostEmployeeProfile(EmployeeProfile employeeprofile)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.EmployeeProfile.Add(employeeprofile);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (EmployeeProfileExists(employeeprofile.EmployeeId))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = employeeprofile.EmployeeId }, employeeprofile);
        }
예제 #3
0
        public async Task<IHttpActionResult> PostEmployee(EmployeeViewModel emp)
        {
            
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var branch = GetBranch(emp.Branch);//getting branch data
            var empstatus = GetStatus(emp.EmployementStatus);//getting employee status
            var emptype = GetType(emp.EmployementType);//getting employement status of employee

            //creating a new employee
            var employee = new Employee();
            employee.FirstName = emp.FirstName;
            employee.LastName = emp.LastName;
            employee.OtherName = emp.OtherName;
            employee.Email = emp.Email;
            employee.PhoneNumber = emp.PhoneNumber;
            employee.Passport = emp.Passport;
            employee.Birthdate = emp.Birthdate;
            employee.Gender = emp.Gender;
            employee.ResidentialAddress = emp.ResidentialAddress;
            employee.Nationality = emp.Nationality;
            employee.BranchId = branch.BranchId;
            employee.CompanyEmail = emp.CompanyEmail;
            employee.CompanyPhoneNumber = emp.CompanyPhoneNumber;
            employee.EmployementStatusId = empstatus.EmployementStatusdId;
            employee.EmployementTypeId = emptype.EmploymentTypeId;

            //adding the employee to roles

            //adding titles to emloyee

            //adding a user profile
           
            db.Employees.Add(employee);
            var profile = new EmployeeProfile
            {
                EmployeeId = employee.EmployeeId,
                Name = employee.FirstName.Substring(0,3)+employee.LastName.Substring(employee.LastName.Length-3)
            };
            db.EmployeeProfile.Add(profile);
            await db.SaveChangesAsync();
            emp.EmployeeId = employee.EmployeeId;

            //returning back back employee along with his/her branch details
            //avoiding circular reference exception by returning from the model
            BranchOffice b = new BranchOffice
            {
                BranchId = branch.BranchId,
                BranchName = branch.BranchName
            };
            emp.Branch = b;
            return CreatedAtRoute("DefaultApi", new { id = employee.EmployeeId }, emp);
        }