Пример #1
0
        public async Task <Unit> Handle(UpdateProfileCommand request, CancellationToken cancellationToken)
        {
            var exists = await _profileRepository.CheckProfileExistsAsync(request.ProfileId);

            if (!exists)
            {
                throw new NotFoundException("Profile", request.ProfileId);
            }

            var profileName = await _profileRepository.GetProfileNameAsync(request.ProfileId);

            if (profileName == "Administrator")
            {
                throw new AtlasBusinessException("The Administrator profile cannot be edited");
            }

            var sameNameExists = await _profileRepository.CheckProfileExistsAsync(request.Name, request.ProfileId);

            if (sameNameExists)
            {
                throw new AtlasBusinessException($"A profile with the name {request.Name} already exists.");
            }

            _unitOfWork.BeginTransaction();
            try
            {
                var profile = ConvertToProfile(request);

                await _profileRepository.UpdateProfileAsync(profile);

                _unitOfWork.Commit();

                _logger.LogInformation("Profile with id {Atlas_ProfileId} updated.", request.ProfileId);
            }
            catch
            {
                _unitOfWork.Rollback();
                throw;
            }

            return(Unit.Value);
        }
Пример #2
0
        private static Profile ConvertToProfile(UpdateProfileCommand request)
        {
            var profile = new Profile
            {
                Id          = request.ProfileId,
                Name        = request.Name,
                Description = request.Description
            };

            foreach (var item in request.Privileges)
            {
                profile.ProfilePrivileges.Add(new ProfilePrivilege
                {
                    PrivilegeId = item.PrivilegeId,
                    Permission  = item.Permission
                });
            }

            return(profile);
        }