public async Task <IActionResult> Update(int id, IFormCollection request) { Notice n = await _Database.Notices.FindAsync(id);; try { Enum.TryParse(request.Str("noticeboard"), out Noticeboard noticeboard); n.Title = request.Str("title"); n.Noticeboard = noticeboard; n.LongDesc = request.Str("long_desc"); n.Urgent = request.Bool("urgent") ? 1 : 0; n.Active = request.Bool("active"); await _Database.SaveChangesAsync(); return(Ok($"Notice \"{n.Title}\" updated.")); } catch (Exception ex) { _Logger.LogError("Error updating notice: {0}", ex.Message); _Logger.LogTrace(ex.Message); return(BadRequest(new ResponseHelper($"Error updating \"{n.Title}\".", ex.Message))); } }
public async Task <IActionResult> Create(IFormCollection request) { try { Enum.TryParse(request.Str("noticeboard"), out Noticeboard noticeboard); Notice n = new Notice { Title = request.Str("title"), Noticeboard = noticeboard, LongDesc = request.Str("long_desc"), Urgent = request.Bool("urgent") ? 1 : 0, Active = request.Bool("active"), }; await _Database.AddAsync(n); await _Database.SaveChangesAsync(); return(Ok($"Notice \"{n.Title}\" has been issued.")); } catch (Exception ex) { _Logger.LogError("Error saving notice: {0}", ex.Message); _Logger.LogTrace(ex.StackTrace); return(BadRequest(new ResponseHelper("Error issuing the notice, try again later.", ex.Message))); } }
//PUT /admin/pages/{pageId} public async Task <IActionResult> UpdatePage(int pageId, IFormCollection request, [Bind("Name", "Description", "Section")] Page changes) { Page page = await _Db.Pages.FindAsync(pageId); if (page == null) { return(NotFound()); } try { page.Name = changes.Name; page.Description = changes.Description; page.Section = changes.Section; page.Public = request.Bool("public"); await _Db.SaveChangesAsync(); return(Ok( Url.Action("Index", "Page", null, "https"))); } catch (Exception ex) { _Logger.LogWarning("Error updating page settings: {0}", ex.Message); _Logger.LogWarning(ex.StackTrace); return(BadRequest(new ResponseHelper("There was an error updating the page settings. Please try again later.", ex.Message))); } }
public async Task <IActionResult> SendEmail(IFormCollection request) { try { var reCAPTCHA = await _Recaptcha.Validate(request.Str("code")); if (!reCAPTCHA.success) { _Logger.LogWarning($"{request.Str("name")}'s captcha validation failed."); return(Conflict("Please complete the reCAPTCHA")); } var messageArgs = new EmailRecieved(request, this.Request.BaseUrl()); EmailContact Sender = new EmailContact { Name = messageArgs.Name, Address = messageArgs.SendersEmail }; if (request.Bool("SendToBookings")) { // Sends a master email to the "BOOKINGS" email address in system settings, then sends a copy to each person on the mailing list. await _EmailService.SendBookingInquiryAsync(Sender, messageArgs.Subject, messageArgs); } else { // Sends a master email to the "GENERAL" email address in system settings, then sends a copy to each person on the mailing list. await _EmailService.SendGeneralInquiryAsync(Sender, messageArgs.Subject, messageArgs); } _Logger.LogInformation("Sucsefully sent {0}'s email", messageArgs.SendersName); return(Ok()); } catch (Exception ex) { _Logger.LogError("Error sending {0}'s email: {1}", request.Str("name"), ex.Message); _Logger.LogError(ex.StackTrace); return(BadRequest("Something went wrong and we could not send your email. Please try again later.")); } }
//PUT /admin/pages/{pageId}/visibility public async Task <IActionResult> UpdatePageVisibility(int pageId, IFormCollection request) { try { Page page = await _Db.Pages.FindAsync(pageId); if (page == null) { return(NotFound()); } page.Public = request.Bool("visbility"); await _Db.SaveChangesAsync(); return(Ok()); } catch (Exception ex) { _Logger.LogWarning("Error updating page settings: {0}", ex.Message); _Logger.LogWarning(ex.StackTrace); return(BadRequest(new ResponseHelper("There was an error updating the page settings. Please try again later.", ex.Message))); } }
public async Task <IActionResult> Update(int id, IFormCollection request) { try { BaseMedia media = await _Db.Media.FindAsync(id); if (media == null) { return(NotFound("The requested media file does not exist.")); } // All types of media have a name, so we can update that media.Name = request.Str("name"); media.Source = request.Str("source"); media.ShowCopyright = request.Bool("showCopyright"); // If file is an image, we can also set the title and alt-text if (media is ImageMedia) { (media as ImageMedia).Title = request.Str("title"); (media as ImageMedia).Alt = request.Str("alt"); } // Save changes await _Db.SaveChangesAsync(); _Logger.LogDebug("Metadata updated for media file {0}", id); return(Ok()); } catch (Exception ex) { _Logger.LogError("Error updating metadata for media file {0}: {1}", id, ex.Message); _Logger.LogError(ex.StackTrace); return(BadRequest("Something went wrong, please try again later.")); } }
public async Task <IActionResult> AddEntry(int categoryId, IFormCollection form) { try { FactFileEntry entryToSave = new FactFileEntry(); // Validate inputs first // // // entryToSave.Active = form.Bool("active"); entryToSave.CategoryId = categoryId; // Update text fields entryToSave.PrimaryName = form.Str("primaryName"); entryToSave.AltName = form.Str("altName"); entryToSave.BodyText = form.Str("bodyText"); // Update media fields entryToSave.ListenAudioId = form.Int("listenAudioId"); entryToSave.PronounceAudioId = form.Int("pronounceAudioId"); if (entryToSave.ListenAudioId == 0) { entryToSave.ListenAudioId = null; } if (entryToSave.PronounceAudioId == 0) { entryToSave.PronounceAudioId = null; } entryToSave.MainImageId = form.Int("mainImageId"); await _Db.AddAsync(entryToSave); await _Db.SaveChangesAsync(); // to generate id // Remove and rebuild fact file entry image records int[] imageArray = JsonConvert.DeserializeObject <int[]>(form.Str("images")); entryToSave.FactFileEntryImages = new List <FactFileEntryImage>(); foreach (int imageId in imageArray) { entryToSave.FactFileEntryImages.Add(new FactFileEntryImage { FactFileEntryId = entryToSave.Id, MediaFileId = imageId }); } // Remove and rebuild fact file nugget records FactFileNugget[] updatedNuggets = JsonConvert.DeserializeObject <FactFileNugget[]>(form.Str("nuggets")); entryToSave.FactFileNuggets = new List <FactFileNugget>(); for (int i = 0; i < updatedNuggets.Length; i++) { updatedNuggets[i].OrderIndex = i; entryToSave.FactFileNuggets.Add(updatedNuggets[i]); } await _Db.SaveChangesAsync(); return(Ok(entryToSave.Id)); } catch (Exception ex) { _Logger.LogError("Error saving new entry", ex.Message); _Logger.LogError(ex.StackTrace); return(BadRequest(new ResponseHelper("Something went wrong, please try again in a few minutes.", ex.Message))); } }
public async Task <IActionResult> UpdateEntry(int id, IFormCollection form) { try { FactFileEntry entryToUpdate = await _Db.FactFileEntries .Include(entry => entry.FactFileEntryImages) .Include(entry => entry.FactFileNuggets) .Where(entry => entry.Id == id) .FirstOrDefaultAsync(); if (entryToUpdate == null) { return(NotFound(new ResponseHelper("Something went wrong, please contact the developers if the problem persists."))); } // Validate inputs first // // // entryToUpdate.Active = form.Bool("active"); // Update text fields entryToUpdate.PrimaryName = form.Str("primaryName"); entryToUpdate.AltName = form.Str("altName"); entryToUpdate.BodyText = form.Str("bodyText"); // Update media fields entryToUpdate.ListenAudioId = form.Int("listenAudioId"); entryToUpdate.PronounceAudioId = form.Int("pronounceAudioId"); if (entryToUpdate.ListenAudioId == 0) { entryToUpdate.ListenAudioId = null; } if (entryToUpdate.PronounceAudioId == 0) { entryToUpdate.PronounceAudioId = null; } entryToUpdate.MainImageId = form.Int("mainImageId"); // Remove and rebuild fact file entry image records int[] imageArray = JsonConvert.DeserializeObject <int[]>(form.Str("images")); _Db.RemoveRange(entryToUpdate.FactFileEntryImages); foreach (int imageId in imageArray) { entryToUpdate.FactFileEntryImages.Add(new FactFileEntryImage { FactFileEntryId = entryToUpdate.Id, MediaFileId = imageId }); } // Remove and rebuild fact file nugget records FactFileNugget[] updatedNuggets = JsonConvert.DeserializeObject <FactFileNugget[]>(form.Str("nuggets")); _Db.RemoveRange(entryToUpdate.FactFileNuggets); for (int i = 0; i < updatedNuggets.Length; i++) { updatedNuggets[i].OrderIndex = i; entryToUpdate.FactFileNuggets.Add(updatedNuggets[i]); } await _Db.SaveChangesAsync(); return(Ok()); } catch (Exception ex) { _Logger.LogError("Error updating entry", ex.Message); _Logger.LogError(ex.StackTrace); return(BadRequest(new ResponseHelper("Something went wrong, please try again in a few minutes.", ex.Message))); } }