From 4cc763706597d6ea82392f653333e33c31485b49 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期五, 22 十二月 2023 15:23:01 +0800 Subject: [PATCH] 提交查询、删除接口 --- JiangSu/index.html | 2 JiangSu/Controllers/OpController.cs | 60 +++++++++++++++++++ JiangSu/cs/ModelDAL.cs | 59 +++++++++++++++++++ JiangSu/App_Data/js.db | 0 4 files changed, 118 insertions(+), 3 deletions(-) diff --git a/JiangSu/App_Data/js.db b/JiangSu/App_Data/js.db index 1fe0a8f..e08fa83 100644 --- a/JiangSu/App_Data/js.db +++ b/JiangSu/App_Data/js.db Binary files differ diff --git a/JiangSu/Controllers/OpController.cs b/JiangSu/Controllers/OpController.cs index 3f41a1f..145f3a2 100644 --- a/JiangSu/Controllers/OpController.cs +++ b/JiangSu/Controllers/OpController.cs @@ -12,9 +12,65 @@ public class OpController : ApiController { [HttpGet] - public Model SelectById(int id) + public Model SelectById(long id) { - return ModelDAL.SelectById(id); + try + { + return ModelDAL.SelectById(id); + } + catch (Exception ex) + { + LogOut.Error(ex.StackTrace); + return null; + } + } + + [HttpGet] + public int DeleteByIds([FromUri] List<long> ids) + { + try + { + if (null == ids || ids.Count == 0) return 0; + + return ModelDAL.DeleteByIds(ids); + } + catch (Exception ex) + { + LogOut.Error(ex.StackTrace); + return 0; + } + } + + [HttpPost] + public int insert([FromBody] Model model) + { + try + { + if (null == model) return 0; + + return ModelDAL.insert(model); + } + catch (Exception ex) + { + LogOut.Error(ex.StackTrace); + return 0; + } + } + + [HttpPost] + public int UpdateById([FromBody] Model model) + { + try + { + if (null == model) return 0; + + return ModelDAL.UpdateById(model); + } + catch (Exception ex) + { + LogOut.Error(ex.StackTrace); + return 0; + } } } } diff --git a/JiangSu/cs/ModelDAL.cs b/JiangSu/cs/ModelDAL.cs index defad0a..45be61d 100644 --- a/JiangSu/cs/ModelDAL.cs +++ b/JiangSu/cs/ModelDAL.cs @@ -4,13 +4,46 @@ using System.Data; using System.Data.SQLite; using System.Linq; +using System.Reflection; using System.Web; namespace JiangSu.cs { public class ModelDAL { - public static Model SelectById(int id) + /// <summary> + /// 鑾峰彇鍙傛暟鍒楄〃 + /// </summary> + public static SQLiteParameter[] GetParams<T>(string sql, T t) + { + List<SQLiteParameter> list = new List<SQLiteParameter>(); + Type tType = typeof(T); + BindingFlags flags = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance; + + int start = sql.IndexOf("@"); + while (start != -1) + { + int end = sql.IndexOf(",", start); + if (end == -1) end = sql.IndexOf(")", start); + if (end == -1) end = sql.IndexOf(" ", start); + if (end == -1) end = sql.Length; + + string name = sql.Substring(start + 1, end - start - 1); + PropertyInfo pi = tType.GetProperty(name, flags); + if (pi != null) + { + object value = pi.GetValue(t, null); + SQLiteParameter dp = new SQLiteParameter("@" + name, value); + list.Add(dp); + } + + start = sql.IndexOf("@", end); + } + + return list.ToArray(); + } + + public static Model SelectById(long id) { string sql = "select * from model where id = @id"; @@ -27,5 +60,29 @@ return null == list || list.Count == 0 ? null : list[0]; } + + public static int DeleteByIds(List<long> list) + { + string ids = string.Join(",", list.ToArray()); + string sql = string.Format("delete from model where id in ({0})", ids); + + return SQLiteHelper.ExecuteNonQuery(sql); + } + + public static int insert(Model model) + { + string sql = "insert into model (name, json) values (@name, @json)"; + SQLiteParameter[] sqlParams = GetParams<Model>(sql, model); + + return SQLiteHelper.ExecuteNonQuery(sql, sqlParams); + } + + public static int UpdateById(Model model) + { + string sql = "update model set name = @name, json = @json where id = @id"; + SQLiteParameter[] sqlParams = GetParams<Model>(sql, model); + + return SQLiteHelper.ExecuteNonQuery(sql, sqlParams); + } } } diff --git a/JiangSu/index.html b/JiangSu/index.html index a2bf56f..497638a 100644 --- a/JiangSu/index.html +++ b/JiangSu/index.html @@ -8,5 +8,7 @@ <a href="http://localhost/JiangSu/Values/Get">GetValues</a> <br /> <a href="http://localhost/JiangSu/Op/SelectById?id=1">SelectById?id=1</a> <br /> + + <a href="http://localhost/JiangSu/Op/DeleteByIds?ids=2,3">DeleteByIds?ids=2,3</a> <br /> </body> </html> -- Gitblit v1.9.3