public async Task <ArticleServiceModel> GetArticleById(string id) { ArticleServiceModel article = await this.context.Articles.To <ArticleServiceModel>() .SingleOrDefaultAsync(a => a.Id == id); return(article); }
public async Task <bool> CreateArticle(ArticleServiceModel serviceModel) { Publication publication = new Publication { CreatedOn = DateTime.UtcNow, Title = serviceModel.PublicationTitle, UserId = serviceModel.UserId, }; await this.context.Publications.AddAsync(publication); Article article = new Article { Description = serviceModel.Description, CategoryId = serviceModel.CategoryId, PublicationId = publication.Id, }; await this.context.Articles.AddAsync(article); int result = await this.context.SaveChangesAsync(); if (result > 0) { Category categoryFromDB = await this.context.Categories.SingleOrDefaultAsync(a => a.Id == serviceModel.CategoryId); categoryFromDB.ArticlesCount++; this.context.Categories.Update(categoryFromDB); await this.context.SaveChangesAsync(); } return(result > 0); }
public IActionResult Details(string id) { //to get the article ArticleServiceModel articleServiceModel = this.articlesService.GetArticleDetails(id); //to pass the view model return(this.View(articleServiceModel)); }
public async Task <int> CreateAsync(ArticleServiceModel articleServiceModel) { var article = AutoMapperConfig.MapperInstance.Map <Article>(articleServiceModel); await this.articleRepository.AddAsync(article); await this.articleRepository.SaveChangesAsync(); return(article.Id); }
public ActionResult Create(ArticleServiceModel model) { if (!ModelState.IsValid) { return(View(model)); } articleService.Create(model); return(RedirectToAction("Index", "Home")); }
public async Task <IActionResult> Add(ArticleAddInputModel articleAddInputModel) { if (!this.ModelState.IsValid) { return(this.View(articleAddInputModel)); } List <string> urlsPicture = new List <string>(); foreach (IFormFile picture in articleAddInputModel.Pictures) { string pictureUrl = null; if (picture != null) { pictureUrl = await this.cloudinaryService.UploadPictureAsync(picture, articleAddInputModel.Title); } urlsPicture.Add(pictureUrl); } ArticleServiceModel articleServiceModel = new ArticleServiceModel { Title = articleAddInputModel.Title, Author = articleAddInputModel.Author, Content = articleAddInputModel.Content, Date = articleAddInputModel.Date, Category = articleAddInputModel.Category, ArticlePictures = new List <ArticlePictureServiceModel> { new ArticlePictureServiceModel { ImageUrl = urlsPicture[0] }, new ArticlePictureServiceModel { ImageUrl = urlsPicture[1] }, new ArticlePictureServiceModel { ImageUrl = urlsPicture[2] }, new ArticlePictureServiceModel { ImageUrl = urlsPicture[3] }, new ArticlePictureServiceModel { ImageUrl = urlsPicture[4] } } }; await this.articlesService.Add(articleServiceModel); return(this.Redirect("/Articles/All")); }
public async Task <ArticleServiceModel> CreateArticleAsync(ArticleServiceModel articleServiceModel, string authorId) { Article article = articleServiceModel.To <Article>(); article.AuthorId = authorId; article.CreatedOn = DateTime.UtcNow; await this.context.Articles.AddAsync(article); await this.context.SaveChangesAsync(); return(article.To <ArticleServiceModel>()); }
public async Task GetById_WithNonExistentId_ShouldReturnNull() { string errorMessagePrefix = "ArticlesService Method GetArticleById() does not work properly."; var context = CDGBulgariaInmemoryFactory.InitializeContext(); await SeedData(context); this.articlesService = new ArticlesService(context); ArticleServiceModel actualResult = await this.articlesService.GetArticleById("fake1"); Assert.True(actualResult == null, errorMessagePrefix); }
public void SaveArticle(ArticleServiceModel model) { var article = new Article() { Id = model.Id, ArticlePrice = model.ArticlePrice, BuyerUserId = model.BuyerUserId, IsSold = model.IsSold, Name_of_article = model.Name_of_article, SoldDate = model.SoldDate }; unitOfWork.Articles.Add(article); unitOfWork.SaveChanges(); }
public async Task EditShouldThrowExceptionsSuccessfully(int articleId) { this.SetupSqlite(); await this.SeedDatabase(); using var context = new ApplicationDbContext(this.ContextOptions); var repository = new EfDeletableEntityRepository <Article>(context); var articleService = new ArticleService(repository); var newArticleObj = new ArticleServiceModel { Id = articleId, Content = "Test", ImageUrl = "Test", Title = "Test" }; await Assert.ThrowsAsync <ArgumentNullException>(() => articleService.Edit(newArticleObj)); }
public async void EditArticleAsync_ShouldThrowArgumentNullExceptionWithInvalidArticle() { var options = new DbContextOptionsBuilder <TechAndToolsDbContext>() .UseInMemoryDatabase(databaseName: "EditArticleAsync_ShouldThrowArgumentNullExceptionWithInvalidArticleId") .Options; TechAndToolsDbContext context = new TechAndToolsDbContext(options); IArticleService articleService = new ArticleService(context); await this.SeedData(context); ArticleServiceModel invalidArticle = new ArticleServiceModel(); await Assert.ThrowsAsync <ArgumentNullException>(() => articleService.EditArticleAsync(invalidArticle)); }
public async Task Delete_WithCorrectData_ShouldPassSuccesfully() { string errorMessagePrefix = "ArticlesService Method Delete() does not work properly."; var context = CDGBulgariaInmemoryFactory.InitializeContext(); await SeedData(context); this.articlesService = new ArticlesService(context); ArticleServiceModel expectedData = context.Articles.First().To <ArticleServiceModel>(); bool actualData = await this.articlesService.Delete(expectedData.Id); Assert.True(actualData, errorMessagePrefix); }
public async Task <IActionResult> Create(ArticleCreateInputModel model) { if (!this.ModelState.IsValid) { return(this.View(model)); } ArticleServiceModel serviceModel = model.To <ArticleServiceModel>(); serviceModel.AuthorId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value; await this.articlesService.CreateArticle(serviceModel); return(this.Redirect("/Articles/All")); }
public int Create(ArticleServiceModel model) { var userID = HttpContext.Current.User.Identity.GetUserId(); var entity = new Article { SubmitDate = DateTime.Now, Title = model.Title, Body = model.Body, ApplicationUserId = userID }; db.Articles.Add(entity); db.SaveChanges(); return(entity.Id); }
public ArticleServiceModel GetArticleDetails(string id) { ArticleServiceModel articleServiceModel = this.context.Articles .Include(x => x.ArticlePictures) .Where(articleDb => articleDb.Id == id) .Select(x => new ArticleServiceModel { Id = x.Id, Title = x.Title, Author = x.Author, Category = (ArticleCategorySetviceModel)x.Category, Content = x.Content, Date = x.Date, ArticlePictures = new List <ArticlePictureServiceModel> { new ArticlePictureServiceModel { Id = x.ArticlePictures[0].Id, ImageUrl = x.ArticlePictures[0].ImageUrl }, new ArticlePictureServiceModel { Id = x.ArticlePictures[1].Id, ImageUrl = x.ArticlePictures[1].ImageUrl }, new ArticlePictureServiceModel { Id = x.ArticlePictures[2].Id, ImageUrl = x.ArticlePictures[2].ImageUrl }, new ArticlePictureServiceModel { Id = x.ArticlePictures[3].Id, ImageUrl = x.ArticlePictures[3].ImageUrl }, new ArticlePictureServiceModel { Id = x.ArticlePictures[4].Id, ImageUrl = x.ArticlePictures[4].ImageUrl } } }).FirstOrDefault(); return(articleServiceModel); }
public async Task <int> Edit(ArticleServiceModel articleServiceModel) { var dbArticle = await this.articleRepository.All().SingleOrDefaultAsync(a => a.Id == articleServiceModel.Id); if (dbArticle == null) { throw new ArgumentNullException(nameof(dbArticle)); } dbArticle.Title = articleServiceModel.Title; dbArticle.Content = articleServiceModel.Content; dbArticle.ImageUrl = articleServiceModel.ImageUrl; this.articleRepository.Update(dbArticle); int result = await this.articleRepository.SaveChangesAsync(); return(result); }
public async Task <ArticleServiceModel> EditArticleAsync(ArticleServiceModel articleServiceModel) { Article articleFromDb = this.context.Articles .Find(articleServiceModel.Id); if (articleFromDb == null) { throw new ArgumentNullException(nameof(articleFromDb)); } articleFromDb.Content = articleServiceModel.Content; articleFromDb.Title = articleServiceModel.Title; this.context.Articles.Update(articleFromDb); await this.context.SaveChangesAsync(); return(articleFromDb.To <ArticleServiceModel>()); }
public async Task <IActionResult> Create(CreateArticleInputModel inputModel) { if (!this.ModelState.IsValid) { return(this.View(inputModel)); } ArticleServiceModel articleServiceModel = inputModel.To <ArticleServiceModel>(); string pictureUrl = await this.cloudinaryService .UploadPictureAsync(inputModel.ImageFormFile, inputModel.Title); ArticleServiceModel productFromDb = await this.articleService.CreateArticleAsync(articleServiceModel, this.User.FindFirstValue(ClaimTypes.NameIdentifier)); await this.imageService.CreateWithArticleAsync(pictureUrl, productFromDb.Id); return(this.RedirectToAction("All")); }
public async Task <bool> Edit(string id, ArticleServiceModel articleServiceModel) { Article articleFromDb = await this.context.Articles.SingleOrDefaultAsync(a => a.Id == id); if (articleFromDb == null) { throw new ArgumentNullException(nameof(articleFromDb)); } articleFromDb.Title = articleServiceModel.Title; articleFromDb.Content = articleServiceModel.Content; this.context.Articles.Update(articleFromDb); int result = await this.context.SaveChangesAsync(); return(result > 0); }
public async Task <bool> CreateArticle(ArticleServiceModel serviceModel) { CDGUser author = await this.context.Users.SingleOrDefaultAsync(u => u.Id == serviceModel.AuthorId); Article article = new Article { Title = serviceModel.Title, Content = serviceModel.Content, CreatedOn = DateTime.UtcNow, }; article.Author = author; await this.context.Articles.AddAsync(article); int result = await this.context.SaveChangesAsync(); return(result > 0); }
public async Task <IActionResult> Edit(ArticleServiceModel article, IFormFile Image) { if (!ModelState.IsValid) { return(View(article)); } article.Content = this.html.Sanitize(article.Content); if (Image != null) { article.ImageName = await SaveImage(Image); } await this.blogService.EditAsync(article); TempData[WebConstants.TempDataSuccessMessageKey] = ($"Article {article.Title} successfuly updated."); return(RedirectToAction(nameof(BlogController.Index), new { page = 1 })); }
public async Task Create_WithCorrectData_ShouldSuccesfullyCreate() { string errorMessagePrefix = "ArticlesService Method CreateArticle() does not work properly."; var context = CDGBulgariaInmemoryFactory.InitializeContext(); await SeedData(context); this.articlesService = new ArticlesService(context); ArticleServiceModel articleServiceModel = new ArticleServiceModel() { Title = "CDGHealthMeeting", Content = "Sofia is the place, where the association is founded.", }; bool actualResult = await this.articlesService.CreateArticle(articleServiceModel); Assert.True(actualResult, errorMessagePrefix); }
public async Task EditAsync(ArticleServiceModel article) { var articleForEdit = this.db.Articles.Where(a => a.Id == article.Id).FirstOrDefault(); articleForEdit.Title = article.Title; articleForEdit.Content = article.Content; if (!string.IsNullOrEmpty(article.LinkFrom)) { articleForEdit.LinkFrom = article.LinkFrom; } if (!string.IsNullOrEmpty(article.ImageName)) { articleForEdit.ImageName = article.ImageName; } this.db.Articles.Update(articleForEdit); await this.db.SaveChangesAsync(); }
public List <ArticleServiceModel> GetById(int Id) { var articles = unitOfWork.Articles.Find(x => x.Id == Id); List <ArticleServiceModel> result = new List <ArticleServiceModel>(); foreach (Article item in articles) { ArticleServiceModel articleSm = new ArticleServiceModel() { ArticlePrice = item.ArticlePrice, BuyerUserId = item.BuyerUserId, Id = item.Id, IsSold = item.IsSold, Name_of_article = item.Name_of_article, SoldDate = item.SoldDate }; result.Add(articleSm); } return(result); }
public async Task Edit_WithGivenNonExistenId_ShouldThrowArgumentNullException() { string errorMessagePrefix = "ArticlesService Method Edit() does not work properly."; var context = CDGBulgariaInmemoryFactory.InitializeContext(); await SeedData(context); this.articlesService = new ArticlesService(context); ArticleServiceModel expectedData = context.Articles.First().To <ArticleServiceModel>(); expectedData.Title = "Edited Title"; expectedData.Content = "Edited Content"; expectedData.CreatedOn = DateTime.UtcNow; expectedData.AuthorId = "gfbehs"; await this.articlesService.Edit(expectedData.Id, expectedData); await Assert.ThrowsAsync <ArgumentNullException>(() => this.articlesService.Edit("Non-Existent", expectedData)); }
public async Task <IActionResult> Edit(EditArticleInputModel inputModel) { if (!ModelState.IsValid) { return(this.View(inputModel)); } ArticleServiceModel articleServiceModel = inputModel.To <ArticleServiceModel>(); if (inputModel.ImageFormFile != null) { string imageUrl = await this.cloudinaryService .UploadPictureAsync(inputModel.ImageFormFile, inputModel.Title); await this.imageService.EditWithArticleAsync(imageUrl, articleServiceModel.Id); } await this.articleService.EditArticleAsync(articleServiceModel); return(this.RedirectToAction("All", "Articles")); }
public async Task <IActionResult> Edit(string id, ArticleEditInputModel articleEditInputModel) { if (id == null) { return(NotFound()); } if (!this.ModelState.IsValid) { return(this.View(articleEditInputModel)); } ArticleServiceModel articleServiceModel = new ArticleServiceModel() { Title = articleEditInputModel.Title, Content = articleEditInputModel.Content }; await this.articlesService.Edit(id, articleServiceModel); return(this.Redirect("/Articles/All")); }
public async Task CreateShouldAddArticleToDatabaseSuccessfully() { this.SetupSqlite(); await this.SeedDatabase(); using var context = new ApplicationDbContext(this.ContextOptions); var repository = new EfDeletableEntityRepository <Article>(context); var articleService = new ArticleService(repository); var articleToAdd = new ArticleServiceModel { Title = "Test", Content = "Test", ImageUrl = "Test", UserId = "1", }; var articleId = await articleService.CreateAsync(articleToAdd); Assert.True(articleId != 0); }
public async Task Edit_WithCorrectData_ShouldEditArticleCorrectly() { string errorMessagePrefix = "ArticlesService Method Edit() does not work properly."; var context = CDGBulgariaInmemoryFactory.InitializeContext(); await SeedData(context); this.articlesService = new ArticlesService(context); ArticleServiceModel expectedData = context.Articles.First().To <ArticleServiceModel>(); expectedData.Title = "Edited Title"; expectedData.Content = "Edited Content"; await this.articlesService.Edit(expectedData.Id, expectedData); ArticleServiceModel actualData = context.Articles.First().To <ArticleServiceModel>(); Assert.True(actualData.Title == expectedData.Title, errorMessagePrefix + "Title not edited properly"); Assert.True(actualData.Content == expectedData.Content, errorMessagePrefix + "Content not edited properly"); }
public async Task Create_WithCorrectData_ShouldSuccesfullyCreate() { string errorMessagePrefix = "ArticlesService Method CreateArticle() does not work properly."; var context = MyLifeStyleInmemoryFactory.InitializeContext(); await SeedData(context); this.articlesService = new ArticlesService(context); ArticleServiceModel articleServiceModel = new ArticleServiceModel() { PublicationTitle = "Books Exchange", Description = "Users can add readed book to list of books for second hand exchange", CategoryId = "category_1", UserId = "Mimi_1", }; bool actualResult = await this.articlesService.CreateArticle(articleServiceModel); Assert.True(actualResult, errorMessagePrefix); }