public CarEmployeeMappingModel GetCarEmployeeMappingById(int carEmployeeMappingId)
 {
     ////unitOfWork.StartTransaction();
     CarEmployeeMappingRepository repo = new CarEmployeeMappingRepository(unitOfWork);
     CarEmployeeMappingModel carEmployeeMappingModel = new CarEmployeeMappingModel();
     CarEmployeeMapping carEmployeeMapping = new CarEmployeeMapping();
     AutoMapper.Mapper.Map(carEmployeeMappingModel, carEmployeeMapping);
     carEmployeeMapping = repo.GetAll().Where(x => x.CarTypeId == carEmployeeMappingId).FirstOrDefault();
        // //unitOfWork.Commit();
     AutoMapper.Mapper.Map(carEmployeeMapping, carEmployeeMappingModel);
     return carEmployeeMappingModel;
 }
        public async Task<IHttpActionResult> UpdateServiceProviderAsIndividual()
        {

            try
            {
                //var userAgent = HttpContext.Current.Request.UserAgent;
                //var deviceType = HttpContext.Current.Request.Headers.GetValues("device_type");
                if (!Request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }

                var root = HttpContext.Current.Server.MapPath(Utility.Constants.BASE_FILE_UPLOAD_PATH);
                Directory.CreateDirectory(root);
                var provider = new MultipartFormDataStreamProvider(root);
                var resultModel = await Request.Content.ReadAsMultipartAsync(provider);
                if (resultModel.FormData["model"] == null)
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }
                var result = resultModel.FormData["model"];
                EmployeeBindingModel employeeBindingModel = new EmployeeBindingModel();
                //var model = result.Substring(1, result.Length - 2);
                employeeBindingModel = JsonConvert.DeserializeObject<EmployeeBindingModel>(result);
                //AutoMapper.Mapper.Map(employeeBindingModel, clientModel);
                //var count = resultModel.FormData.Count;

                //employeeBindingModel.EmployeeId = resultModel.FormData[0];
                //employeeBindingModel.FirstName = resultModel.FormData[1];
                //employeeBindingModel.LastName = resultModel.FormData[2];
                //employeeBindingModel.Description = resultModel.FormData[3];
                //employeeBindingModel.CityId = Convert.ToInt32(resultModel.FormData[4]);
                //employeeBindingModel.DistrictId = Convert.ToInt32(resultModel.FormData[5]);
                //employeeBindingModel.CarTypeId = Convert.ToInt32(resultModel.FormData[6]);
                //employeeBindingModel.LicensePlateNumber = resultModel.FormData[7];

                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                else
                {
                    try
                    {
                        EmployeeModel employeeModelForCheckRecord = new EmployeeModel();
                        employeeModelForCheckRecord = employeeService.GetEmployeeById(employeeBindingModel.EmployeeId);

                        if (resultModel.FileData.Count > 0)
                        {
                            string fileName;

                            if (HttpContext.Current.Request.Files != null)
                            {
                                for (var i = 0; i < resultModel.FileData.Count; i++)
                                {
                                    var file = HttpContext.Current.Request.Files[i];
                                    fileName = file.FileName;


                                    if (i == 0)
                                    {

                                        file.SaveAs(Path.Combine(root, Utility.Constants.EMPLOYEE_PROFILE_PATH, fileName));
                                        employeeBindingModel.ProfileImage = fileName;
                                    }
                                    else
                                    {
                                        file.SaveAs(Path.Combine(root, Utility.Constants.NATIONAL_ID_PATH, fileName));
                                        employeeBindingModel.NationalIdAndIqamaNumber = fileName;
                                    }

                                }

                            }


                        }
                        if (employeeBindingModel.ProfileImage == null)
                        {
                            employeeBindingModel.ProfileImage = employeeModelForCheckRecord.ProfileImage;
                        }
                        if (employeeBindingModel.NationalIdAndIqamaNumber == null)
                        {
                            employeeBindingModel.NationalIdAndIqamaNumber = employeeModelForCheckRecord.NationalIdAndIqamaNumber;
                        }

                        //Update employee
                        EmployeeModel employeeModelEntity = new EmployeeModel();
                        AutoMapper.Mapper.Map(employeeBindingModel, employeeModelEntity);
                        employeeModelEntity = employeeService.UpadteEmployee(employeeModelEntity);
                        AutoMapper.Mapper.Map(employeeModelEntity, employeeBindingModel);
                        //Add Car type for Employee
                        CarEmployeeMappingModel carEmployeeMappingModel = new CarEmployeeMappingModel();
                        carEmployeeMappingModel.CarTypeId = employeeBindingModel.CarTypeId;
                        carEmployeeMappingModel.EmployeeId = employeeBindingModel.EmployeeId;
                        carEmployeeMappingModel = carEmployeeMappingService.SaveCarEmployeeMapping(carEmployeeMappingModel);
                        //save location for employee

                        UserLocationModel locationModel = new UserLocationModel();
                        locationModel.UserId = employeeBindingModel.EmployeeId;
                        locationModel.CityId = employeeBindingModel.CityId;
                        locationModel.DistrictId = 1;
                        userLocationService.InsertUserLocation(locationModel);
                        //locationModel = userLocationService.FindLocationById(employeeBindingModel.EmployeeId);
                        //if (locationModel.UserLocationId != 0)
                        //{
                        //    userLocationService.UpadteUserLocation(locationModel);
                        //}


                        var user = await UserManager.FindByIdAsync(employeeBindingModel.EmployeeId);
                        if (user.Email != employeeBindingModel.Email)
                        {
                            IdentityResult userResult = await UserManager.UpdateAsync(user);
                        }
                    }
                    catch (Exception ex)
                    {
                        Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                        return BadRequest(ex.Message);
                    }

                }

                return Json(employeeBindingModel);
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                return BadRequest(ex.Message);
            }
        }
        public async Task<IHttpActionResult> PutEmployee()
        {

            try
            {
                if (!Request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }

                var root = HttpContext.Current.Server.MapPath(Utility.Constants.BASE_FILE_UPLOAD_PATH);
                Directory.CreateDirectory(root);
                var provider = new MultipartFormDataStreamProvider(root);
                var resultModel = await Request.Content.ReadAsMultipartAsync(provider);
                if (resultModel.FormData["model"] == null)
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }
                var model = resultModel.FormData["model"];

                EmployeeBindingModel employeeBindingModel = new EmployeeBindingModel();
                employeeBindingModel = JsonConvert.DeserializeObject<EmployeeBindingModel>(model);

                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                else
                {

                    EmployeeModel employeeModelForCheckRecord = new EmployeeModel();
                    employeeModelForCheckRecord = employeeService.GetEmployeeById(employeeBindingModel.EmployeeId);

                    if (resultModel.FileData.Count > 0)
                    {
                        string fileName;

                        if (HttpContext.Current.Request.Files != null)
                        {
                            for (var i = 0; i < resultModel.FileData.Count; i++)
                            {
                                var file = HttpContext.Current.Request.Files[i];
                                fileName = file.FileName;


                                if (i == 0)
                                {

                                    file.SaveAs(Path.Combine(root, Utility.Constants.EMPLOYEE_PROFILE_PATH, fileName));
                                    employeeBindingModel.ProfileImage = fileName;
                                }
                                else
                                {
                                    file.SaveAs(Path.Combine(root, Utility.Constants.NATIONAL_ID_PATH, fileName));
                                    employeeBindingModel.NationalIdAndIqamaNumber = fileName;
                                }

                            }

                        }


                    }
                    if (employeeBindingModel.ProfileImage == null)
                    {
                        employeeBindingModel.ProfileImage = employeeModelForCheckRecord.ProfileImage;
                    }
                    if (employeeBindingModel.NationalIdAndIqamaNumber == null)
                    {
                        employeeBindingModel.NationalIdAndIqamaNumber = employeeModelForCheckRecord.NationalIdAndIqamaNumber;
                    }
                    //if (employeeBindingModel.IqamaNumber == null)
                    //{
                    //    employeeBindingModel.IqamaNumber = employeeModelForCheckRecord.IqamaNumber;
                    //}
                    try
                    {
                        //Update employee
                        EmployeeModel employeeModelEntity = new EmployeeModel();
                        AutoMapper.Mapper.Map(employeeBindingModel, employeeModelEntity);
                        employeeModelEntity = employeeService.UpadteEmployee(employeeModelEntity);
                        AutoMapper.Mapper.Map(employeeModelEntity, employeeBindingModel);
                        //Add Car type for Employee
                        if (employeeBindingModel.CarTypeId != 0)
                        {
                            CarEmployeeMappingModel carEmployeeMappingModel = new CarEmployeeMappingModel();
                            carEmployeeMappingModel.CarTypeId = employeeBindingModel.CarTypeId;
                            carEmployeeMappingModel.EmployeeId = employeeBindingModel.EmployeeId;
                            CarEmployeeMappingModel carEmployeeMappingModelCheck = new CarEmployeeMappingModel();
                            carEmployeeMappingModelCheck = carEmployeeMappingService.GetCarEmployeeMappingByEmployeeId(carEmployeeMappingModel.EmployeeId);
                            carEmployeeMappingModelCheck.CarTypeId = employeeBindingModel.CarTypeId;
                            carEmployeeMappingModelCheck.EmployeeId = employeeBindingModel.EmployeeId;
                            if (carEmployeeMappingModelCheck.CarEmployeeMappingId == 0)
                            {
                                carEmployeeMappingModel = carEmployeeMappingService.SaveCarEmployeeMapping(carEmployeeMappingModelCheck);
                            }
                            else
                            {
                                carEmployeeMappingModel = carEmployeeMappingService.UpadteCarEmployeeMapping(carEmployeeMappingModelCheck);
                            }
                        }


                        //save location for employee
                        var location = resultModel.FormData["LocationModel"];
                        UserLocationModel locationModel = new UserLocationModel();
                        UserLocationModel userlocationModel = new UserLocationModel();
                        locationModel = JsonConvert.DeserializeObject<UserLocationModel>(location);
                        locationModel.UserId = employeeBindingModel.EmployeeId;
                        locationModel.DistrictId = 1;
                        bool Exists;
                        Exists = userLocationService.CheckExistance(locationModel.UserId);
                        if (!Exists && locationModel.CityId != 0)
                        {
                            userLocationService.InsertUserLocation(locationModel);
                        }
                        else if (Exists)
                        {
                            userlocationModel = userLocationService.FindLocationById(locationModel.UserId);
                            locationModel.UserLocationId = userlocationModel.UserLocationId;
                            userLocationService.UpadteUserLocation(locationModel);
                        }
                        //check service provider as individual and update service provider
                        IndividualBindingModel individualBindingModel = new IndividualBindingModel();
                        RegisterServiceProviderBindingModel serviceProviderBindingModel = new RegisterServiceProviderBindingModel();
                        ServiceProviderModel serviceProvider = new ServiceProviderModel();
                        serviceProvider = serviceProviderService.GetServiceProviderById(employeeBindingModel.EmployeeId);
                        if (serviceProvider != null && serviceProvider.ServiceProviderType == ServiceProviderType.Individual)
                        {

                            serviceProvider.CompanyLogoPath = employeeBindingModel.ProfileImage;
                            serviceProvider.RegistrationCertificatePath = employeeBindingModel.NationalIdAndIqamaNumber;
                            serviceProvider.Description = employeeBindingModel.Description;

                            serviceProvider = serviceProviderService.UpadteServiceProvider(serviceProvider);
                        }
                        var user = await UserManager.FindByIdAsync(employeeBindingModel.EmployeeId);
                        if (user != null)
                        {
                            if (user.Email != employeeBindingModel.Email || user.PhoneNumber != employeeBindingModel.PhoneNumber)
                            {
                                user.Email = employeeBindingModel.Email;
                                user.PhoneNumber = employeeBindingModel.PhoneNumber;
                                IdentityResult result = await UserManager.UpdateAsync(user);
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                        return BadRequest(ex.Message);
                    }

                }

                return Ok();
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                return BadRequest(ex.Message);
            }
        }
 public IHttpActionResult GetEmployee(string id)
 {
     //Get employee
     EmployeeModel employeeModel = new EmployeeModel();
     EmployeeBindingModel employeeBindingModel = new EmployeeBindingModel();
     List<JobModel> jobModel = new List<JobModel>();
     List<JobBindingModel> jobBindingModel = new List<JobBindingModel>();
     employeeModel = employeeService.GetEmployeeById(id);
     AutoMapper.Mapper.Map(employeeModel, employeeBindingModel);
     //Get Details of employee from user table
     var Email = UserManager.FindById(id) != null ? UserManager.FindById(id).Email : "";
     var PhoneNumber = UserManager.FindById(id) != null ? UserManager.FindById(id).PhoneNumber : "";
     //Get Car Type of employee
     CarEmployeeMappingModel carEmployeeMappingModel = new CarEmployeeMappingModel();
     carEmployeeMappingModel = carEmployeeMappingService.GetCarEmployeeMappingByEmployeeId(id);
     //Get Location of employee
     userLocationModel = userLocationService.FindLocationById(id);
     employeeBindingModel.UserLocationModel = userLocationModel;
     employeeBindingModel.CarTypeId = carEmployeeMappingModel.CarTypeId;
     employeeBindingModel.Email = Email;
     employeeBindingModel.PhoneNumber = PhoneNumber;
     //Get rating for employee
     jobModel = jobService.GetJobListByEmployeeId(id);
     AutoMapper.Mapper.Map(jobModel, jobBindingModel);
     List<RatingModel> ratingModel = new List<RatingModel>();
     ratingModel = ratingService.GetAllRatings();
     jobBindingModel.ForEach(x =>
     {
         x.RatingModel = ratingModel.Where(j => j.JobId == x.JobId).ToList();
     });
     employeeBindingModel.JobBindingModel = jobBindingModel;
     var serviceProvider = serviceProviderService.GetServiceProviderById(id);
     if (serviceProvider != null)
     {
         employeeBindingModel.model = serviceProvider;
     }
     return Json(employeeBindingModel);
 }
 public CarEmployeeMappingModel UpadteCarEmployeeMapping(CarEmployeeMappingModel model)
 {
     ////unitOfWork.StartTransaction();
     CarEmployeeMappingRepository repo = new CarEmployeeMappingRepository(unitOfWork);
     CarEmployeeMapping carEmployeeMapping = new CarEmployeeMapping();
     carEmployeeMapping = repo.GetAll().Where(x => x.EmployeeId == model.EmployeeId).FirstOrDefault();
     AutoMapper.Mapper.Map(model, carEmployeeMapping);
     repo.Update(carEmployeeMapping);
     ////unitOfWork.Commit();
     AutoMapper.Mapper.Map(carEmployeeMapping, model);
     return model;
 }
 public CarEmployeeMappingModel SaveCarEmployeeMapping(CarEmployeeMappingModel model)
 {
     // //unitOfWork.StartTransaction();
     CarEmployeeMappingRepository repo = new CarEmployeeMappingRepository(unitOfWork);
     CarEmployeeMapping carEmployeeMapping = new CarEmployeeMapping();
     AutoMapper.Mapper.Map(model, carEmployeeMapping);
     repo.Insert(carEmployeeMapping);
        // //unitOfWork.Commit();
     AutoMapper.Mapper.Map(carEmployeeMapping, model);
     return model;
 }