public ActionResult <ItemResponse <int> > Create(CryptoPortfolioInsertRequest req) { ItemResponse <int> response = null; ActionResult result = null; try { int id = _cryptoPortfolioService.Create(req); if (id > 0) { response = new ItemResponse <int>(); response.Item = id; result = Ok200(response); } else { result = NotFound404(new ErrorResponse("Request could not be completed.")); } } catch (Exception ex) { Logger.LogError(ex.ToString()); result = StatusCode(500, new ErrorResponse(ex.Message.ToString())); } return(result); }
public int Create(CryptoPortfolioInsertRequest req) { int id = 0; using (SqlConnection con = new SqlConnection(connString)) { SqlCommand cmd = new SqlCommand("dbo.CryptoPortfolio_Insert", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@CoinId", req.CoinId); cmd.Parameters.AddWithValue("@ExchangeId", req.ExchangeId); cmd.Parameters.AddWithValue("@Price", req.Price); cmd.Parameters.AddWithValue("@Quantity", req.Quantity); SqlParameter outputParam = cmd.Parameters.Add("@Id", SqlDbType.Int); outputParam.Direction = ParameterDirection.Output; con.Open(); cmd.ExecuteNonQuery(); id = int.Parse(outputParam.Value.ToString()); con.Close(); } return(id); }