public ViewResult Edit(int id)
        {
            Employee employee = _employeeRepository.GetEmployee(id);
            EmployeeCreateViewModel employeeEditViewModel = new EmployeeCreateViewModel
            {
                Id                = employee.Id,
                Name              = employee.Name,
                Email             = employee.Email,
                Department        = employee.Department,
                ExistingPhotoPath = employee.PhotoPath,
                ReceiptDate       = employee.ReceiptDate,
                ReimburseDate     = employee.ReimburseDate,
                ReceiptAmount     = employee.ReceiptAmount
            };

            return(View(employeeEditViewModel));
        }
Ejemplo n.º 2
0
        [ValidateAntiForgeryToken]      //Prevents cross-site Request Forgery Attacks
        public async Task <IActionResult> Create(EmployeeCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var employee = new Employee
                {
                    Id                  = model.Id,
                    EmployeeNo          = model.EmployeeNo,
                    FirstName           = model.FirstName,
                    MiddleName          = model.MiddleName,
                    LastName            = model.LastName,
                    FullName            = model.FullName,
                    Gender              = model.Gender,
                    Email               = model.Email,
                    DOB                 = model.DOB,
                    DateJoined          = model.DateJoined,
                    NationalInsuranceNo = model.NationalInsuranceNo,
                    PaymentMethod       = model.PaymentMethod,
                    StudentLoan         = model.StudentLoan,
                    UnionMember         = model.UnionMember,
                    Address             = model.Address,
                    City                = model.City,
                    Phone               = model.Phone,
                    Postcode            = model.Postcode,
                    Designation         = model.Designation,
                };


                if (model.ImageUrl != null && model.ImageUrl.Length > 0)
                {
                    var uploadDir   = @"images/employee";
                    var fileName    = Path.GetFileNameWithoutExtension(model.ImageUrl.FileName);
                    var extension   = Path.GetExtension(model.ImageUrl.FileName);
                    var webRootPath = _env.WebRootPath;
                    fileName = DateTime.UtcNow.ToString("yymmssfff") + fileName + extension;
                    var path = Path.Combine(webRootPath, uploadDir, fileName);
                    await model.ImageUrl.CopyToAsync(new FileStream(path, FileMode.Create)); //Copy image to the filestream

                    employee.ImageUrl = "/" + uploadDir + "/" + fileName;                    //The Image URL that persists to the database
                }
                await _employeeService.CreateAsync(employee);

                return(RedirectToAction(nameof(Index)));
            }
            return(View());      //If Model state is not valid return View
        }
Ejemplo n.º 3
0
 public IActionResult Create(EmployeeCreateViewModel employeeCreateViewModel)
 {
     if (ModelState.IsValid)
     {
         string   uniqueFileName = UploadedFileName(employeeCreateViewModel);
         Employee newEmp         = new Employee()
         {
             Name       = employeeCreateViewModel.Name,
             Department = employeeCreateViewModel.Department,
             Email      = employeeCreateViewModel.Email,
             PhotoPath  = uniqueFileName
         };
         _employeeRepo.AddEmployee(newEmp);
         return(RedirectToAction("Details", new { id = newEmp.EmployeeID }));
     }
     return(View());
 }
 public IActionResult Create(EmployeeCreateViewModel model)
 {
     if (ModelState.IsValid)
     {
         string   uniqueFileName = ProcessUploadedFile(model);
         Employee newEmployee    = new Employee
         {
             Name       = model.Name,
             Email      = model.Email,
             Department = model.Department,
             PhotoPath  = uniqueFileName
         };
         _employeeRepository.Add(newEmployee);
         return(RedirectToAction("details", new { id = newEmployee.Id }));
     }
     return(View());
 }
        private string GetImage(EmployeeCreateViewModel employeeEditViewModel)
        {
            string uniqueFilename = null;

            if (employeeEditViewModel.Photo != null)
            {
                string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images");
                uniqueFilename = Guid.NewGuid().ToString() + "_" + employeeEditViewModel.Photo.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFilename);
                using (FileStream fs = new FileStream(filePath, FileMode.Create))
                {
                    employeeEditViewModel.Photo.CopyTo(fs);
                }
            }

            return(uniqueFilename);
        }
