public async Task <IActionResult> ToggleEntry(int id) { try { FactFileEntry factFileEntry = await _Db.FactFileEntries.Include(entry => entry.Activities).Where(entry => entry.Id == id).FirstOrDefaultAsync(); if (factFileEntry == null) { return(NotFound(new ResponseHelper("Something went wrong. If the problem persists, please contact the developers"))); } if (factFileEntry.Activities.Count > 0) { StringBuilder message = new StringBuilder();; message.AppendLine("This entry cannot be disabled while it is \n linked to the following guided walk activities:\n"); foreach (string title in factFileEntry.Activities.Select(a => a.Title).ToList()) { message.AppendLine(title + ", "); } return(BadRequest(new ResponseHelper(message.ToString()))); } factFileEntry.Active = !factFileEntry.Active; await _Db.SaveChangesAsync(); return(Ok()); } catch (Exception ex) { _Logger.LogError("Error toggling 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> 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))); } }
public async Task <IActionResult> GetEntryDetails(int id) { try { FactFileEntry entry = await _Db.FactFileEntries .Include(e => e.ListenAudio) .Include(e => e.PronounceAudio) .Include(e => e.FactFileEntryImages) .ThenInclude(ei => ei.MediaFile) .Include(e => e.FactFileNuggets) .ThenInclude(n => n.Image) .Where(e => e.Id == id).FirstOrDefaultAsync(); if (entry == null) { return(NotFound(new ResponseHelper("Something went wrong, please contact the developers if the problem persists."))); } return(Ok(new { entry.Active, entry.PrimaryName, entry.AltName, entry.MainImageId, Images = entry.FactFileEntryImages.Select(image => new { image.MediaFile.Id, image.MediaFile.Filename, image.MediaFile.Name, isSquare = image.MediaFile.Height == image.MediaFile.Width, }), ListenAudio = entry.ListenAudio != null ? new { entry.ListenAudio.Id, entry.ListenAudio.Name, entry.ListenAudio.Filename, entry.ListenAudio.MediaType } : null, PronounceAudio = entry.PronounceAudio != null ? new { entry.PronounceAudio.Id, entry.PronounceAudio.Name, entry.PronounceAudio.Filename, entry.PronounceAudio.MediaType } : null, Nuggets = entry.FactFileNuggets.OrderBy(nugget => nugget.OrderIndex).Select(nugget => new { nugget.Id, nugget.OrderIndex, nugget.Name, nugget.Text, Image = new { nugget.Image?.Id, nugget.Image?.Filename, nugget.Image?.Name, isSquare = nugget.Image?.Height == nugget.Image?.Width } }), entry.BodyText })); } catch (Exception ex) { _Logger.LogError("Error retrieving entry details", ex.Message); _Logger.LogError(ex.StackTrace); return(BadRequest(new ResponseHelper("Something went wrong, please try again in a few minutes.", ex.Message))); } }