public async Task <IActionResult> Add(AddDestinationViewModel model) { if (!string.IsNullOrWhiteSpace(model.Name) && await destinationService.ExistsAsync(model.Name)) { ModelState.AddModelError(nameof(model.Name), "Destination with the same name already exists."); } var invalidIds = await tagService.CheckForInvalidTagIds(model.SelectedTags); if (invalidIds.Any()) { ModelState.AddModelError(nameof(model.SelectedTags), $"Non-existing tags were selected. (Count: {invalidIds.Count})"); } if (!await regionService.ExistsAsync(model.RegionId)) { ModelState.AddModelError(nameof(model.RegionId), "Selected region does not exist."); } if (!ModelState.IsValid) { model.Tags = await GetTags(); model.Regions = await GetRegions(); return(View(model)); } await destinationService.AddAsync(model.Name, model.RegionId, model.SelectedTags); return(RedirectToAction(nameof(Add))); }
public async Task <IActionResult> Add(AddArticleViewModel model) { if (!string.IsNullOrWhiteSpace(model.Title) && await articleService.ExistsAsync(model.Title)) { ModelState.AddModelError(nameof(model.Title), "Article with the same title already exists."); } if (!await destinationService.ExistsAsync(model.DestinationId)) { ModelState.AddModelError(nameof(model.DestinationId), "Selected destination does not exist."); } if (!ModelState.IsValid) { model.Destinations = await GetDestinations(); return(View(model)); } var authorId = userManager.GetUserId(User); await articleService.AddAsync( model.Title, model.Content, model.DestinationId, authorId, DateTime.UtcNow); return(RedirectToAction(nameof(Add))); }
public async Task <int> AddAsync(string title, string content, int destinationId, string authorId, DateTime creationDate) { if (!await destinationService.ExistsAsync(destinationId)) { return(0); } var author = await userManager.FindByIdAsync(authorId); if (author == null) { return(0); } var article = new Article { Title = title, Content = content, CreationDate = creationDate, AuthorId = authorId, DestinationId = destinationId }; await database.Articles.AddAsync(article); await database.SaveChangesAsync(); return(article.Id); }
public async Task <IActionResult> RemoveTag(RemoveTagRequestModel model) { if (!await destinationService.ExistsAsync(model.DestinationId)) { TempData.AddErrorMessage("Destination does not exist."); return(RedirectToAction(nameof(All))); } if (!await tagService.ExistsAsync(model.TagId)) { TempData.AddErrorMessage("Destination does not exist."); return(RedirectToAction(nameof(Details), new { id = model.DestinationId })); } if (!ModelState.IsValid) { TempData.AddErrorMessage("Invalid data."); return(RedirectToAction(nameof(All))); } await destinationService.RemoveTagAsync(model.DestinationId, model.TagId); TempData.AddSuccessMessage(); return(RedirectToAction(nameof(Details), new { id = model.DestinationId })); }