Ejemplo n.º 6
0
        private string ProcessUploadedFile(EmployeeCreateViewModel model)
        {
            string uniqueFileName = null;

            // If the Photo property on the incoming model is not null then the user has selected an image to upload
            if (model.Photo != null)
            {
                string uploadFolder = Path.Combine(_hostingEnvironment.WebRootPath, "Images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;
                string filePath = Path.Combine(uploadFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.Photo.CopyTo(fileStream);
                }
            }
            return(uniqueFileName);
        }
Ejemplo n.º 7
0
        private async Task <string> ProcessUploadedFile(EmployeeCreateViewModel model)
        {
            string fileName = null;

            if (model.Photo != null)
            {
                var uplodedFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images");
                fileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;
                string filePath = Path.Combine(uplodedFolder, fileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await model.Photo.CopyToAsync(fileStream);
                }
            }

            return(fileName);
        }
Ejemplo n.º 8
0
        private string ProcessUploadFile(EmployeeCreateViewModel model)
        {
            string uniqueFileName = null;

            if (model.Photos != null && model.Photos.Count > 0)
            {
                foreach (IFormFile photo in model.Photos)
                {
                    string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                    photo.CopyTo(new FileStream(filePath, FileMode.Create));
                }
            }

            return(uniqueFileName);
        }
Ejemplo n.º 9
0
        [ValidateAntiForgeryToken] // prevent cross-site request forgery attacks.
        public async Task <IActionResult> Create(EmployeeCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var employee = new Employee
                {
                    Id                  = model.Id,
                    EmployeeNo          = model.EmployeeNo,
                    FirstName           = model.FirstName,
                    LastName            = model.LastName,
                    FullName            = model.FullName,
                    Gender              = model.Gender,
                    DOB                 = model.DOB,
                    Email               = model.Email,
                    DateJoined          = model.DateJoined,
                    NationalInsuranceNo = model.NationalInsuranceNo,
                    PaymentMethod       = model.PaymentMethod,
                    StudentLoan         = model.StudentLoan,
                    UnionMember         = model.UnionMember,
                    Address             = model.Address,
                    City                = model.City,
                    Phone               = model.Phone,
                    PostCode            = model.PostCode,
                    Designation         = model.Designation
                };

                if (string.IsNullOrEmpty(model.ImageUrl.ToString()))
                {
                    var uploadDirectory = @"images/employee";
                    var fileName        = Path.GetFileNameWithoutExtension(model.ImageUrl.FileName);
                    var extension       = Path.GetExtension(model.ImageUrl.FileName);
                    var webRootPath     = _hostingEnvironment.WebRootPath;
                    fileName = DateTime.UtcNow.ToString("yymmssfff") + fileName + extension;
                    var path = Path.Combine(webRootPath, uploadDirectory, fileName);
                    await model.ImageUrl.CopyToAsync(new FileStream(path, FileMode.Create));

                    employee.ImageUrl = "/" + uploadDirectory + "/" + fileName;
                }

                await _employeeService.CreateAsync(employee);

                return(RedirectToAction(nameof(Index)));
            }

            return(View());
        }
Ejemplo n.º 10
0
        private string UploadedFileName(EmployeeCreateViewModel employeeEditViewModel)
        {
            string uniqueFileName = string.Empty;

            if (employeeEditViewModel.Photo != null)
            {
                string uploadfolder = Path.Combine(_hostEnvironment.WebRootPath, "Images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + employeeEditViewModel.Photo.FileName;
                string exactImagePath = Path.Combine(uploadfolder, uniqueFileName);
                using (FileStream fileToSave = new FileStream(exactImagePath, FileMode.Create))
                {
                    employeeEditViewModel.Photo.CopyTo(fileToSave);//create new file on exactImagePath
                }
            }

            return(uniqueFileName);
        }
Ejemplo n.º 11
0
        private string ProcessUploadedFile(EmployeeCreateViewModel model)
        {
            string uniqueFileName = null;

            if (model.Photo != null)
            {
                string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;
                string filepath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filepath, FileMode.Create))
                {
                    model.Photo.CopyTo(fileStream);
                }
            }

            return(uniqueFileName);
        }
 public ActionResult Edit(int id, EmployeeCreateViewModel model)
 {
     if (ModelState.IsValid)
     {
         try
         {
             // TODO: Add update logic here
             _service.UpdateEmployee(model.Id, model.EmployeeNo, model.HireDate, model.DateOfBirth, model.FirstName, model.LastName, model.MiddleName, model.Gender, model.PhoneNo, model.StreetAddress, model.PostCode, model.Suburb, model.State);
             return(RedirectToAction("Index"));
         }
         catch (Exception ex)
         {
             ModelState.AddModelError("", ex);
         }
     }
     return(View(model));
 }
