Exemple #1
0
        public IActionResult Create(EmployeeEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                var employee = new Employee {
                    Name = model.Name, Name = model.Name
                };
                _employeeDataData.Add(Employee);


                return(RedirectToAction("Details", new { id = restaurant.Id })); // TODO :fix redirection to details
            }

            return(View());
        }
        public IActionResult Create(EmployeeViewModel model)
        {
            //The data is coming in correct form or not
            if (ModelState.IsValid)
            {
                string uniqueFileName = null;

                // If the Photo property on the incoming model object is not null, then the user
                // has selected an image to upload.
                if (model.Photo != null)
                {
                    // The image must be uploaded to the images folder in wwwroot
                    // To get the path of the wwwroot folder we are using the inject
                    // HostingEnvironment service provided by ASP.NET Core
                    string uploadsFolder = Path.Combine(_ihosting.WebRootPath, "images");
                    // To make sure the file name is unique we are appending a new
                    // GUID value and and an underscore to the file name
                    //Guid Create a hash format for file name
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                    // Use CopyTo() method provided by IFormFile interface to
                    // copy the file to wwwroot/images folder
                    model.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
                }

                Employee newEmployee = new Employee
                {
                    Name       = model.Name,
                    EmailId    = model.EmailId,
                    Number     = model.Number,
                    Department = model.Department,
                    // Store the file name in PhotoPath property of the employee object
                    // which gets saved to the Employees database table
                    Photo = uniqueFileName
                };

                _employeerepo.Add(newEmployee);
                return(RedirectToAction("AllDetails", new { Id = newEmployee.Id }));
            }

            return(View());
        }
Exemple #3
0
        public async Task <ActionResult <EmployeeModel> > Post(EmployeeModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var employee = _mapper.Map <Employee>(model);
                _repository.Add(employee);
                if (await _repository.SaveChangesAsync() == 1)
                {
                    return(Created($"/api/employee/{employee.Id}", _mapper.Map <EmployeeModel>(employee)));
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError));
            }

            return(BadRequest());
        }