Ejemplo n.º 1
0
        public async Task <IActionResult> Edit([FromBody] EditCompanyDto companyDto)
        {
            var user = await _userManager.GetByIdentityAsync(this);

            if (user == null)
            {
                return(Unauthorized());
            }

            var selectedCompany = await _userCompanyService.GetCompanyByIdAsync(companyDto.Id);

            var mappedCompany = MapEdit(selectedCompany, companyDto);

            var userCompaniesIds = user
                                   .Companies
                                   .Select(x => x.Id)
                                   .ToList();

            if (userCompaniesIds.Any() && userCompaniesIds.Contains(mappedCompany.Id))
            {
                var updatedCompany = await _userCompanyService.EditCompanyAsync(mappedCompany, user);

                if (updatedCompany == null)
                {
                    return(BadRequest("Can't update company"));
                }

                return(Ok(_mapper.Map <Company, CompanyDto>(updatedCompany)));
            }

            return(StatusCode(403));
        }