public async Task <int> Create(ReviewsCreate model) { using (var con = new SqlConnection(_config.GetConnectionString("MyConnectionString"))) { con.Open(); using (IDbTransaction transaction = con.BeginTransaction()) { try { string query = "exec [dbo].[usp_Reviews_Create] @UserID, @ProductId, @Comment, @Rating"; var result = await con.QueryAsync <int>(query, new { UserID = model.UserID, ProductId = model.ProductId, Comment = model.Comment, Rating = model.Rating }, transaction); transaction.Commit(); return(result.FirstOrDefault()); } catch (Exception) { transaction.Rollback(); throw; } } }; }
public async Task <IActionResult> Create([FromBody] ReviewsCreate model) { List <String> messages = new List <String>(); var product = await _productService.GetById(model.ProductId); var user = await _userService.GetByUsername(model.Username); if (product == null) { messages.Add("Product not found!"); } if (user == null) { messages.Add("User not found!"); } else { model.UserID = user.ID; } if (model.Rating <= 0 && model.Rating > 10) { messages.Add("Rating must be in range 0-10"); } if (messages.Any()) { return(StatusCode(StatusCodes.Status500InternalServerError, new { ErrorCode = StatusCodes.Status500InternalServerError, ErrorMessages = string.Join(",", messages) })); } var reviewId = await _reviewService.CreateReview(model); return(Ok(new { resultId = reviewId, desciption = reviewId > 0 ? "Successfully created." : "Could not create this reivew." })); }