public static NewsArticle ToNewsArticleEntity( NewsArticleModel newsArticleModel, DbUsersRepository usersRepository, IRepository<Comment> commentsRepository, IRepository<NewsArticle> newsArticlesRepository) { NewsArticle newsArticleEntity = new NewsArticle() { ID = newsArticleModel.ID, Author = usersRepository.GetByNickname(newsArticleModel.Author), Content = newsArticleModel.Content, Date = newsArticleModel.Date, ImageUrl = newsArticleModel.ImageUrl, Rating = newsArticleModel.Rating, Title = newsArticleModel.Title }; foreach (CommentModel comment in newsArticleModel.Comments) { newsArticleEntity.Comments.Add( Extensions.CreateOrLoadComment( comment, commentsRepository, usersRepository, newsArticlesRepository)); } return newsArticleEntity; }
public static NewsArticleDetails ToNewsArticleDetails(NewsArticle newsArticle) { NewsArticleDetails newsArticleDetails = new NewsArticleDetails() { ID = newsArticle.ID, Author = newsArticle.Author.Nickname, Date = newsArticle.Date, Rating = newsArticle.Rating, Title = newsArticle.Title }; return newsArticleDetails; }
public int Add(NewsArticle model, string username) { var currentUser = this.users.All().FirstOrDefault(u => u.UserName == username); if (currentUser == null) { throw new ArgumentException("Current user cannot be found!"); } model.Author = currentUser; this.newsArticles.Add(model); this.newsArticles.SaveChanges(); return model.Id; }
public static NewsArticleModel ToNewsArticleModel(NewsArticle newsArticle) { NewsArticleModel newsArticleModel = new NewsArticleModel() { ID = newsArticle.ID, Title = newsArticle.Title, Content = newsArticle.Content, Rating = newsArticle.Rating, ImageUrl = newsArticle.ImageUrl, Date = newsArticle.Date, Author = newsArticle.Author.Nickname }; foreach (Comment comment in newsArticle.Comments) { newsArticleModel.Comments.Add(CommentsMapper.ToCommentModel(comment)); } return newsArticleModel; }
//public int Add(NewsArticle model, string username) //{ // var currentUser = this.users.All().FirstOrDefault(u => u.UserName == username); // if (currentUser == null) // { // throw new ArgumentException("Current user cannot be found!"); // } // model.Author = currentUser; // this.newsArticles.Add(model); // this.newsArticles.SaveChanges(); // return model.Id; //} public int Add(string name, string content, int categoryId, ICollection<Image> images, string username) { var currentUser = this.users.All().FirstOrDefault(u => u.UserName == username); if (currentUser == null) { throw new ArgumentException("Current user cannot be found!"); } var articleToAdd = new NewsArticle() { Name = name, Content = content, CategoryId = categoryId, Images = images, Author = currentUser, CreatedOn = DateTime.Now }; this.newsArticles.Add(articleToAdd); this.newsArticles.SaveChanges(); return articleToAdd.Id; }
private static void AddImages(NewsArticleModel newsArticle, NewsArticle newsArticleEntity) { foreach (string imageUrl in newsArticle.ImagesUrls) { Entry uploadFileEntry = DropboxUtilities.UploadImage(imageUrl, dropbox, "New_Folder"); DropboxLink imageLink = dropbox.GetShareableLinkAsync(uploadFileEntry.Path).Result; newsArticleEntity.ImagesUrls.Add(imageLink.Url); } }