public int Publish(ArticleNewModel articleNewModel) { if (GetCurrentUser() == null) { throw new ArgumentException(" 当前用户参数发生异常"); } string[] container = articleNewModel.Keywords.Split(' '); Article article = mapper.Map <Article>(articleNewModel); article.Publish(); article.Author = GetCurrentUser(); article.KeyWords = new List <Keyword>(); for (int i = 0; i < container.Length; i++) { Keyword keyword = keywordRepository.GetByName(container[i]); if (keyword == null) { keyword = new Keyword(); keyword.Name = container[i]; }//else nothing keyword.Used++; article.KeyWords.Add(keyword); } articleRepository.Save(article); return(article.Id); }
public int Publish(NewModel model) { Article article = new Article { Title = model.Title, Body = model.Body, Author = GetCurrentUser(false), Digest = model.Digest }; article.Belong = serieRepository.Find(model.Belong.Id); IList <string> strKeywords = keywordRepository.splitKeywords(model.Keywords); IList <Keyword> objKeywords = new List <Keyword>(); Keyword keyword; foreach (var item in strKeywords) { if (keywordRepository.GetByName(item) == null) { Keyword newKeyword = new Keyword { Name = item }; keywordRepository.Save(newKeyword); keyword = newKeyword; } else { keyword = keywordRepository.GetByName(item); } objKeywords.Add(keyword); } article.Keywords = objKeywords; articleRepository.Save(article); return(article.Id); }
protected async void DownloadXml() { var myXmlDocument = new XmlDocument(); myXmlDocument.Load("https://raw.githubusercontent.com/BSData/wh40k/master/Tyranids.cat"); var catalogue = myXmlDocument.GetElementsByTagName("catalogue")[0]; var army = await ArmyRepository.GetByBattleScribeId(catalogue.Attributes["id"].Value); if (army == null) { army = new Data.Models.Army { BattleScribeId = catalogue.Attributes["id"].Value, BattleScribeRevision = catalogue.Attributes["revision"].Value, Name = catalogue.Attributes["name"].Value }; await ArmyRepository.Create(army); } var categoryNodeList = myXmlDocument.GetElementsByTagName("categoryEntry"); foreach (XmlNode categoryNode in categoryNodeList) { var category = await CategoryRepository.GetByBattleScribeId(categoryNode.Attributes["id"].Value); var create = false; if (category == null) { create = true; category = new Category(); } category.Name = HttpUtility.HtmlDecode(categoryNode.Attributes["name"].Value); category.BattleScribeId = categoryNode.Attributes["id"].Value; if (create) { await CategoryRepository.Create(category); } else { await CategoryRepository.Update(category); } } var unitList = myXmlDocument.GetElementsByTagName("selectionEntry"); foreach (XmlNode unitNode in unitList) { if (!unitNode.Attributes["type"].Any() || unitNode.Attributes["type"].Value != "unit") { continue; } var create = false; var unit = await UnitRepository.GetByBattleScribeId(unitNode.Attributes["id"].Value); if (unit == null) { create = true; unit = new Data.Models.Unit(); } unit.Name = unitNode.Attributes["name"].Value; unit.BattleScribeId = unitNode.Attributes["id"].Value; unit.Army = army; if (create) { await UnitRepository.Create(unit); } else { await UnitRepository.Update(unit); } var categoryLinkNodes = unitNode.ChildNodes.Cast <XmlNode>() .First(nc => nc.LocalName == "categoryLinks").Cast <XmlNode>() .Where(ncc => ncc.LocalName == "categoryLink"); foreach (var categoryLinkNode in categoryLinkNodes) { var unitCateg = await UnitCategoryRepository.GetbyBattleScribeIds( categoryLinkNode.Attributes["targetId"].Value, unitNode.Attributes["id"].Value); if (unitCateg != null) { continue; } Console.WriteLine("CREATE CATEG"); var category = await CategoryRepository.GetByBattleScribeId(categoryLinkNode.Attributes["targetId"].Value); if (category == null) { continue; } unitCateg = new UnitCategory { Unit = unit, Category = category }; await UnitCategoryRepository.Create(unitCateg); } var keywordNodes = unitNode.ChildNodes.Cast <XmlNode>() .FirstOrDefault(nc => nc.LocalName == "profiles")?.Cast <XmlNode>() .FirstOrDefault(ncc => ncc.LocalName == "profile" && ncc.Attributes["typeName"] != null && ncc.Attributes["typeName"].Value == "Keywords")?.Cast <XmlNode>() .FirstOrDefault(atr => atr.LocalName == "characteristics")?.Cast <XmlNode>() .Where(atr => atr.LocalName == "characteristic"); if (keywordNodes == null) { continue; } foreach (var keywordNode in keywordNodes) { var keyword = await KeywordRepository.GetByName(HttpUtility.HtmlDecode(keywordNode.InnerText)); if (keyword == null) { keyword = new Keyword { Name = HttpUtility.HtmlDecode(keywordNode.InnerText) }; await KeywordRepository.Create(keyword); } var unitKeyword = await UnitKeywordRepository.GetbyIds(keyword.Id, unit.Id); if (unitKeyword != null) { continue; } unitKeyword = new UnitKeyword { Unit = unit, Keyword = keyword }; await UnitKeywordRepository.Create(unitKeyword); } } }