public async Task AddProposedTopic(string topicName, string username) { using (var databaseContext = new DatabaseContext()) { var user = await databaseContext.Users.SingleOrDefaultAsync(u => u.UserName == username); var topic = new ProposedTopic() { TopicName = topicName, User = user }; databaseContext.ProposedTopics.Add(topic); await databaseContext.SaveChangesAsync(); } }
public async Task <IActionResult> Create([Bind("Id,Name")] ProposedTopic proposedTopic) { if (ModelState.IsValid) { _context.Add(proposedTopic); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ModelState.AddModelError("", "Тема должна быть длиной от 10 до 100 символов"); ViewData["topics"] = await _context.ProposedTopics.ToListAsync(); return(View("Index", proposedTopic)); }
public async Task <IActionResult> Index(ProposedTopic topic) { ViewData["topics"] = await _context.ProposedTopics.ToListAsync(); return(View(topic ?? new ProposedTopic())); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var pathToUpdate = await _context.Paths.FindAsync(id); if (pathToUpdate == null) { return(RedirectToPage("/error", new { errorMessage = "That's Odd! We were unable to find this Path." })); } // Is this an attempt to change the name? If so check the name. if (pathToUpdate.Name.ToLower() != Name.ToLower()) { if (await Path.PathNameAlreadyExistsStaticAsync(_context, Name)) { ModelState.AddModelError("Name", "Sorry, this Name is already in use."); } } // confirm our owner is a valid path owner. IdentityUser user = await _userManager.GetUserAsync(User); if (!pathToUpdate.IsPathOwner(user.Email)) { return(RedirectToPage("/error", new { errorMessage = "Sorry! Only a Path Owner is allowed to publish a Path" })); } // Try to update our model with the bound Name, Topic Properties. BibleId = await Path.GetValidBibleIdAsync(_context, BibleId); BibleVerses = await Path.GetPathVersesAsync(_context, BibleId); if (await TryUpdateModelAsync <Path>( pathToUpdate, "Path", p => p.Name, p => p.Topics)) { // Now We've got some necessary validation to do here so let's build the verse text block pathToUpdate.Name = Name; // Name is handled seperately for remote validation to work. StringBuilder PathText = new StringBuilder(); foreach (BibleVerse Verse in BibleVerses) { PathText.Append(Verse.Text + " "); } String PathTextForCompare = PathText.ToString().ToLower(); if (!PathTextForCompare.Contains(pathToUpdate.Name.ToLower())) { //Invalidate the Model State by adding a Model Error ModelState.AddModelError(string.Empty, "Please check Name and Topics"); ModelState.AddModelError("Name", "Sorry! The supplied name was not found in the text shown below. Please select a Path Name from Bible passages in this Path."); } if (pathToUpdate.Topics != null) { string[] IndividualProposedTopics = pathToUpdate.Topics.Split(','); foreach (string ProposedTopic in IndividualProposedTopics) { if (!(PathTextForCompare.Contains(ProposedTopic.ToLower()))) { //Invalidate the Model State by adding a Model Error ModelState.AddModelError(string.Empty, "Please check Name and Topics"); string ErrorMessage = "Sorry! The following Topic was not found in the Bible passages in this Path: " + ProposedTopic; ModelState.AddModelError("Path.Topics", ErrorMessage); } } } if (!ModelState.IsValid) { //BibleVerses is already set return(Page()); } _context.Attach(pathToUpdate).State = EntityState.Modified; pathToUpdate.Modified = DateTime.Now; pathToUpdate.IsPublished = true; await _context.SaveChangesAsync(); return(RedirectToPage("./MyPaths")); } //BibleVerses set above return(Page()); }