using DPMService.Models; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Data; using SecuringWebApiUsingApiKey.Attributes; using DPMService.Models; using System.Security.Cryptography; using System.IO; using System.Text; namespace DPMService.Controllers { [Route("api/[controller]")] [ApiController] public class Service_View_PatController : ControllerBase { private string tblpraefix = ""; private string tblname = ""; private string apikey = ""; private string secretkey = ""; private string tablename = "Patient"; private void GetKeys() { apikey = get_headerinfo("ApiKey"); secretkey = get_headerinfo("SecKey"); dbhelper dbh = new dbhelper(); tblpraefix = dbh.Get_TablePraefix(apikey); } private string get_headerinfo(string headertype) { Microsoft.Extensions.Primitives.StringValues headerValues; var headerinfo = string.Empty; if (Request.Headers.TryGetValue(headertype, out headerValues)) { headerinfo = headerValues.FirstOrDefault(); return headerinfo; } else { return ""; }; } private string get_sql(string sql) { string tmpsql = sql; if (tblpraefix != "") tmpsql=tmpsql.Replace(tablename, tblpraefix + tablename); if (secretkey != "") tmpsql=tmpsql.Replace("&seckey&", secretkey); return tmpsql; } // GET: api/ [HttpGet] public List Get() { dbhelper dbh = new dbhelper(); List list = new List(); return dbh.ConvertDataTable(dbh.Get_Tabledata("Select * from [Service_View_Pat]", false, true)); } // GET api//5 [HttpGet("{id}")] public List Get(int id) { dbhelper dbh = new dbhelper(); List list = new List(); return dbh.ConvertDataTable(dbh.Get_Tabledata(string.Concat("Select * from [Service_View_Pat] where id=", id.ToString()), false, true)); } [HttpGet] [Route("search/{searchstring}")] public List Get(string searchstring) { //Models.Crypto enc = new Models.Crypto(); dbhelper dbh = new dbhelper(); dbh.Get_Tabeldata_for_Update("Select top 1 * from PatChargeLog where id=-1", false, true); DataRow dr = dbh.dsdaten.Tables[0].NewRow(); //dr[1] = namefilterenc; dbh.dsdaten.Tables[0].Rows.Add(dr); dbh.Update_Tabeldata(); dbh.dsdaten.Tables.Clear(); List Details = new List(); return dbh.ConvertDataTable(dbh.Get_Tabledata("Select * from [Service_View_Pat] where pat like '%" + searchstring + "%' order by pat", false, true)); } // POST api/ [HttpPost] public void Post([FromBody] Service_View_Pat Patient) { GetKeys(); dbhelper dbh = new dbhelper(); string sql = "Insert [Patient] (id,pat) values(" + Patient.ID.ToString() + ",dbo.encrypt('&seckey&','" + Patient.Pat + "'))"; dbh.Get_Tabledata(get_sql(sql), false, true); } [HttpPost("{id},{charge}")] public void Post(string id, string charge) { dbhelper dbh = new dbhelper(); dbh.Get_Tabeldata_for_Update("Select top 1 * from [Patient] where id=-1", false, true); DataRow dr = dbh.dsdaten.Tables[0].NewRow(); dr[1] = id; dr[2] = charge.ToString(); dr[3] = DateTime.Now; dr[4] = DateTime.Now; dr[5] = 1; dr[6] = true; dbh.dsdaten.Tables[0].Rows.Add(dr); dbh.Update_Tabeldata(); } // PUT api//5 [HttpPut("{id}")] public void Put(int id, [FromBody] Service_View_Pat Service_View_Pat) { dbhelper dbh = new dbhelper(); dbh.Get_Tabeldata_for_Update("Select top 1 * from Patient where id=" + id.ToString(), false, true); DataRow dr = dbh.dsdaten.Tables[0].Rows[0]; Service_View_Pat.GetType().GetProperties().ToList().ForEach(f => { try { if (f.PropertyType == typeof(DateTime)) { dr[f.Name] = (DateTime)f.GetValue(Service_View_Pat, null); } else { dr[f.Name] = f.GetValue(Service_View_Pat, null); } } catch (Exception ex) { string s = ex.Message; } }); dbh.Update_Tabeldata(); } // DELETE api//5 [HttpDelete("{id}")] public void Delete(int id) { dbhelper dbh = new dbhelper(); dbh.Get_Tabeldata_for_Update("Select top 1 * from [patient] where id=" + id, false, true); DataRow dr = dbh.dsdaten.Tables[0].Rows[0]; dr["Aktiv"] = false; dr["mutiert_am"] = DateTime.Now; dbh.Update_Tabeldata(); } } }