Beispiel #1
0
        public IActionResult CreateEmployeeForCompany(Guid companyId, [FromBody] EmployeeForCreateDto employee)
        {
            if (employee == null)
            {
                _logger.LogError("EmployeeForCreateDto Object sent from client is null");
                return(BadRequest("EmployeeForCreateDto Object is null"));
            }
            if (!ModelState.IsValid)
            {
                _logger.LogError("Invalid Model state for the EmployeeForCreateDto object");
                return(UnprocessableEntity(ModelState));
            }
            // Sure ? if company id exists ?
            var company = _repositrory.Company.GetCompanyAsync(companyId, trackChanges: false);

            if (company == null)
            {
                _logger.LogInfo($"Company With d : {companyId} does't exist in the database.");
                return(NotFound());
            }
            // Everything is Okay emp/id
            var employeeEntity = _mapper.Map <Employee>(employee);

            _repositrory.Employee.CreateEmployeeForCompany(companyId, employeeEntity);
            _repositrory.SaveAsync();
            var employeeToReturn = _mapper.Map <EmployeeDto>(employeeEntity);

            // save --?url
            return(CreatedAtRoute("GetEmployeeForCompany", new { companyId, id = employeeEntity.Id }, employeeToReturn));
        }
        public async Task <IActionResult> CreateCompanyCollection([FromBody] IEnumerable <CompanyForCreateDto> companyCollection)
        {
            if (companyCollection == null)
            {
                _logger.LogError("Company Collection Object sent from client is null");
                return(BadRequest("Company Collection Object is null"));
            }
            var companyEntites = _mapper.Map <IEnumerable <Company> >(companyCollection);

            foreach (var company in companyEntites)
            {
                _repositrory.Company.CreateCompany(company);
            }
            await _repositrory.SaveAsync();

            // return result ==> Saved
            var companyCollectiontoReturn = _mapper.Map <IEnumerable <CompanyDto> >(companyEntites);
            var ids = String.Join(",", companyCollectiontoReturn.Select(c => c.Id));

            return(CreatedAtRoute("CompanyCollection", new { ids }, companyCollectiontoReturn));
        }