Ejemplo n.º 13
0
 public ActionResult Create(EmployeeCreateViewModel model)
 {
     if (ModelState.IsValid)
     {
         string UniqueFileName = UploadFileProcess(model);
         var    newEmployee    = new Employee
         {
             Name       = model.Name,
             Email      = model.Email,
             Deparment  = model.Deparment,
             StringPath = UniqueFileName
         };
         employeeRepository.AddEmployee(newEmployee);
         return(RedirectToAction("Details", new { id = newEmployee.Id }));
     }
     return(View());
 }
Ejemplo n.º 14
0
        private string ProcessUploadedFile(EmployeeCreateViewModel model)
        {
            string uniqueFileName = null;

            if (model.Photo != null)
            {
                string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");
                var    filenameonly  = Path.GetFileName(model.Photo.FileName); //Fix: Internet Explorer were returning the whole file path including drive letter
                uniqueFileName = Guid.NewGuid().ToString() + "_" + filenameonly;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.Photo.CopyTo(fileStream);
                }
            }

            return(uniqueFileName);
        }
        public async Task <IActionResult> Create(EmployeeCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = await UploadFiles(model);

                Employee employee = new Employee()
                {
                    Name       = model.Name,
                    Email      = model.Email,
                    Department = model.Department,
                    PhotoPath  = uniqueFileName
                };
                _employeeRepository.Add(employee);
                return(RedirectToAction("details", new { Id = employee.Id }));
            }
            return(View());
        }
Ejemplo n.º 16
0
        private string ProcessUploadFiles(EmployeeCreateViewModel model)
        {
            string uniqueFileName = null;

            foreach (var photo in model.Photos)
            {
                string uploadFolder = Path.Combine(hostEnvironment.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName;
                string filePath = Path.Combine(uploadFolder, uniqueFileName);

                using (var filestream = new FileStream(filePath, FileMode.Create))
                {
                    photo.CopyTo(filestream);
                }
            }

            return(uniqueFileName);
        }
        private string processUploadFile(EmployeeCreateViewModel model)
        {
            string uniqueFileName = null;

            if (model.Photo != null)
            {
                string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);

                // using block forces closure of the fileStream object releasing the photo file
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.Photo.CopyTo(fileStream);
                }
            }
            return(uniqueFileName);
        }
        private string ProcessUploadedFile(EmployeeCreateViewModel model)
        {
            string uniqueFileName;
            string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images");

            uniqueFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(model.Photo.FileName);
            string filePath = Path.Combine(uploadsFolder, uniqueFileName);

            // kudvenkat orijinal içerik
            // model.Photo.CopyTo(new FileStream(filePath, FileMode.Create));

            // yorumlarda system.io hatası almamak için önerilen yol
            FileStream fs = new FileStream(filePath, FileMode.Create);

            model.Photo.CopyTo(fs);
            fs.Close();
            return(uniqueFileName);
        }
        public IHttpActionResult Create([FromBody] EmployeeCreateViewModel model)
        {
            if (model.StartDate > DateTime.Now || model.StartDate.Year != DateTime.Now.Year || model.StartDate.Month != DateTime.Now.Month)
            {
                ModelState.AddModelError("StartDate", "Date is not valid!");
            }
            if (!ModelState.IsValid)
            {
                return(Json(new { Success = false, Message = "Not valid data!", Data = ModelState.Keys.Select(k => k.Replace("model.", "")) }));
            }
            var result = _repository.Add(Mapper.Map <Employee>(model));

            if (!result)
            {
                return(Json(new { Success = false, Message = "Something went wrong!" }));
            }
            return(Json(new { Success = true, Message = "Employee successfully created!" }));
        }
