Esempio n. 1
0
        public async Task <IActionResult> UpdateOrganization(Guid id, [FromBody] OrganizationPutRp organizationRp)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest(ModelState));
            }

            await _organizationService.UpdateOrganization(id, organizationRp);

            if (_domainManagerService.HasNotFounds())
            {
                return(this.NotFound(_domainManagerService.GetNotFounds()));
            }

            if (_domainManagerService.HasForbidden())
            {
                return(this.Forbidden(_domainManagerService.GetForbidden()));
            }

            if (_domainManagerService.HasConflicts())
            {
                return(this.Conflict(_domainManagerService.GetConflicts()));
            }

            return(this.Ok());
        }
        public async Task UpdateOrganization(Guid organizationId, OrganizationPutRp resource)
        {
            string ownerUserId  = _identityService.GetOwnerId();
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation name {resource.Name} with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to perform updates in this organization.");

                return;
            }

            Organization existingOrganization = user.FindOrganizationByName(resource.Name);

            if (existingOrganization != null && existingOrganization.OrganizationId != organizationId)
            {
                await _domainManagerService.AddConflict($"The organzation name {resource.Name} has already been taken.");

                return;
            }

            user.UpdateOrganization(organizationId, resource.Name, resource.Description, resource.WebSiteUrl);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }