Example #1
0
        /// <summary>
        /// Replaces the author or if it doesn't exist, creates it.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <c>PutAuthorResponse</c> with a author id.
        /// </returns>
        public PutAuthorResponse Put(PutAuthorRequest request)
        {
            var authors = repository.AsQueryable <Module.Blog.Models.Author>()
                          .Where(l => l.Id == request.Id)
                          .ToList();

            var authorToSave = authors.FirstOrDefault(l => l.Id == request.Id);

            var createAuthor = authorToSave == null;

            if (createAuthor)
            {
                authorToSave = new Module.Blog.Models.Author
                {
                    Id = request.Id.GetValueOrDefault()
                };
            }
            else if (request.Data.Version > 0)
            {
                authorToSave.Version = request.Data.Version;
            }

            unitOfWork.BeginTransaction();

            authorToSave.Name        = request.Data.Name;
            authorToSave.Description = request.Data.Description;
            authorToSave.Image       = request.Data.ImageId.HasValue ? repository.AsProxy <MediaImage>(request.Data.ImageId.Value) : null;

            repository.Save(authorToSave);

            unitOfWork.Commit();

            // Fire events.
            if (createAuthor)
            {
                Events.BlogEvents.Instance.OnAuthorCreated(authorToSave);
            }
            else
            {
                Events.BlogEvents.Instance.OnAuthorUpdated(authorToSave);
            }

            return(new PutAuthorResponse
            {
                Data = authorToSave.Id
            });
        }
Example #2
0
        /// <summary>
        /// Replaces the author or if it doesn't exist, creates it.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <c>PutAuthorResponse</c> with a author id.
        /// </returns>
        public PutAuthorResponse Put(PutAuthorRequest request)
        {
            var authors = repository.AsQueryable<Module.Blog.Models.Author>()
                .Where(l => l.Id == request.Id)
                .ToList();

            var authorToSave = authors.FirstOrDefault(l => l.Id == request.Id);

            var createAuthor = authorToSave == null;
            if (createAuthor)
            {
                authorToSave = new Module.Blog.Models.Author
                {
                    Id = request.Id.GetValueOrDefault()
                };
            }
            else if (request.Data.Version > 0)
            {
                authorToSave.Version = request.Data.Version;
            }

            unitOfWork.BeginTransaction();

            authorToSave.Name = request.Data.Name;
            authorToSave.Image = request.Data.ImageId.HasValue ? repository.AsProxy<MediaImage>(request.Data.ImageId.Value) : null;

            repository.Save(authorToSave);

            unitOfWork.Commit();

            // Fire events.
            if (createAuthor)
            {
                Events.BlogEvents.Instance.OnAuthorCreated(authorToSave);
            }
            else
            {
                Events.BlogEvents.Instance.OnAuthorUpdated(authorToSave);
            }

            return new PutAuthorResponse
            {
                Data = authorToSave.Id
            };
        }