public async Task <ActionResult <ReviewComment> > Create(ReviewCommentCreate reviewCommentCreate) { int applicationUserId = int.Parse(User.Claims.First(i => i.Type == JwtRegisteredClaimNames.NameId).Value); var createdReviewComment = await _reviewCommentRepository.UpsertAsync(reviewCommentCreate, applicationUserId); return(Ok(createdReviewComment)); }
public async Task <ReviewComment> UpsertAsync(ReviewCommentCreate reviewCommentCreate, int applicationUserId) { using (var dataTable = new DataTable()) { dataTable.Columns.Add("ReviewCommentId", typeof(int)); dataTable.Columns.Add("ParentReviewCommentId", typeof(int)); dataTable.Columns.Add("ReviewId", typeof(int)); dataTable.Columns.Add("Content", typeof(string)); dataTable.Rows.Add( reviewCommentCreate.ReviewCommentId, reviewCommentCreate.ParentReviewCommentId, reviewCommentCreate.ReviewId, reviewCommentCreate.Content); int?newReviewCommentId; using (var connection = new SqlConnection(_config.GetConnectionString("DefaultConnection"))) { await connection.OpenAsync(); newReviewCommentId = await connection.ExecuteScalarAsync <int?>( "ReviewComment_Upsert", new { ReviewComment = dataTable.AsTableValuedParameter("dbo.ReviewCommentType"), ApplicationUserId = applicationUserId }, commandType : CommandType.StoredProcedure); } newReviewCommentId = newReviewCommentId ?? reviewCommentCreate.ReviewCommentId; ReviewComment reviewComment = await GetAsync(newReviewCommentId.Value); return(reviewComment); } }