public void Post([FromBody] TeamPick value) { value.User = System.Convert.ToInt32(HttpContext.Session.GetString("User")); using (var db = new MySqlConnection(_configuration["ConnectionStrings:DefaultConnection"])) { var data = db.Query <TeamPick>(@"INSERT INTO Picks(Pick, Game, User) VALUES(@Pick, @Game, @User); Select * from Picks where Id=@id", value).FirstOrDefault(); } }
public void Put(int id, [FromBody] TeamPick value) { value.User = System.Convert.ToInt32(HttpContext.Session.GetString("User")); using (var db = new MySqlConnection(_configuration["ConnectionStrings:DefaultConnection"])) { var data = db.Query <TeamPick>(@"UPDATE Picks SET Pick=@Pick WHERE Id=@Id; Select * from Picks where Id=@id", value).FirstOrDefault(); } }
public JsonResult InsertPicks([FromBody] IEnumerable <TeamPick> values) { var t = values; var user = System.Convert.ToInt32(HttpContext.Session.GetString("User")); using (var db = new MySqlConnection(_config["ConnectionStrings:DefaultConnection"])) { if (db.State == ConnectionState.Closed) { db.Open(); } var transaction = db.BeginTransaction(); try { for (int i = 0; i < values.Count(); ++i) { TeamPick pick = values.ElementAt(i); pick.User = user; var data = db.Query <TeamPick>(@"INSERT INTO Picks(Pick, Game, User) VALUES(@Pick, @Game, @User); Select * from Picks where Id=@id", pick).FirstOrDefault(); } transaction.Commit(); } catch (Exception ex) { //Log the exception (ex) try { transaction.Rollback(); } catch (Exception ex2) { Console.WriteLine("Roll back failed"); // roll back failed } } return(Json(values)); } }