public async Task <EpisodeExtendedExtra> GetClaimedEpisode(int id) { try { HttpResponseMessage response = null; using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{_httpClient.BaseAddress}episodes/{id}")) { LoginResponse token = await _userSession.GetToken(); if (token != null) { requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken); } response = await _httpClient.SendAsync(requestMessage); } if (response.IsSuccessStatusCode) { string json = response.Content.ReadAsStringAsync().Result; EpisodeExtendedExtra ep = JsonSerializer.Deserialize <EpisodeExtendedExtra>(json); return(ep); } } catch (Exception e) { SentrySdk.CaptureException(e); } return(null); }
public async Task <ActionResult <List <TopicExtended> > > PostTopicList([FromBody] TopicRequest request) { if (request.Topics == null) { return(BadRequest("Topic list is null!")); } try { User user = await _auth.GetUserFromValidToken(Request); Episode claimedEpisode = await _claims.GetUserByClaimedEpisodeAsync(user.Id); // Topics and Subtopics await _database.DeleteTopicAndSubtopicAsync(claimedEpisode.Id); await _database.ResetIdentityForTopicAndSubtopicsAsync(); for (int i = 0; i < request.Topics.Count; i++) { await _database.InsertTopicAsync(new ProcessedTopicPostRequest(request.Topics[i]), claimedEpisode.Id, user.Id); } // People await _database.DeletePeopleFromEpisodeByEpisodeIdAsync(claimedEpisode.Id); await _database.ResetIdentityForPersonInEpisodeTableAsync(); await _database.InsertPeopleInEpisodeAsync(request.People, claimedEpisode.Id); EpisodeExtendedExtra episode = await _database.GetEpisodeAsync(claimedEpisode.Id, true, user.Id); return(Ok(episode)); } catch (TokenDoesNotExistException e) { return(Unauthorized()); } catch (Exception e) { if (e.Message.Contains("Sequence contains no elements")) { return(Unauthorized("User has no claimed episode!")); } SentrySdk.CaptureException(e); _logger.LogError("Error while trying to post a list of topics. Error:\n" + e.Message); return(Problem()); } }
public async Task <IActionResult> Post(int id) { try { User user = await _authenticationService.GetUserFromValidToken(Request); if (!await _authenticationService.CheckIfUserHasElivatedPermissionByUserObject(user)) { return(Unauthorized()); } EpisodeExtendedExtra episode = await _databaseService.GetEpisodeAsync(id, true, user.Id); if (episode.Topic == null || episode.Topic.Count == 0) { return(BadRequest("Episode has nothing to verify.")); } if (episode.Verified) { return(BadRequest("Episode is already verified.")); } await _databaseService.VerifyEpisodeAsync(id); await _search.AddTopicsAsync(episode.Topic); return(Ok()); } catch (TokenDoesNotExistException e) { return(Unauthorized()); } catch (Exception e) { if (e.Message.Contains("Sequence contains no elements")) { return(BadRequest("Episode is not open to verify.")); } SentrySdk.CaptureException(e); _logger.LogError(e.Message); return(Problem()); } }
public async Task <EpisodeExtendedExtra> GetEpisode(int id) { try { var response = await _httpClient.GetAsync($"{_httpClient.BaseAddress}episodes/{id}"); if (response.IsSuccessStatusCode) { string json = response.Content.ReadAsStringAsync().Result; EpisodeExtendedExtra ep = JsonSerializer.Deserialize <EpisodeExtendedExtra>(json); return(ep); } } catch (Exception e) { SentrySdk.CaptureException(e); } return(null); }
public async Task <EpisodeExtendedExtra> GetEpisodeAsync(int episodeId, bool editorRequest = false, int userId = 0) { _logger.LogInformation($"Returning episode with id: {episodeId}."); var parameters = new { epId = episodeId, uId = userId }; var query = $"SELECT * FROM udf_GetEpisodes() WHERE id=@epId LIMIT 1"; if (userId != 0) { query = $"SELECT * FROM udf_GetEpisodes(@uId) WHERE id=@epId LIMIT 1"; } using (var connection = _context.CreateConnection()) { EpisodeExtendedExtra episode = await connection.QuerySingleAsync <EpisodeExtendedExtra>(query, parameters); if (episode.Verified || editorRequest) { episode.Topic = await GetTopicsAsync(episode.Id); episode.Person = await GetPeopleFeaturedInEpisodeByEpisodeIdAsync(episode.Id); } return(episode); } }