Exemplo n.º 1
0
        /// <summary>
        /// Saves inquiry and client.
        /// </summary>
        /// <instructions>
        /// Set inputItem.Id to null when creating a new object.
        /// </instructions>
        /// <param name="site">Name of site for image.</param>
        /// <param name="inputItem">Inquiry object.</param>
        /// /// <param name="clientItem">Client to be associated with this inquiry.</param>
        /// <returns>Returns save status and messages. If successful, returns an identifier via ReturnId.</returns>
        public static async Task <Result> Save(string site, Models.ProfessionalTranslator.Net.Log.Inquiry inputItem, models.Client clientItem)
        {
            var saveStatus = ResultStatus.Undetermined;
            var messages   = new List <string>();

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

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

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

            Rules.StringRequiredMaxLength(inputItem.TranslationType, "Translation Type", 25, ref messages);
            Rules.StringRequiredMaxLength(inputItem.TranslationDirection, "Translation Direction", 25, ref messages);
            Rules.StringRequiredMaxLength(inputItem.SubjectMatter, "SubjectMatter", 50, ref messages);
            Rules.MinIntValue(inputItem.WordCount, "Word Count", 1, ref messages);

            Rules.StringRequired(inputItem.Message, "Message", ref messages);

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

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

            Guid returnId = convertedInquiry.Id;

            Result saveInquiryResult = await dbWrite.Item(site, convertedInquiry);

            if (saveInquiryResult.Status == ResultStatus.PartialSuccess || saveInquiryResult.Status == ResultStatus.Succeeded)
            {
                saveInquiryResult.ReturnId = returnId;
            }
            else
            {
                saveStatus = saveInquiryResult.Status;
            }

            if (saveStatus == ResultStatus.Undetermined)
            {
                foreach (Models.ProfessionalTranslator.Net.Upload.Client uploads in clientItem.Uploads)
                {
                    Result uploadResult = await ClientInquiry.Item(uploads.Id, returnId);

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

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

            return(new Result(saveStatus, messages, returnId));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Saves client and client uploads.
        /// </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">Client 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.Client inputItem)
        {
            var saveStatus = ResultStatus.Undetermined;
            var messages   = new List <string>();

            if (inputItem == null)
            {
                return(new Result(ResultStatus.Failed, "Client 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."));
            }

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

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

            foreach (Models.ProfessionalTranslator.Net.Upload.Client uploads in inputItem.Uploads)
            {
                Rules.StringRequiredMaxLength(uploads.GeneratedFilename, "Generated filename", 45, ref messages);
                Rules.StringRequiredMaxLength(uploads.OriginalFilename, "Original filename", 256, ref messages);
            }

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

            var saveItem = new Tables.dbo.Client
            {
                Id           = inputItem.Id,
                SiteId       = siteItem.Id,
                Name         = inputItem.Name,
                EmailAddress = inputItem.EmailAddress
            };

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

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

            // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
            foreach (Models.ProfessionalTranslator.Net.Upload.Client uploads in inputItem.Uploads)
            {
                var saveUpload = new Tables.Upload.Client
                {
                    Id                = uploads.Id,
                    ClientId          = saveItem.Id,
                    GeneratedFilename = uploads.GeneratedFilename,
                    OriginalFilename  = uploads.OriginalFilename
                };
                Result uploadResult = await DatabaseOperations.Upload.Write.Client.Item(saveUpload);

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

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

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