//private Model.Advertisment x; public Advertisment(Model.Advertisment entity) { //this.x = x; this.Id = entity.Id; this.text = entity.text; this.createDate = entity.createDate; this.modifyDate = entity.modifyDate; this.searchresult_id = entity.searchresult_id; this.link = entity.link; this.siteName = entity.siteName; this.isSpecial = entity.isSpecial; this.Phones = entity.AdvertismentPhones.Select(p => new Phone(p)).ToList(); this.Photos = entity.AdvertismentsPhotos.Select(p => new Photo(p)).ToList(); }
public void Save() { var context = new Model.DataModel(); var model = new Model.Advertisment(); model.Id = ID; model.text = Text; model.link = Link; model.siteName = SiteName; model.createDate = CreateDate; model.modifyDate = ModifyDate; model.searchresult_id = SearchResultId; context.Advertisments.InsertOnSubmit(model); context.SubmitChanges(); }
private int SaveAdvertisments(Model.SearchResult searchResult, IList <Server.Entities.Advertisment> advertisments) { Utils.PingServer(); WriteLog("Saving advertisments in DB."); int savedAdvertismentsCount = 0; foreach (var advertisment in advertisments) { try { var textHash = Utils.HashToHex(Utils.CalculateMD5Hash(advertisment.Text), true); Model.Advertisment existsAdvertisment = context.Advertisments .Where(a => a.TextHashValue == textHash) .FirstOrDefault(); if (existsAdvertisment == null) { #region New Advertisment Model.AdvertismentSubSection subSectionObject = null; if (advertisment.SubSectionID != null) { subSectionObject = context.AdvertismentSubSections .SingleOrDefault(s => s.Id == advertisment.SubSectionID.Value); if (subSectionObject == null) { throw new Exception("Can not find Sub Section. ID: " + advertisment.SubSectionID.Value); } } var advertismentEntity = new Model.Advertisment { createDate = Utils.GetUkranianDateTimeNow(), modifyDate = Utils.GetUkranianDateTimeNow(), text = advertisment.Text, AdvertismentSection = searchResult.AdvertismentSection, SearchResult = searchResult, link = advertisment.Link, siteName = advertisment.SiteName, subpurchaseAdvertisment = true, AdvertismentSubSection = subSectionObject, Price = advertisment.Price, Address1 = advertisment.Address1, UndergroundStationID = advertisment.UndergroundStationID, City = advertisment.City }; context.Advertisments.InsertOnSubmit(advertismentEntity); //try //{ // context.SubmitChanges(); //} //catch (Exception e) //{ // WriteLog("Saving advertisment error. " + Environment.NewLine // + "Text: " + advertisment.Text + Environment.NewLine // + "Link: " + advertisment.Link + Environment.NewLine // + "Site: " + advertisment.SiteName + Environment.NewLine // + "Error: " + e.Message + Environment.NewLine // + ". Trace:" + e.StackTrace); // throw; //} //--- add phones foreach (var phone in advertisment.Phones) { if (!string.IsNullOrWhiteSpace(phone)) { //_log.WriteLog(phone.phone); var advertismentPhoneEntity = new Model.AdvertismentPhone { phone = phone, Advertisment = advertismentEntity }; context.AdvertismentPhones.InsertOnSubmit(advertismentPhoneEntity); //advertismentEntity.AdvertismentPhones.Add(advertismentPhoneEntity); } } //---- //--- add photos foreach (var photoUrl in advertisment.PhotoUrls) { if (!string.IsNullOrWhiteSpace(photoUrl)) { var advertismentPhotoEntity = new Model.AdvertismentsPhoto { filename = photoUrl, createDate = Utils.GetUkranianDateTimeNow(), Advertisment = advertismentEntity }; context.AdvertismentsPhotos.InsertOnSubmit(advertismentPhotoEntity); //advertismentEntity.AdvertismentsPhotos.Add(advertismentPhotoEntity); } } //--- try { context.SubmitChanges(); } catch (Exception e) { WriteLog("Saving advertisment's error. " + Environment.NewLine + "Error: " + e.Message + Environment.NewLine + ". Trace:" + e.StackTrace); throw; } savedAdvertismentsCount++; #endregion New Advertisment } else { var advertismentUpdate = new Model.AdvertismentUpdate() { AdvertismentUpdateID = Guid.NewGuid(), AdvertismentID = existsAdvertisment.Id, CreateDate = Utils.GetUkranianDateTimeNow(), SearchResultID = searchResult.Id }; context.AdvertismentUpdates.InsertOnSubmit(advertismentUpdate); //--- update modified datetime for advertisment var modifyAdvertisment = context.Advertisments .FirstOrDefault(a => a.Id == existsAdvertisment.Id); modifyAdvertisment.modifyDate = Utils.GetUkranianDateTimeNow(); context.SubmitChanges(); } } catch (Exception e) { WriteLog("Saving advertisment error. " + Environment.NewLine + "Error: " + e.Message + Environment.NewLine + ". Trace:" + e.StackTrace); } } WriteLog("Finished. Saved advertisments in DB."); return(savedAdvertismentsCount); }
/// <summary> /// Filter subpurchases from Database /// </summary> /// <param name="adversitments"></param> /// <returns>Bad advertisments, with subpurchases phones</returns> private List <Model.Advertisment> DatabaseFilter(IList <Model.Advertisment> adversitments) { var goodAdvList = new List <Model.Advertisment>(); int advertismentsCount = adversitments.Count; int sendSqlCheckCount = 20; List <Tuple <int, string> > advertismentsPhonesToFilter = new List <Tuple <int, string> >(); for (int i = 0; i < advertismentsCount; i++) { Model.Advertisment adversitment = adversitments[i]; if (i == 0 || i % sendSqlCheckCount == 0) { advertismentsPhonesToFilter.Clear(); } foreach (var advertismentPhone in adversitment.AdvertismentPhones .Where(p => !string.IsNullOrEmpty(p.phone))) { advertismentsPhonesToFilter .Add( new Tuple <int, string> ( adversitment.Id, SubPurchases.MakePhoneLikeExpression(advertismentPhone.phone) ) ); } if ((i + 1) % sendSqlCheckCount == 0 || (i + 1) == advertismentsCount) { // The XML XElement xml = new XElement("root", from adv in advertismentsPhonesToFilter select new XElement("a", new XElement("Id", adv.Item1), new XElement("p", adv.Item2))); IEnumerable <Model.SubPurchaseCheckResult> goodAdvertismentsList = context.CheckSubPurchases(xml); goodAdvertismentsList = goodAdvertismentsList.ToList(); foreach (var resultElement in goodAdvertismentsList) { var currentAdvertisment = adversitments.SingleOrDefault(a => a.Id == resultElement.Id); if (currentAdvertisment != null) { if (resultElement.SubPurchaseID == null) { if (!goodAdvertismentsList.Any(ga => ga.Id == resultElement.Id && ga.SubPurchaseID != null)) { goodAdvList.Add(currentAdvertisment); } } else { currentAdvertisment.subpurchaseAdvertisment = true; currentAdvertisment.SubPurchase_Id = resultElement.SubPurchaseID; } } } context.SubmitChanges(); } if (i % sendSqlCheckCount == 0) { WriteLog("Filtering..." + i.ToString() + " advertisments. " + Environment.NewLine + "Good - " + goodAdvList.Count.ToString()); Utils.PingServer(); } } return(goodAdvList); }