/// <summary> /// Add a generic supplier to the type reference /// </summary> public void AddGenericSupplier(string parameterName, TypeReference typeReference, bool throwOnError) { if (GenericSupplier == null) { GenericSupplier = new List <TypeReference>(); } // Sanity check if (Class != null && (Class.TypeParameters == null || Class.TypeParameters.Find(o => o.ParameterName == parameterName) == null)) { throw new InvalidOperationException(string.Format("Can't assign type parameter '{0}' on class '{1}' as no such parameter exists", parameterName, Name)); } TypeParameter tp = new TypeParameter(); tp.ParameterName = parameterName; tp.Name = typeReference.name; tp.container = typeReference.container; tp.MemberOf = typeReference.MemberOf; tp.GenericSupplier = typeReference.GenericSupplier; tp.Flavor = typeReference.Flavor; // Turns out that the generic supplier can in fact contain more than one traversal name // for different types that are placed at the point of contact... soo... // this can cause if (GenericSupplier.Exists(o => (o as TypeParameter).ParameterName.Equals(tp.ParameterName)) && throwOnError) { throw new ArgumentException(String.Format("More than one supplier is provided for the same type parameter '{0}' on class '{1}'. This is not permitted!", tp.ParameterName, Name)); } GenericSupplier.Add(tp); }
/// <summary> /// Post an item to the Supplier Catalogue Api /// </summary> /// <param name="supplier">The generic supplier model to post to the API</param> /// <returns>Http response code</returns> public async Task <bool> CreateSupplierAsync(GenericSupplier supplier) { // Generate the JSON string from the generic supplier object var serializer = new DataContractJsonSerializer(typeof(GenericSupplier)); var artifact = new System.IO.MemoryStream(); serializer.WriteObject(artifact, supplier); var content = new StringContent(System.Text.Encoding.UTF8.GetString(artifact.ToArray()), System.Text.Encoding.UTF8, "application/json"); // Post to the Api HttpResponseMessage response = await client.PostAsync("suppliers", content); response.EnsureSuccessStatusCode(); return(response.IsSuccessStatusCode); }
/// <summary> /// Perform an extract and submit of the supplier data as models to the API /// </summary> /// <returns>Boolean value to indicate success / failure</returns> public async Task <string> PerformSupplierObjectExtractAsync() { string someString = string.Empty; Console.Write("Beginning object extract....\n"); string queryString = this.queryProvider.FetchSupplierMainQuery(); List <DbParameter> parameterList = new List <DbParameter> { this.GetParameter("RecordCount", this.extract.RecordCount), }; using (DbDataReader dataReader = this.GetDataReader(queryString, parameterList, CommandType.Text)) { if (dataReader != null && dataReader.HasRows) { while (dataReader.Read()) { int supplierId = (int)dataReader["company_id"]; GenericSupplier generic_supplier = new GenericSupplier { Business = new Business { Name = dataReader["company_name"] == DBNull.Value ? string.Empty : (string)dataReader["company_name"], EmailAddress = string.Empty, Contact = new Person { Title = string.Empty, FamilyName = dataReader["FirstName"] == DBNull.Value ? string.Empty : (string)dataReader["FirstName"], GivenName = dataReader["LastName"] == DBNull.Value ? string.Empty : (string)dataReader["LastName"], EmailAddress = string.Empty, JobTitle = string.Empty, ProfileImage = new Image(), SocialMediaPresence = new List <SocialMediaPresence> { new SocialMediaPresence { Network = "Facebook", Identifier = dataReader["facebook_username"] == DBNull.Value ? string.Empty : (string)dataReader["facebook_username"], }, new SocialMediaPresence { Network = "Twitter", Identifier = dataReader["twitter_username"] == DBNull.Value ? string.Empty : (string)dataReader["twitter_username"], }, new SocialMediaPresence { Network = "Pinterest", Identifier = dataReader["pinterest_username"] == DBNull.Value ? string.Empty : (string)dataReader["pinterest_username"], }, }, }, Address = new Address { Street = string.Empty, Town = string.Empty, County = string.Empty, Postcode = string.Empty, }, PhoneNumber = string.Empty, }, Category = string.Empty, Website = null, Location = new Location { }, Locations = new List <Location>(), StarRating = 0, Reviews = new List <Review>(), Description = string.Empty, Faqs = new List <Faq>(), CoverImage = new Image { Height = 0, Width = 0, Name = string.Empty, Path = dataReader["cover_image"] == DBNull.Value || ((string)dataReader["cover_image"]).Equals(string.Empty) ? null : new UriBuilder((string)dataReader["cover_image"]).Uri, }, ProfileImage = new Image { Height = 0, Width = 0, Name = string.Empty, Path = dataReader["cover_image"] == DBNull.Value || ((string)dataReader["cover_image"]).Equals(string.Empty) ? null : new UriBuilder((string)dataReader["cover_image"]).Uri, }, Images = new List <Image>(), ListingType = string.Empty, SocialMediaPresence = new List <SocialMediaPresence> { new SocialMediaPresence { Network = "Facebook", Identifier = dataReader["facebook_username"] == DBNull.Value ? string.Empty : (string)dataReader["facebook_username"], }, new SocialMediaPresence { Network = "Twitter", Identifier = dataReader["twitter_username"] == DBNull.Value ? string.Empty : (string)dataReader["twitter_username"], }, new SocialMediaPresence { Network = "Pinterest", Identifier = dataReader["pinterest_username"] == DBNull.Value ? string.Empty : (string)dataReader["pinterest_username"], }, }, SpecialOffers = new List <SpecialOffer>(), Treatments = new List <string>(), Designers = new List <Designer>(), Styles = new List <string>(), }; string listingQueryString = this.queryProvider.FetchSupplierListingQuery(); List <DbParameter> listingParameterList = new List <DbParameter> { this.GetParameter("SupplierId", supplierId), }; using (DbDataReader listingDataReader = this.GetDataReader(listingQueryString, listingParameterList, CommandType.Text)) { if (listingDataReader != null && listingDataReader.HasRows) { bool firstRow = true; List <Location> supplierLocations = new List <Location>(); while (listingDataReader.Read()) { Location supplierLocation = new Location(); if (firstRow) { generic_supplier.Description = listingDataReader["company_description"] == DBNull.Value ? string.Empty : (string)listingDataReader["company_description"]; generic_supplier.Business.EmailAddress = listingDataReader["email"] == DBNull.Value ? string.Empty : (string)listingDataReader["email"]; generic_supplier.Business.Address.Street = listingDataReader["address"] == DBNull.Value ? string.Empty : (string)listingDataReader["address"]; generic_supplier.Business.Address.Town = listingDataReader["street"] == DBNull.Value ? string.Empty : (string)listingDataReader["street"]; generic_supplier.Business.Address.County = listingDataReader["county_name"] == DBNull.Value ? string.Empty : (string)listingDataReader["county_name"]; generic_supplier.Business.Address.Postcode = listingDataReader["post_code"] == DBNull.Value ? string.Empty : (string)listingDataReader["post_code"]; generic_supplier.Business.PhoneNumber = listingDataReader["telephone"] == DBNull.Value ? string.Empty : (string)listingDataReader["telephone"]; generic_supplier.Website = listingDataReader["url"] == DBNull.Value || ((string)listingDataReader["url"]).Equals(string.Empty) ? null : new UriBuilder((string)listingDataReader["url"]).Uri; int categoryId = listingDataReader["category_id"] == DBNull.Value ? 0 : (int)listingDataReader["category_id"]; string categoryName; switch (categoryId) { case 4: categoryName = "beauty"; break; case 8: categoryName = "bridalwear"; break; case 9: categoryName = "cake"; break; case 22: categoryName = "florist"; break; case 31: categoryName = "music"; break; case 33: categoryName = "photographer"; break; case 42: categoryName = "videographer"; break; default: categoryName = "Unknown"; break; } generic_supplier.Category = categoryName; firstRow = false; } supplierLocation.Name = supplierLocation.County = listingDataReader["county_name"] == DBNull.Value ? string.Empty : (string)listingDataReader["county_name"]; supplierLocation.GeoCode = new GeoCode { Latitude = 0, Longitude = 0, }; supplierLocations.Add(supplierLocation); } generic_supplier.Locations = supplierLocations; } } string imagesQueryString = this.queryProvider.FetchSupplierImagesQuery(); List <DbParameter> imageParameterList = new List <DbParameter> { this.GetParameter("SupplierId", supplierId), }; using (DbDataReader imagesDataReader = this.GetDataReader(imagesQueryString, imageParameterList, CommandType.Text)) { if (imagesDataReader != null && imagesDataReader.HasRows) { List <Image> supplierImages = new List <Image>(); while (imagesDataReader.Read()) { Image supplierImage = new Image { Name = imagesDataReader["name"] == DBNull.Value ? string.Empty : (string)imagesDataReader["name"], Path = imagesDataReader["path"] == DBNull.Value || ((string)imagesDataReader["path"]).Equals(string.Empty) ? null : new UriBuilder((string)imagesDataReader["path"]).Uri, Height = imagesDataReader["height"] == DBNull.Value ? 0 : (int)imagesDataReader["height"], Width = imagesDataReader["width"] == DBNull.Value ? 0 : (int)imagesDataReader["width"], }; supplierImages.Add(supplierImage); } generic_supplier.Images = supplierImages; } } string faqsQueryString = this.queryProvider.FetchSupplierFaqsQuery(); List <DbParameter> faqsParameterList = new List <DbParameter> { this.GetParameter("SupplierId", supplierId), }; using (DbDataReader faqsDataReader = this.GetDataReader(faqsQueryString, faqsParameterList, CommandType.Text)) { if (faqsDataReader != null && faqsDataReader.HasRows) { List <Faq> supplierFaqs = new List <Faq>(); while (faqsDataReader.Read()) { Faq supplierFaq = new Faq { Question = faqsDataReader["question"] == DBNull.Value ? string.Empty : (string)faqsDataReader["question"], Answer = faqsDataReader["answer"] == DBNull.Value ? string.Empty : (string)faqsDataReader["answer"], }; supplierFaqs.Add(supplierFaq); } generic_supplier.Faqs = supplierFaqs; } } Console.Write("."); // Post the new supplier to the Api await this.apiService.CreateSupplierAsync(generic_supplier); } } } Console.Write("\n"); return(someString); }