public async Task <ActionResult <CVIssueResponse> > SearchIssues(string name, string description) { ComicVineFilters filter = new ComicVineFilters(); if (name != null) { filter.AddFilter("name", name); } if (description != null) { filter.AddFilter("description", description); } CVIssueResponse response = await comicVine.GetIssues(filter); return(Ok(response)); }
public async Task <ActionResult <ComicBook> > AddComicToCollection(int id, ComicBook comicBook) { int userId = GetUserIdFromToken(); if (VerifyActiveUserOwnsCollection(id)) { if (CheckUserRole(userId) || collectionDAO.UserTotalComicCount(userId) < 100) { try { ComicBook existing = comicDAO.GetById(comicBook.Id); // Comic book is not in local database, get from API if (existing == null) { ComicVineFilters filters = new ComicVineFilters(); filters.AddFilter("id", comicBook.Id.ToString()); CVIssueResponse response = await comicVine.GetIssues(filters); if (response.StatusCode != 1) { throw new ComicVineException($"Failed ComicVine Issue request: {response.Error}"); } ComicBook issue = response.Results[0]; CVSingleIssueResponse issueDetails = await comicVine.GetIssueDetails(issue.ApiDetailUrl); List <Character> characters = issueDetails.Results.CharacterCredits; characterDAO.CheckDatabaseForCharacters(characters); List <Creator> creators = issueDetails.Results.PersonCredits; creatorDAO.CheckDatabaseForCreators(creators); Volume volume = volumeDAO.GetById(issueDetails.Results.Volume.Id); // Volume not in database, need to add details. if (volume == null) { CVVolumeResponse volumeResponse = await comicVine.GetVolumeDetails(issueDetails.Results.Volume.ApiDetailUrl); if (volumeResponse.StatusCode != 1) { throw new ComicVineException($"Failed ComicVine Volume Request: {volumeResponse.Error}"); } volumeDAO.AddVolume(volumeResponse.Results); volume = volumeResponse.Results; } comicBook.Volume = volume; using (TransactionScope scope = new TransactionScope()) { bool addedComic = comicDAO.AddComic(issue); bool addedImages = comicDAO.AddImages(issue); for (int i = 0; i < characters.Count; i++) { if (!characters[i].InDatabase) { bool addedChar = characterDAO.AddCharacterToTable(characters[i]); if (!addedChar) { throw new Exception("Failed to add character from ComicVine API"); } } bool addedCharToLinker = characterDAO.LinkCharacterToComic(characters[i].Id, issue.Id); if (!addedCharToLinker) { throw new Exception("Failed to add character to linker table"); } } for (int i = 0; i < creators.Count; i++) { if (!creators[i].InDatabase) { bool addedCreator = creatorDAO.AddCreatorCreditToTable(creators[i]); if (!addedCreator) { throw new Exception("Failed to add creator credit from ComicVine API"); } } bool addedCreatorToLinker = creatorDAO.LinkCreatorToComic(creators[i].Id, issue.Id); if (!addedCreatorToLinker) { throw new Exception("Failed to add creator to linker table"); } } if (addedComic && addedImages) { scope.Complete(); existing = issue; comicBook.Characters = characters; comicBook.Creators = creators; } else { throw new Exception("Failed to add new comic from ComicVine API"); } } } comicDAO.AddComicToCollection(id, existing); return(Created($"/user/collection/{id}", comicBook)); } catch (ComicVineException e) { return(StatusCode(502, new { message = $"Bad Gateway: 502 - {e.Message}" })); } catch (Exception e) { return(BadRequest(new { message = $"Could not add comic to collection - {e.Message}" })); } } else { return(BadRequest(new { message = "Need premium status to add more than 100 comics across all your collections." })); } } else { return(Unauthorized(new { message = "Not owner of collection" })); } }