using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreWebAPI1.Models;
using BWPMModels;
using System.Data;
using SecuringWebApiUsingApiKey.Attributes;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreWebAPI1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    
    public class @TablenameController : ControllerBase
    {
        // GET: api/<@TablenameController>
        [HttpGet]
        public List<@Tablename> Get()
        {
             dbhelper dbh = new dbhelper();
            //dbh.Get_Tabledata("Select * from [@Tablename]", false, true);

            List<BWPMModels.@Tablename> Details = new List<BWPMModels.@Tablename>();
            return dbh.ConvertDataTable<BWPMModels.User>(dbh.Get_Tabledata("Select * from [@Tablename]", false, true));
        }

     
        // GET api/<@TablenameController>/5
        [HttpGet("{id}")]
        public List<@Tablename> Get(int id)
        {
            dbhelper dbh = new dbhelper();
            List<@Tablename> Details = new List<@Tablename>();
            return dbh.ConvertDataTable<User>(dbh.Get_Tabledata("Select * from [@Tablename] where id=" + id.ToString(), false, true));
        }
        
        // POST api/<@TablenameController>
        [HttpPost]
        public void Post([FromBody] @Tablename @tablename)
        {
            dbhelper dbh = new dbhelper();
            dbh.Get_Tabeldata_for_Update("Select top 1 * from [@Tablename] where id=-1", false, true);
            DataRow dr = dbh.dsdaten.Tables[0].NewRow();
            user.GetType().GetProperties().ToList().ForEach(f =>
            {
                try
                {
                    if ( f.PropertyType == typeof(DateTime))
                    {
                        dr[f.Name] = (DateTime)f.GetValue(user, null);

                    }
                    else
                    {
                        dr[f.Name] = f.GetValue(user, null);
                    }
                }
                catch (Exception ex) { string s = ex.Message; }
            });
            dbh.dsdaten.Tables[0].Rows.Add(dr);
            dbh.Update_Tabeldata();
        }

        // PUT api/<@TablenameController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] @Tablename @tablename)
        {
            dbhelper dbh = new dbhelper();
            dbh.Get_Tabeldata_for_Update("Select top 1 * from [@Tablename] where id="+id.ToString(), false, true);
            DataRow dr = dbh.dsdaten.Tables[0].Rows[0];
            user.GetType().GetProperties().ToList().ForEach(f =>
            {
                try
                {
                    if (f.PropertyType == typeof(DateTime))
                    {
                        dr[f.Name] = (DateTime)f.GetValue(user, null);
                    }
                    else
                    {
                        dr[f.Name] = f.GetValue(user, null);
                    }
                }
                catch (Exception ex) { string s = ex.Message; }
            });
            dbh.Update_Tabeldata();
          
        }

        // DELETE api/<@TablenameController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
            dbhelper dbh = new dbhelper();
            dbh.Get_Tabeldata_for_Update("Select top 1 * from [@Tablename] where id="+id, false, true);
            DataRow dr = dbh.dsdaten.Tables[0].Rows[0];
            dr["Aktiv"] = false;
            dr["mutiert_am"] = DateTime.Now;
            dbh.Update_Tabeldata();
        }
    }
}
