コード例 #1
0
ファイル: FragmentService.cs プロジェクト: bakuzan/realms
        public async Task <FragmentCreateUpdateResponse> UpdateFragment(ClaimsPrincipal claim, FragmentUpdateRequest request)
        {
            var response = new FragmentCreateUpdateResponse();

            var user = await _userService.GetCurrentUser(claim);

            var userId = user == null ? string.Empty : user.Id;

            if (string.IsNullOrEmpty(userId))
            {
                response.ErrorMessages.Add("You are not authorised to perform this action.");
                return(response);
            }

            var fragment = await _fragmentDataService.GetAsync <Fragment>(request.Id, x => x.Realm, x => x.Tags);

            if (fragment == null)
            {
                response.ErrorMessages.Add("Fragment not found.");
                return(response);
            }
            else if (fragment.Realm.IsPrivate && fragment.Realm.ApplicationUserId != userId)
            {
                response.ErrorMessages.Add($"Fragment(code: {fragment.Code}) is part of a private realm.");
                return(response);
            }

            _mapper.Map(request, fragment);

            if (string.IsNullOrEmpty(fragment.Name))
            {
                response.ErrorMessages.Add("Name is required.");
                return(response);
            }

            // Update tags
            var oldTagIds = request.TagList
                            .Where(x => x.Id.HasValue)
                            .Select(x => x.Id.Value);

            var currentTags = fragment.Tags.ToList();

            foreach (var t in currentTags)
            {
                if (!oldTagIds.Contains(t.Id))
                {
                    fragment.Tags.Remove(t);
                }
            }

            await ProcessAndPersistNewTags(fragment.Realm, fragment, request.TagList);

            _fragmentDataService.SetToPersist(fragment);
            await _fragmentDataService.SaveAsync();

            response.Data = _mapper.Map <FragmentItemViewModel>(fragment);
            return(response);
        }
コード例 #2
0
ファイル: FragmentService.cs プロジェクト: bakuzan/realms
        public async Task <FragmentCreateUpdateResponse> CreateFragment(ClaimsPrincipal claim, FragmentCreateRequest request)
        {
            var response = new FragmentCreateUpdateResponse();

            var user = await _userService.GetCurrentUser(claim);

            var userId = user == null ? string.Empty : user.Id;

            if (string.IsNullOrEmpty(userId))
            {
                response.ErrorMessages.Add("You are not authorised to perform this action.");
                return(response);
            }

            if (string.IsNullOrEmpty(request.Name))
            {
                response.ErrorMessages.Add("Name is required.");
                return(response);
            }

            var realm = await _fragmentDataService.GetAsync <Realm>(request.RealmId);

            if (realm == null)
            {
                response.ErrorMessages.Add("The Realm you are attempting to create a page for was not found.");
                return(response);
            }

            var fragment = _mapper.Map <Fragment>(request);

            fragment.Code = await ValidateAndSetFragmentCode(fragment, realm);

            await ProcessAndPersistNewTags(realm, fragment, request.TagList);

            _fragmentDataService.SetToPersist(fragment);
            await _fragmentDataService.SaveAsync();

            response.Data = _mapper.Map <FragmentItemViewModel>(fragment);
            return(response);
        }