using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Http;

using Microsoft.AspNetCore.Mvc;

 

using System.Data;              // import needed for DataSet and other data classes

using System.Data.SqlClient;    // import needed for ADO.NET classes

using Utilities;                // import needed for DBConnect class

 

namespace Core2WebAPI.Controllers

{

    // AUTO-ADDED: [Produces("application/json")]

    //[Route("api/Teams")]

    [Route("api/[controller]")]

    public class TeamsController : Controller

    {

        // GET api/teams

        [HttpGet]

        public List<Team> Get()

        {

            DBConnect objDB = new DBConnect();

            DataSet ds = objDB.GetDataSet("SELECT * FROM Teams");

            List<Team> teams = new List<Team>();

            Team team;

 

            foreach(DataRow record in ds.Tables[0].Rows)

            {

                team = new Team();

                team.TeamID = int.Parse(record["TeamID"].ToString());

                team.Name = record["TeamName"].ToString();

                team.University = record["University"].ToString();

                team.Mascot = record["Mascot"].ToString();

                teams.Add(team);

            }

 

            return teams;

        }

 

        // GET api/teams/2018

        [HttpGet("{id}")]

        public Team Get(int id)

        {

            DBConnect objDB = new DBConnect();

            DataSet ds = objDB.GetDataSet("SELECT * FROM Teams WHERE TeamID = " + id);

            DataRow record;

 

            Team team = new Team();

 

            if (ds.Tables[0].Rows.Count != 0)

            {

                record = ds.Tables[0].Rows[0];

                team.TeamID = int.Parse(record["TeamID"].ToString());

                team.Name = record["TeamName"].ToString();

                team.University = record["University"].ToString();

                team.Mascot = record["Mascot"].ToString();

            }

 

            return team;

        }

 

        // POST api/teams

        // The [FromBody] attribute is needed in order to pass a JSON object

        // and allow the model-binding to occur properly. This tells the .NET framework

        // to use the 'content-type' header information from the HTTP Request to

        // determine which of the configured IInputFormatters to use in the model-binding.

        [HttpPost]

        public Boolean Post([FromBody] Team theTeam)

        {

            DBConnect objDB = new DBConnect();

            string strSQL = "INSERT INTO Teams (TeamName, University, Mascot) " +

                            "VALUES ('" + theTeam.Name + "', '" + theTeam.University + "', '" + theTeam.Mascot + "')";

 

            // Execute the INSERT statement in the database

            // The DoUpdate() method returns the number of records effected by the INSERT statement.

            // Otherwise, it returns -1 when there was an error exception.

            int result = objDB.DoUpdate(strSQL);

          

            if (result > 0)

                return true;

 

 

            return false;

        }

 

    }

}