public ActionResult Create( Article article) { try { _articles.Insert( ParseTags( article)); return RedirectToAction("Index"); } catch( Exception exception) { ViewBag.ErrorMessage = exception.Message; var viewModel = new ArticleEditViewModel() { Article = article, Authors = _authors.RetrieveAll(), Tags = _tags.RetrieveAll() }; return View( viewModel); } }
public Tag Map( Tag tag, Article article, Author author) { // Terminating call. Since we can return null from this function // we need to be ready for PetaPoco to callback later with null // parameters if( tag == null) return current; // Is this the same tag as the current one we're processing if( current != null && current.Id == tag.Id) { article.Author = author; // Yes, just add this article to the current tag's collection of articles if( article != null) current.Articles.Add( article); // Return null to indicate we're not done with this tag yet return null; } // This is a different tag to the current one, or this is the // first time through and we don't have an tag yet // Save the current tag var prev = current; // Setup the new current tag current = tag; if( article != null && article.Id != int.MinValue) { article.Author = author; current.Articles.Add( article); } return prev; }
private Article ParseTags( Article article) { var editedTags = Request["Tags"]; if( ! string.IsNullOrEmpty( editedTags)) { var tags = Regex.Split( editedTags, "\r\n"); if( tags != null && tags.Length > 0) { foreach( var tag in tags) { article.Tags.Add( new Tag( tag)); } } } return article; }