public async Task Save()
        {
            models.Testimonial item   = WriteItem();
            Result             result = await repository.Save(Constants.Site, item);

            Assert.IsTrue(result.Status == ResultStatus.Succeeded);
        }
        public static async Task <List <models.Testimonial> > List(string site, bool approved)
        {
            List <Task <models.Testimonial> > taskList = await TaskList(site, approved);

            if (taskList.Count == 0)
            {
                return(new List <models.Testimonial>());
            }
            var output = new List <models.Testimonial>();

            for (var i = 0; 0 < taskList.Count; i++)
            {
                if (i == taskList.Count)
                {
                    break;
                }
                models.Testimonial item = taskList[i].Result;
                if (!output.Contains(item))
                {
                    output.Add(item);
                }
            }

            return(output);
        }
        public async Task Item()
        {
            List <models.Testimonial> list = await SharedList();

            Guid?id = list[0].Id;

            if (!id.HasValue)
            {
                Assert.Fail("List is empty.");
            }

            models.Testimonial item = await repository.Item(id);

            Assert.IsTrue(item != null);
        }
        private static models.Testimonial WriteItem()
        {
            var output = new models.Testimonial
            {
                Id           = Guid.Empty,
                Work         = Work.WriteItem(),
                Portrait     = Image.WriteItem(),
                Name         = "Test",
                EmailAddress = "*****@*****.**",
                DateCreated  = DateTime.Now,
                Approved     = true,
                Entries      = LocalizedList()
            };

            return(output);
        }
        private static async Task <models.Testimonial> Item(Tables.dbo.Testimonial testimonial)
        {
            try
            {
                models.Image portrait = await Image.Item(testimonial.PortraitImageId);

                models.Work work = await Work.Item(testimonial.WorkId);

                if ((portrait == null) && (work?.Cover?.Id != null))
                {
                    portrait = await Image.Item(work.Cover.Id);
                }
                List <Tables.Localization.Testimonial> localizedList = await dbLocalizedRead.List(testimonial.Id);

                var output = new models.Testimonial
                {
                    Id           = testimonial.Id,
                    Work         = work,
                    Portrait     = portrait,
                    Name         = testimonial.Name,
                    EmailAddress = testimonial.EmailAddress,
                    DateCreated  = testimonial.DateCreated,
                    Approved     = testimonial.Approved,
                    Entries      = localizedList.Select(n => new Models.ProfessionalTranslator.Net.Localized.Testimonial
                    {
                        Lcid = n.Lcid,
                        Html = n.Html.Trim()
                    }).ToList()
                };
                return(output);
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
                return(null);
            }
        }
        /// <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));
        }