public async Task <IActionResult> PutTohHero(long id, TohHero tohHero) { if (id != tohHero.Id) { return(BadRequest()); } _context.Entry(tohHero).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TohHeroExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <TohHero> > PostTohHero(TohHero tohHero) { _context.TohHeroes.Add(tohHero); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(GetTohHero), new { id = tohHero.Id }, tohHero)); }
public TohHero updateHero(int id, TohHero updated) { var text = $"UPDATE Heroes SET Name = @Name WHERE HeroId = @HeroId"; using (SqlConnection conn = new SqlConnection(_settings.sqlConnection)) { SqlCommand cmd = new SqlCommand(text, conn); conn.Open(); List <SqlParameter> param = new List <SqlParameter>() { new SqlParameter("@HeroId", System.Data.SqlDbType.Int) { Value = id }, new SqlParameter("@Name", System.Data.SqlDbType.VarChar) { Value = updated.Name } }; cmd.Parameters.AddRange(param.ToArray()); using (SqlDataReader reader = cmd.ExecuteReader()) { var hero = new TohHero { HeroId = id, Name = updated.Name, Likes = updated.Likes }; return(hero); } } }
public TohHero getHero(int id) { var text = "Select * FROM heroes WHERE HeroId = @HeroId"; using (SqlConnection conn = new SqlConnection(_settings.sqlConnection)) { SqlCommand cmd = new SqlCommand(text, conn); SqlParameter param = new SqlParameter(); param.ParameterName = "@HeroId"; param.Value = id; cmd.Parameters.Add(param); conn.Open(); using (SqlDataReader reader = cmd.ExecuteReader()) { var hero = new TohHero(); while (reader.Read()) { hero.HeroId = (int)reader[0]; hero.Name = (string)reader[1]; hero.Likes = (int)reader[2]; } return(hero); } } }
public List <TohHero> getHeroes() { var text = "SELECT * FROM Heroes"; using (SqlConnection conn = new SqlConnection(_settings.sqlConnection)) { SqlCommand cmd = new SqlCommand(text, conn); conn.Open(); using (SqlDataReader reader = cmd.ExecuteReader()) { var Heroes = new List <TohHero>(); while (reader.Read()) { var hero = new TohHero { HeroId = (int)reader[0], Name = (string)reader[1], Likes = (int)reader[2] }; Heroes.Add(hero); } return(Heroes); } } }
public TohHero addHero(TohHero hero) { var text = $"INSERT INTO Heroes (Name, Likes) VALUES (@Name, @Likes)"; using (SqlConnection conn = new SqlConnection(_settings.sqlConnection)) { SqlCommand cmd = new SqlCommand(text, conn); conn.Open(); List <SqlParameter> param = new List <SqlParameter>() { new SqlParameter("@Name", System.Data.SqlDbType.VarChar) { Value = hero.Name }, new SqlParameter("@Likes", System.Data.SqlDbType.Int) { Value = hero.Likes } }; cmd.Parameters.AddRange(param.ToArray()); using (SqlDataReader reader = cmd.ExecuteReader()) { return(hero); } } }
public async Task <IActionResult> AddHero([HttpTrigger(AuthorizationLevel.Function, "post", Route = "add")] HttpRequest req) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var input = JsonConvert.DeserializeObject <TohHero>(requestBody); var hero = new TohHero() { Name = input.Name, Likes = 1 }; var newHero = _heroSqlQueries.addHero(hero); return(new OkObjectResult(newHero)); }
public async void AddHeroTest(HeroFunction heroFunction) { this._heroFunction = heroFunction; var req = new TohHero() { Name = "Flame Boy", Likes = 1 }; var query = new Dictionary <String, StringValues>(); var body = JsonConvert.SerializeObject(req, Formatting.Indented); var result = await _heroFunction.AddHero(HttpRequestSetup(query, body)); var resultObject = (OkObjectResult)result; var resultResponse = new TohHero() { Name = "Flame Boy", Likes = 1 }; var resultBody = JsonConvert.SerializeObject(resultResponse, Formatting.Indented); Assert.AreEqual(resultBody, resultObject.Value); }