Ejemplo n.º 20
0
        public async Task <IActionResult> Create(EmployeeCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = await ProcessUploadFile(model);

                var data = new EmployeeCreateViewModel {
                    Name       = model.Name,
                    Email      = model.Email,
                    Department = model.Department,
                    PhotoPath  = uniqueFileName
                };
                await employeeRepository.Add(data);

                return(RedirectToAction(nameof(Details), new { id = data.Id }));
            }
            return(View());
        }
Ejemplo n.º 21
0
        public IActionResult Create(EmployeeCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = ProcessUploadFile(model);

                Employee newEmployee = new Employee
                {
                    Name         = model.Name,
                    Email        = model.Email,
                    City         = model.City,
                    DepartmentId = model.DepartmentId,
                    PhotoPath    = uniqueFileName
                };
                _employeeRepository.Add(newEmployee);
            }
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 22
0
        public async Task <ActionResult> Create([Bind(Include = "Name,PhoneNumber")] EmployeeCreateViewModel createdEmployee)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    IEmployee employee = (Mapper.Map <Employee>(createdEmployee));
                    await Service.CreateAsync(employee);

                    return(RedirectToAction("Index"));
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes. Try again and if the problem persists see your system administrator.");
            }
            return(View(createdEmployee));
        }
Ejemplo n.º 23
0
        public IActionResult Create(EmployeeCreateViewModel employeeViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(employeeViewModel));
            }

            var employee = _unitOfWork.employeeRepository.Add(new EmployeeViewModel
            {
                Name       = employeeViewModel.Name,
                Email      = employeeViewModel.Email,
                Department = employeeViewModel.Department,
                PhotoPath  = ProccessUploadedFile(employeeViewModel)
            });

            _unitOfWork.SaveChanges();
            return(RedirectToAction("Details", new { Controller = "Employee", id = employee.Id }));
        }
Ejemplo n.º 24
0
        public IActionResult AddEmployee([FromBody] EmployeeCreateViewModel employeeCreate)
        {
            if (employeeCreate == null)
            {
                return(BadRequest());
            }

            var employeeEntity = employeeCreate.Adapt <Employee>();

            if (!this.service.Add(employeeEntity))
            {
                throw new Exception($"Creation of employee failed!!!");
            }

            var employeeToReturn = employeeEntity.Adapt <EmployeeViewModel>();

            return(CreatedAtRoute("GetEmployeeDetails", new { id = employeeToReturn.Id }, employeeToReturn));
        }
        private string ProcessUploadedFile(EmployeeCreateViewModel model)
        {
            string uniqueFileName = null;

            if (HttpContext.Request.Form.Files[0] != null)
            {
                var    file          = HttpContext.Request.Form.Files[0];
                string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    file.CopyTo(fileStream);
                }
            }

            return(uniqueFileName);
        }
Ejemplo n.º 26
0
        public BusinessLayerResult <Employee> AddNewEmployee(EmployeeCreateViewModel employeeCreateViewModel)
        {
            if (employeeCreateViewModel != null)
            {
                if (repositoryEmployee.Find(x => x.EmployeeNo == employeeCreateViewModel.EmployeeNo) != null)
                {
                    // error
                    return(null);
                }
                Employee newEmployee = new Employee()
                {
                    Address     = employeeCreateViewModel.Address,
                    City        = employeeCreateViewModel.City,
                    DateJoined  = DateTime.Now.Date,
                    Designation = employeeCreateViewModel.Designation,
                    DOB         = employeeCreateViewModel.DOB,
                    Email       = employeeCreateViewModel.Email,
                    EmployeeNo  = employeeCreateViewModel.EmployeeNo,
                    FirstName   = employeeCreateViewModel.FirstName,
                    MiddleName  = employeeCreateViewModel.MiddleName,
                    LastName    = employeeCreateViewModel.LastName,
                    //FullName = employeeCreateViewModel.FirstName + " " + employeeCreateViewModel.MiddleName[0] + " " + employeeCreateViewModel.LastName,
                    FullName            = employeeCreateViewModel.FullName,
                    PaymentMethod       = employeeCreateViewModel.PaymentMethod,
                    StudentLoan         = employeeCreateViewModel.StudentLoan,
                    UnionMember         = employeeCreateViewModel.UnionMember,
                    Phone               = employeeCreateViewModel.Phone,
                    Postcode            = employeeCreateViewModel.Postcode,
                    ImageUrl            = employeeCreateViewModel.ImageUrl,
                    Gender              = employeeCreateViewModel.Gender,
                    NationalInsuranceNo = employeeCreateViewModel.NationalInsuranceNo
                };
                var checkIsEmployeeInserted = repositoryEmployee.Insert(newEmployee);
                if (checkIsEmployeeInserted > 0)
                {
                    blResultEmployee.BlResult = newEmployee;
                    //blResultEmployee.ToastrNotificationObject.Title = "Deneme";
                    //blResultEmployee.ToastrNotificationObject.Message = "Employee added succesfully";
                    //blResultEmployee.ToastrNotificationObject.ToastrNotificationType = ToastrNotificationType.Success;
                }
            }

            return(blResultEmployee);
        }
