Exemplo n.º 1
0
        /// <summary>
        /// Replaces the tag or if it doesn't exist, creates it.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <c>PutTagResponse</c> with a tag id.
        /// </returns>
        public PutTagResponse Put(PutTagRequest request)
        {
            var tagName            = request.Data.Name.Trim();
            var tagsByIdFuture     = repository.AsQueryable <Module.Root.Models.Tag>().Where(tag1 => tag1.Id == request.Id).ToFuture();
            var tagsByIdNameFuture = repository.AsQueryable <Module.Root.Models.Tag>().Where(tag1 => tag1.Name == tagName).ToFuture();
            var tagById            = tagsByIdFuture.FirstOrDefault();
            var tagByName          = tagsByIdNameFuture.FirstOrDefault();

            // Validate.
            if (tagById != null && tagByName != null && tagById.Id != tagByName.Id)
            {
                var logMessage = string.Format("Failed to rename tag. Tag with the same name already exists. Name: '{0}'.", tagName);
                throw new CmsApiValidationException(logMessage);
            }

            if (tagById == null && tagByName != null)
            {
                var logMessage = string.Format("Failed to create a tag. Tag with the same name already exists. Name: '{0}'.", tagName);
                throw new CmsApiValidationException(logMessage);
            }

            // Create or update.
            var createTag = tagById == null;

            if (createTag)
            {
                tagById = new Module.Root.Models.Tag {
                    Id = request.Id.GetValueOrDefault()
                };
            }
            else if (request.Data.Version > 0)
            {
                tagById.Version = request.Data.Version;
            }

            tagById.Name = tagName;

            repository.Save(tagById);
            unitOfWork.Commit();

            // Fire events.
            if (createTag)
            {
                Events.RootEvents.Instance.OnTagCreated(tagById);
            }
            else
            {
                Events.RootEvents.Instance.OnTagUpdated(tagById);
            }

            return(new PutTagResponse
            {
                Data = tagById.Id,
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Replaces the tag or if it doesn't exist, creates it.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <c>PutTagResponse</c> with a tag id.
        /// </returns>
        public PutTagResponse Put(PutTagRequest request)
        {
            var tagName = request.Data.Name.Trim();
            var tagsByIdFuture = repository.AsQueryable<Module.Root.Models.Tag>().Where(tag1 => tag1.Id == request.Id).ToFuture();
            var tagsByIdNameFuture = repository.AsQueryable<Module.Root.Models.Tag>().Where(tag1 => tag1.Name == tagName).ToFuture();
            var tagById = tagsByIdFuture.FirstOrDefault();
            var tagByName = tagsByIdNameFuture.FirstOrDefault();

            // Validate.
            if (tagById != null && tagByName != null && tagById.Id != tagByName.Id)
            {
                var logMessage = string.Format("Failed to rename tag. Tag with the same name already exists. Name: '{0}'.", tagName);
                throw new CmsApiValidationException(logMessage);
            }

            if (tagById == null && tagByName != null)
            {
                var logMessage = string.Format("Failed to create a tag. Tag with the same name already exists. Name: '{0}'.", tagName);
                throw new CmsApiValidationException(logMessage);
            }

            // Create or update.
            var createTag = tagById == null;
            if (createTag)
            {
                tagById = new Module.Root.Models.Tag { Id = request.Id.GetValueOrDefault() };
            }
            else if (request.Data.Version > 0)
            {
                tagById.Version = request.Data.Version;
            }

            tagById.Name = tagName;

            repository.Save(tagById);
            unitOfWork.Commit();

            // Fire events.
            if (createTag)
            {
                Events.RootEvents.Instance.OnTagCreated(tagById);
            }
            else
            {
                Events.RootEvents.Instance.OnTagUpdated(tagById);
            }

            return new PutTagResponse
            {
                Data = tagById.Id,
            };
        }