Exemple #1
0
        public static async Task <models.Work> Item(Guid?id)
        {
            if (!id.HasValue)
            {
                return(null);
            }
            Tables.dbo.Work item = await dbRead.Work.Item(id.Value);

            if (item == null)
            {
                return(null);
            }
            var output = new models.Work
            {
                Id          = item.Id,
                Cover       = await Image.Item(item.CoverId),
                Title       = item.Title,
                Authors     = item.Authors,
                Href        = item.Href,
                DateCreated = item.DateCreated,
                Display     = item.Display
            };

            return(output);
        }
Exemple #2
0
        /// <summary>
        /// Saves work and child items.
        /// </summary>
        /// <instructions>
        /// Set inputItem.Id to null when creating a new object.
        /// </instructions>
        /// <param name="site">Name of site related to work.</param>
        /// <param name="inputItem">Work object.</param>
        /// <returns>Returns save status and messages. If successful, returns an identifier via ReturnId.</returns>
        public static async Task <Result> Save(string site, models.Work inputItem)
        {
            var messages = new List <string>();

            if (inputItem == null)
            {
                return(new Result(ResultStatus.Failed, "Work cannot be null."));
            }

            Tables.dbo.Site siteItem = await dbRead.Site.Item(site);

            if (siteItem == null)
            {
                return(new Result(ResultStatus.Failed, "No site was found with that name."));
            }

            Tables.dbo.Image convertImage = Image.Convert(inputItem.Cover, siteItem.Id);
            if (convertImage == null)
            {
                return(new Result(ResultStatus.Failed, "Work must have a cover image."));
            }

            Result saveImageResult = await Image.Save(site, inputItem.Cover);

            if (saveImageResult.Status == ResultStatus.Failed)
            {
                return(new Result(saveImageResult.Status, messages));
            }

            Rules.StringRequiredMaxLength(inputItem.Title, "Title", 100, ref messages);
            Rules.StringRequiredMaxLength(inputItem.Authors, "Authors", 255, ref messages);
            if (Rules.StringRequiredMaxLength(inputItem.Href, "Href", 2048, ref messages) == Rules.Passed.Yes)
            {
                Rules.ValidateUrl(inputItem.Href, "Href", ref messages);
            }

            if (messages.Any())
            {
                return(new Result(ResultStatus.Failed, messages));
            }

            Tables.dbo.Work convertedWork = Convert(inputItem, siteItem.Id);
            if (convertedWork == null)
            {
                return(new Result(ResultStatus.Failed, "Could not convert Work model to table."));
            }

            Result output = await dbWrite.Item(site, convertedWork);

            if (output.Status == ResultStatus.PartialSuccess || output.Status == ResultStatus.Succeeded)
            {
                output.ReturnId = inputItem.Id;
            }
            return(output);
        }
Exemple #3
0
        internal static Tables.dbo.Work Convert(models.Work inputItem, Guid siteId)
        {
            if (inputItem == null)
            {
                return(null);
            }
            var output = new Tables.dbo.Work
            {
                Id          = inputItem.Id,
                SiteId      = siteId,
                CoverId     = inputItem.Cover.Id,
                Title       = inputItem.Title,
                Authors     = inputItem.Authors,
                Href        = inputItem.Href,
                DateCreated = inputItem.DateCreated,
                Display     = inputItem.Display
            };

            return(output);
        }
Exemple #4
0
        private static async Task <Tables.dbo.Work> Item(string site, string title, string authors)
        {
            Tables.dbo.Work output = await dbRead.Work.Item(site, title, authors);

            return(output);
        }
        /// <summary>
        /// Saves testimonial and child items.
        /// </summary>
        /// <instructions>
        /// Set inputItem.Id to null when creating a new object.
        /// </instructions>
        /// <param name="site">Name of site related to testimonial.</param>
        /// <param name="inputItem">Testimonial object.</param>
        /// <returns>Returns save status and messages. If successful, returns an identifier via ReturnId.</returns>
        public static async Task <Result> Save(string site, models.Testimonial inputItem)
        {
            var saveStatus = ResultStatus.Undetermined;
            var messages   = new List <string>();

            if (inputItem == null)
            {
                return(new Result(ResultStatus.Failed, "Testimonial cannot be null."));
            }

            Tables.dbo.Site siteItem = await dbRead.Site.Item(site);

            if (siteItem == null)
            {
                return(new Result(ResultStatus.Failed, "No site was found with that name."));
            }

            Tables.dbo.Work convertedWork = Work.Convert(inputItem.Work, siteItem.Id);
            if (convertedWork == null)
            {
                return(new Result(ResultStatus.Failed, "Could not convert work model to table."));
            }

            Tables.dbo.Image convertedPortrait = Image.Convert(inputItem.Portrait, siteItem.Id);
            if (convertedPortrait == null)
            {
                return(new Result(ResultStatus.Failed, "Could not convert portrait model to table."));
            }

            // Disabled until portraits are used for testimonials rather than covers.
            //Result savePortraitResult = await Image.Save(site, inputItem.Portrait);
            //if (savePortraitResult.Status == SaveStatus.Failed)
            //{
            //    return savePortraitResult;
            //}

            inputItem.Portrait.Id = convertedPortrait.Id;

            Rules.StringRequiredMaxLength(inputItem.Name, "Name", 100, ref messages);

            if (Rules.StringRequiredMaxLength(inputItem.EmailAddress, "Email Address", 256, ref messages) ==
                Rules.Passed.Yes)
            {
                Rules.ValidateEmailAddress(inputItem.EmailAddress, "Email Address", ref messages);
            }

            if (messages.Any())
            {
                return(new Result(ResultStatus.Failed, messages));
            }

            var saveItem = new Tables.dbo.Testimonial
            {
                Id              = inputItem.Id,
                SiteId          = siteItem.Id,
                WorkId          = convertedWork.Id,
                PortraitImageId = convertedPortrait.Id,
                Name            = inputItem.Name,
                EmailAddress    = inputItem.EmailAddress,
                Approved        = inputItem.Approved
            };

            Result saveTestimonialResult = await dbWrite.Item(site, saveItem);

            if (saveTestimonialResult.Status == ResultStatus.Failed)
            {
                return(saveTestimonialResult);
            }

            // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
            foreach (Models.ProfessionalTranslator.Net.Localized.Testimonial localizedPage in inputItem.Entries)
            {
                var saveLocalization = new Tables.Localization.Testimonial
                {
                    Id   = saveItem.Id,
                    Html = localizedPage.Html,
                    Lcid = localizedPage.Lcid
                };
                Result localizedResult = await DatabaseOperations.Localization.Write.Testimonial.Item(site, saveLocalization);

                if (localizedResult.Status != ResultStatus.Failed)
                {
                    continue;
                }
                saveStatus = ResultStatus.Failed;
                messages.AddRange(localizedResult.Messages);
                break;
            }

            if (saveStatus == ResultStatus.Undetermined)
            {
                saveStatus = ResultStatus.Succeeded;
            }

            return(new Result(saveStatus, messages, inputItem.Id));
        }