Ejemplo n.º 27
0
        public IActionResult Create(EmployeeCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                string UniqueFileName = null;
                //if (model.Photo != null&&model.Photos.Count > 0)
                //{
                //    foreach (IFormFile Photo in model.Photos)
                //    {
                //        string UploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images");
                //        UniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName;
                //        string filePath = Path.Combine(UploadsFolder, UniqueFileName);
                //        Photo.CopyTo(new FileStream(filePath, FileMode.Create));

                //    }
                //}

                if (model.Photo != null)
                {
                    string UploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images");
                    //UniqueFileName = Path.Combine(Guid.NewGuid().ToString(), "_", model.Photo.FileName);
                    UniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;
                    string filePath = Path.Combine(UploadsFolder, UniqueFileName);
                    model.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
                }

                Employee emp = new Employee
                {
                    FirstName    = model.FirstName,
                    LastName     = model.LastName,
                    DepartmentId = model.DepartmentId,
                    Email        = model.Email,
                    PhotoPath    = UniqueFileName
                };
                //_employeerepository.AddEmployee(employee);
                _employeerepository.AddEmployee(emp);

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.Depts = new SelectList(_departmentrepository.GetDepartments(), "DepartmentId", "DepartmentName", model.DepartmentId);

            return(View(model));
        }
 public IActionResult Create(EmployeeCreateViewModel model)
 {
     if (model != null)
     {
         Employee employee = new Employee()
         {
             Name         = model.Name,
             Email        = model.Email,
             Salary       = model.Salary,
             DepartmentId = model.DepartmentId
         };
         bool isSaved = employeeManager.Add(employee);
         if (isSaved)
         {
             return(RedirectToAction("List"));
         }
     }
     return(View("List"));
 }
Ejemplo n.º 29
0
        public IActionResult Create(EmployeeCreateViewModel model) //* part 53 - step3 - change Employee to EmployeeCreateViewModel
        {
            if (ModelState.IsValid)                                //* Part 42 - step2 model validation - next step in Create.cshtml
            {
                string uniqeFileName = ProccessUplodedFile(model);

                Employee newEmployee = new Employee
                {
                    Name      = model.Name,
                    Email     = model.Email,
                    Role      = model.Role,
                    PhotoPath = uniqeFileName
                };
                _employeeRepositry.Add(newEmployee);
                return(RedirectToAction("details", new { id = newEmployee.Id })); //* Part 44 - need to comment this line
            }

            return(View());
        }
Ejemplo n.º 30
0
        public async Task <IActionResult> Post(EmployeeCreateViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                Employee employee = model.ToEntity();
                var      repo     = this.Storage.GetRepository <IEmployeeRepository>();

                //var imageUrl = await _imageService.UploadImageAsync(model.Image);

                //employee.ImageUrl = imageUrl.ToString();
                repo.Create(employee, GetCurrentUserName());

                this.Storage.Save();

                return(Ok(new { success = true, data = employee }));
            }

            return(BadRequest(new { success = false }));
        }