/// <summary>
        /// Dodaje pracownika do bazy danych
        /// </summary>
        /// <param name="newEmployee">Nowy pracownik</param>
        public IHttpActionResult Post(EmployeeAddDto newEmployee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            Employee employee = Mapper.Map <Employee>(newEmployee);
            var      password = RandomString(15);
            var      role     = newEmployee.Role;
            var      user     = new User {
                UserName = GenerateUserName(employee)
            };

            if (context.Users.Any(u => user.UserName == u.UserName))
            {
                return(BadRequest());
            }
            var result = userManager.Create(user, password);

            if (!result.Succeeded)
            {
                return(BadRequest(result.Errors.ToString()));
            }
            userManager.AddToRole(user.Id, role.ToString());
            employee.User = user;
            context.Employees.Add(employee);
            context.SaveChanges();
            using (var eh = new EMailHelper())
            {
                eh.SendEmail(employee.Email, "Instant Delivery - Rejestracja", eh.RegistrationBody(employee, password));
            }
            return(Ok(employee.Id));
        }
 protected override void OnDeactivate(bool close)
 {
     if (close)
     {
         NewEmployee = null;
     }
 }
        public async Task <ActionResult <EmployeeDto> > CreateEmployee([FromBody] EmployeeAddDto employeeAddDto)
        {
            var entity = _mapper.Map <Employee>(employeeAddDto);

            _employeeRepository.AddEmployee(entity);
            await _employeeRepository.SaveAsync();

            var returnDto = _mapper.Map <EmployeeDto>(entity);

            return(CreatedAtRoute(nameof(GetEmployee), new { employeeId = returnDto.Id },
                                  returnDto));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <Employee> > AddEmployee(Guid companyId, EmployeeAddDto employeeAddDto)
        {
            if (!await _companyRepository.CompanyExistsAsync(companyId))
            {
                return(NotFound());
            }
            var employee = _mapper.Map <Employee>(employeeAddDto);

            _companyRepository.AddEmployee(companyId, employee);
            await _companyRepository.SaveAsync();

            var returnDto = _mapper.Map <EmployeeDto>(employee);

            return(CreatedAtRoute(nameof(GetEmployeeAsync), new { companyId, employeeId = returnDto.Id }, returnDto));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <EmployeeDto> > CreateEmployee([FromBody] EmployeeAddDto employee)
        {
            // add this code if without  [ApiController]
            //if (employee == null)
            //{
            //    return BadRequest();
            //}

            var entity = _mapper.Map <Employee>(employee);

            _EmployRepository.AddEmployee(entity);
            await _EmployRepository.SaveAsync();

            var ReturnDto = _mapper.Map <EmployeeDto>(entity);

            return(CreatedAtRoute(nameof(GetEmployee), routeValues: new { employeeId = ReturnDto.Id }, value: ReturnDto));
        }
        public async Task <ActionResult <EmployeeDto> > CreateEmployee(
            [FromRoute] Guid companyId,
            [FromBody] EmployeeAddDto employee)
        {
            if (!await _companyRepository.CompanyExistAsync(companyId))
            {
                return(NotFound());
            }
            var entity = _mapper.Map <Employee>(employee);

            _companyRepository.AddEmployee(companyId, entity);
            await _companyRepository.SaveAsync();

            var returnDto = _mapper.Map <EmployeeDto>(entity);

            return(CreatedAtRoute(nameof(GetEmployee)
                                  , new { employeeId = entity.Id, companyId = entity.CompanyId }, returnDto));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> CreateEmployeeForCompany([FromRoute] Guid companyId,
                                                                   [FromBody] EmployeeAddDto employee)
        //此处的 [FromRoute] 与 [FromBody] 其实不指定也可以,会自动匹配
        {
            if (!await _companyRepository.CompanyExistsAsync(companyId))
            {
                return(NotFound());
            }
            var entity = _mapper.Map <Employee>(employee);

            _companyRepository.AddEmployee(companyId, entity);
            await _companyRepository.SaveAsync();

            var returnDto = _mapper.Map <EmployeeDto>(entity);

            return(CreatedAtAction(nameof(GetEmployeeForCompany),
                                   new { companyId = returnDto.CompanyId, employeeId = returnDto.Id },
                                   returnDto));
        }
Ejemplo n.º 8
0
        CreateEmployeeForCompany(Guid companyId, EmployeeAddDto employee)
        {
            if (!await _companyRepository.CompanyExistsAsync(companyId))
            {
                return(NotFound());
            }

            var entity = _mapper.Map <Employee>(employee);

            _companyRepository.AddEmployee(companyId, entity);
            await _companyRepository.SaveAsync();

            var dtoToReturn = _mapper.Map <EmployeeDto>(entity);

            return(CreatedAtRoute(nameof(GetEmployeeForCompany), new
            {
                companyId = companyId,
                employeeId = dtoToReturn.Id
            }, dtoToReturn));
        }
        public async Task <ActionResult <EmployeeDto> > EmployeeAdd(Guid companyId, EmployeeAddDto employeeAddDto)
        {
            if (!await _service.CompanyExistsAsync(companyId))
            {
                return(NotFound());
            }

            var employee = EmployeeProfile.InitializeAutoMapper().Map <Employee>(employeeAddDto);

            _service.AddEmployee(companyId, employee);

            await _service.SaveAsync();

            var employeeDto = EmployeeProfile.InitializeAutoMapper().Map <EmployeeDto>(employee);

            return(CreatedAtRoute(nameof(GetEmployeeForCompany), new
            {
                companyId,
                employeeId = employeeDto.Id
            }, employeeDto));
        }
Ejemplo n.º 10
0
        CreateEmployeeForCompany(Guid companyId, EmployeeAddDto employee)
        {
            if (!await _companyRepository.CompanyExistsAsync(companyId))
            {
                return(NotFound());
            }
            var entity = _mapper.Map <Employee>(employee);

            _companyRepository.AddEmployee(companyId, entity);
            await _companyRepository.SaveAsync();

            var dtoToReturn = _mapper.Map <EmployeeDto>(entity);

            //使用CreatedAtRoute成功后返回一下201状态码,并在响应头(Response的headers)中带有已添加employee的查看地址
            return(CreatedAtRoute(
                       nameof(GetEmployeeFromCompany), //此参数为 :GetEmployeeFromCompnay方法的[HttpGet]属性条目中已标识了Route的名称
                       new
            {
                companyId = dtoToReturn.CompanyId,
                employeeId = dtoToReturn.ID
            },
                       dtoToReturn));
        }
        public async Task <ActionResult <EmployeeDto> > CreateEmployeeForCompany(Guid companyId, EmployeeAddDto employee)
        {
            if (!await _companyRepository.CompanyExistAsync(companyId))
            {
                return(NotFound());
            }

            var entity = _mapper.Map <Employee>(employee);

            _employeeRepository.AddEmployee(companyId, entity);
            await _employeeRepository.SaveAsync();

            var returnDto = _mapper.Map <EmployeeDto>(entity);

            return(CreatedAtRoute(nameof(GetEmployeesForCompany), routeValues: new
            {
                companyId = companyId,
                employeeId = returnDto.Id
            }, value: returnDto));
        }
Ejemplo n.º 12
0
        public async Task <ActionResult <EmployeeDto> > CreateEmployeeForCompany(Guid companyId, EmployeeAddDto employee)
        {
            if (!await companyRepository.Where(t => t.Id.Equals(companyId)).AnyAsync())
            {
                return(NotFound());
            }

            var entity = mapper.Map <Employee>(employee);
            var result = await employeeRepository.AddEmployeeAsync(companyId, entity);

            var resultDto = mapper.Map <EmployeeDto>(result);

            return(CreatedAtRoute(nameof(GetEmployeeForCompany),
                                  new { companyId, employeeId = resultDto.Id },
                                  resultDto));
        }
Ejemplo n.º 13
0
        public async Task <ActionResult <EmployeeDto> > CreateEmployeeForRegist(string registId, EmployeeAddDto employee)
        {
            if (string.IsNullOrWhiteSpace(registId))
            {
                throw new ArgumentNullException(nameof(registId));
            }

            if (!await _registRepository.RegistExistsAsync(registId))
            {
                return(NotFound());
            }

            Employee entity = _mapper.Map <Employee>(employee);

            entity.EmployeeId = Guid.NewGuid().ToString();
            entity.RegistId   = registId;
            _employeeRepository.Add(entity);
            _employeeRepository.SaveChanges();
            return(NoContent());
        }
        public async Task <ActionResult <EmployeeDto> > CreateEmployeeForCompany(Guid companyId, EmployeeAddDto employee)
        {
            if (!await _companyRepository.CompanyExistsAsync(companyId))
            {
                return(NotFound());
            }

            var entity = _mapper.Map <Employee>(employee);

            _companyRepository.AddEmployee(companyId, entity);
            await _companyRepository.SaveAsync();

            var dtoToReturn = _mapper.Map <EmployeeDto>(entity);

            // return语句的作用是在响应里添加一个location的header,可以通过里面的uri获得到刚刚创建好的资源
            return(CreatedAtRoute(nameof(GetEmployeeForCompany), new
            {
                companyId, // 属性值和属性名相同的,可以把属性名省略
                employeeId = dtoToReturn.Id
            }, dtoToReturn));
        }
Ejemplo n.º 15
0
        public async Task <ActionResult <EmployeeDto> > CreateEmployee(Guid companyId, EmployeeAddDto employee)
        {
            if (await _companyRepository.CompanyExistsAsync(companyId))
            {
                var entity = _mapper.Map <Employee>(employee);
                _companyRepository.AddEmployee(companyId, entity);
                await _companyRepository.SaveAsync();

                var dtoToReturn = _mapper.Map <EmployeeDto>(entity);
                return(CreatedAtRoute(nameof(GetEmployee), new { companyId = companyId, employeeId = entity.Id }, dtoToReturn));
            }
            else
            {
                return(NotFound());
            }
        }
Ejemplo n.º 16
0
        public async Task <ActionResult <EmployeeDto> > CreateEmployeeForCompany(Guid companyId, EmployeeAddDto employee)
        {
            if (!await _companyRepository.CompanyExistsAsync(companyId))
            {
                return(NotFound());
            }

            //使用AutoMapper 需要利用profile 进行配置
            var entity = _mapper.Map <Employee>(employee);

            _companyRepository.AddEmployee(companyId, entity);
            await _companyRepository.SaveAsync();

            var dtoToReturn = _mapper.Map <EmployeeDto>(entity);

            return(CreatedAtRoute(nameof(GetEmployeeForCompany),
                                  new { companyId, employeeId = dtoToReturn.Id }, dtoToReturn));
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Dodaje pracownika do bazy danych.
 /// </summary>
 /// <param name="employee"></param>
 /// <returns></returns>
 public async Task AddEmployee(EmployeeAddDto employee)
 {
     await PostAsJson("", employee);
 }
Ejemplo n.º 18
0
        public async Task <ActionResult <EmployeeDto> > CreateEmployeeForCompany(Guid companyId, EmployeeAddDto employee)
        {
            if (!await repository.CompanyExistsAsync(companyId))
            {
                return(NotFound());
            }
            var entity = mapper.Map <EmployeeAddDto, Employee>(employee);

            repository.AddEmployee(companyId, entity);
            await repository.SaveAsync();

            return(CreatedAtAction(nameof(GetEmployee),
                                   new { companyId = companyId, employeeId = entity.Id }, mapper.Map <Employee, EmployeeDto>(entity)));
        }
Ejemplo n.º 19
0
        public async Task <ActionResult <Emloyee> > CreateEmployeeForCompany(Guid companyId, EmployeeAddDto employeeAddDto)
        {
            if (!await _companyRepository.CompanyExitsAsync(companyId))
            {
                return(NotFound());
            }
            var entity = _mapper.Map <Emloyee>(employeeAddDto);

            _companyRepository.AddEmployee(entity, companyId);

            await _companyRepository.SaveAsync();

            var dto = _mapper.Map <EmployeeDto>(entity);

            return(CreatedAtRoute(nameof(GetEmployeeForCanpany), new { companyid = companyId, empolyid = dto.Id }, dto));//调用getcompany方达,填写路由
        }
        public async Task <ActionResult <EmployeeDto> > CreateEmployeeForCompany(Guid companyId, EmployeeAddDto employeeAddDto)
        {
            var employee = _mapper.Map <Employee>(employeeAddDto);

            _companyRespository.AddEmployee(companyId, employee);
            await _companyRespository.SaveAsync();

            var dtoReturn = _mapper.Map <EmployeeDto>(employee);

            return(CreatedAtRoute(nameof(GetEmployee), new { companyId = companyId, employeeId = employee.Id }, dtoReturn));
        }
Ejemplo n.º 21
0
        public async Task <ActionResult <EmployeeDto> > CreateEmployeeForCompany(Guid companyId, EmployeeAddDto employee)
        {
            if (!await this.companyRespository.CompanyExistsAsync(companyId))
            {
                return(NotFound());
            }
            var entity = this.mapper.Map <Employee>(employee);

            this.companyRespository.AddEmployee(companyId, entity);
            await this.companyRespository.SaveAsync();

            var dto = this.mapper.Map <EmployeeDto>(entity);

            return(CreatedAtRoute(nameof(GetEmployeeForCompany), new { companyId = companyId, employeeId = dto.Id }, dto));
        }
Ejemplo n.º 22
0
        public async Task <ActionResult <EmployeeDto> > CreateEmployeeForCompany(Guid companyId, EmployeeAddDto employee)
        {
            if (!await _companyRepository.CompanyExistAsync(companyId))
            {
                return(NotFound());
            }

            var entity = _mapper.Map <Employee>(employee);

            _companyRepository.AddEmployee(companyId, entity);
            await _companyRepository.SaveAsync();

            var dtoToReturn = _mapper.Map <EmployeeDto>(entity);

            return(CreatedAtRoute(nameof(GetEmployeeForCompany), new
            {
                //companyId = companyId,
                companyId,  //其中因为参数名和变量名一致,所以可以省略不写
                employeeId = dtoToReturn.Id
            }, dtoToReturn));
        }