¶Ô±ÈÐÂÎļþ |
| | |
| | | using JiangSu.cs; |
| | | using JiangSu.Models; |
| | | using Newtonsoft.Json; |
| | | using Newtonsoft.Json.Linq; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Net; |
| | | using System.Net.Http; |
| | | using System.Web.Http; |
| | | |
| | | namespace JiangSu.Controllers |
| | | { |
| | | public class GridController : ApiController |
| | | { |
| | | [HttpGet] |
| | | public object SelectAll() |
| | | { |
| | | try |
| | | { |
| | | List<Grid> grids = GridDAL.SelectAll(); |
| | | List<JObject> list = GetJObjects(grids); |
| | | |
| | | Dictionary<string, object> dict = GetDict(); |
| | | dict.Add("features", list); |
| | | |
| | | return dict; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | private Dictionary<string, object> GetDict() |
| | | { |
| | | string crs = "{ \"type\": \"name\", \"properties\": { \"name\": \"urn:ogc:def:crs:OGC:1.3:CRS84\" } }"; |
| | | JObject crsJObj = JsonConvert.DeserializeObject<JObject>(crs); |
| | | |
| | | Dictionary<string, object> dict = new Dictionary<string, object>(); |
| | | dict.Add("type", "FeatureCollection"); |
| | | dict.Add("name", "ç½æ ¼4549"); |
| | | dict.Add("crs", crsJObj); |
| | | |
| | | return dict; |
| | | } |
| | | |
| | | private List<JObject> GetJObjects(List<Grid> grids) |
| | | { |
| | | List<JObject> list = new List<JObject>(); |
| | | if (null != grids && grids.Count > 0) |
| | | { |
| | | foreach (Grid g in grids) |
| | | { |
| | | JObject obj = JsonConvert.DeserializeObject<JObject>(g.json); |
| | | list.Add(obj); |
| | | } |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | [HttpGet] |
| | | public object SelectById(long id) |
| | | { |
| | | try |
| | | { |
| | | Grid grid = GridDAL.SelectById(id); |
| | | List<JObject> list = GetJObjects(null == grid ? null : new List<Grid> { grid }); |
| | | |
| | | Dictionary<string, object> dict = GetDict(); |
| | | dict.Add("features", list); |
| | | |
| | | return dict; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | [HttpGet] |
| | | public int DeleteByIds([FromUri] List<int> ids) |
| | | { |
| | | try |
| | | { |
| | | if (null == ids || ids.Count == 0) return 0; |
| | | |
| | | return GridDAL.DeleteByIds(ids); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | [HttpPost] |
| | | public int Insert([FromBody] Grid grid) |
| | | { |
| | | try |
| | | { |
| | | if (null == grid) return 0; |
| | | |
| | | return GridDAL.Insert(grid); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | [HttpPost] |
| | | public int UpdateById([FromBody] Grid grid) |
| | | { |
| | | try |
| | | { |
| | | if (null == grid) return 0; |
| | | |
| | | return GridDAL.UpdateById(grid); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | return 0; |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | <Compile Include="Controllers\HomeController.cs" /> |
| | | <Compile Include="Controllers\OpController.cs" /> |
| | | <Compile Include="Controllers\ValuesController.cs" /> |
| | | <Compile Include="Controllers\GridController.cs" /> |
| | | <Compile Include="cs\GridDAL.cs" /> |
| | | <Compile Include="cs\LogOut.cs" /> |
| | | <Compile Include="cs\ModelDAL.cs" /> |
| | | <Compile Include="cs\ModelHandler.cs" /> |
| | |
| | | <Compile Include="Global.asax.cs"> |
| | | <DependentUpon>Global.asax</DependentUpon> |
| | | </Compile> |
| | | <Compile Include="Models\Grid.cs" /> |
| | | <Compile Include="Models\Model.cs" /> |
| | | <Compile Include="Properties\AssemblyInfo.cs" /> |
| | | </ItemGroup> |
¶Ô±ÈÐÂÎļþ |
| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Web; |
| | | |
| | | namespace JiangSu.Models |
| | | { |
| | | public class Grid |
| | | { |
| | | public Grid() { } |
| | | |
| | | public long id { set; get; } |
| | | |
| | | public string json { set; get; } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | using JiangSu.Models; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Data; |
| | | using System.Data.SQLite; |
| | | using System.Linq; |
| | | using System.Web; |
| | | |
| | | namespace JiangSu.cs |
| | | { |
| | | public class GridDAL |
| | | { |
| | | public static List<Grid> SelectAll() |
| | | { |
| | | string sql = "select * from grid"; |
| | | |
| | | DataTable dt = SQLiteHelper.GetDataTable(sql); |
| | | if (null == dt || dt.Rows.Count == 0) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | List<Grid> list = ModelHandler.FillModel<Grid>(dt); |
| | | |
| | | return list; |
| | | } |
| | | |
| | | public static Grid SelectById(long id) |
| | | { |
| | | string sql = "select * from grid where id = @id"; |
| | | |
| | | SQLiteParameter param = new SQLiteParameter("@id"); |
| | | param.Value = id; |
| | | |
| | | DataTable dt = SQLiteHelper.GetDataTable(sql, param); |
| | | if (null == dt || dt.Rows.Count == 0) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | List<Grid> list = ModelHandler.FillModel<Grid>(dt); |
| | | |
| | | return null == list || list.Count == 0 ? null : list[0]; |
| | | } |
| | | |
| | | public static int DeleteByIds(List<int> ids) |
| | | { |
| | | string str = string.Join(",", ids.ToArray()); |
| | | string sql = string.Format("delete from grid where id in ({0})", str); |
| | | |
| | | return SQLiteHelper.ExecuteNonQuery(sql); |
| | | } |
| | | |
| | | public static int Insert(Grid grid) |
| | | { |
| | | string sql = "insert into grid (json) values (@json);select last_insert_rowid();"; |
| | | SQLiteParameter[] sqlParams = ModelDAL.GetParams<Grid>(sql, grid); |
| | | |
| | | object obj = SQLiteHelper.ExecuteScalar(sql, sqlParams); |
| | | return null == obj ? 0 : Convert.ToInt32(obj); |
| | | } |
| | | |
| | | public static int UpdateById(Grid grid) |
| | | { |
| | | string sql = "update grid set json = @json where id = @id"; |
| | | SQLiteParameter[] sqlParams = ModelDAL.GetParams<Grid>(sql, grid); |
| | | |
| | | return SQLiteHelper.ExecuteNonQuery(sql, sqlParams); |
| | | } |
| | | } |
| | | } |
| | |
| | | return JSON.stringify(model); |
| | | } |
| | | |
| | | function InsertGrid() { |
| | | var data = getGrid(); |
| | | |
| | | ajax("Grid/Insert", "POST", data, null, null, function (rs) { |
| | | console.log(rs); |
| | | alert(rs); |
| | | }); |
| | | } |
| | | |
| | | function UpdateGridById() { |
| | | var data = getGrid(); |
| | | |
| | | ajax("Grid/UpdateById", "POST", data, null, null, function (rs) { |
| | | console.log(rs); |
| | | alert(rs); |
| | | }); |
| | | } |
| | | |
| | | function getGrid() { |
| | | var model = { |
| | | id: $("#gid").val(), |
| | | json: $("#gjson").val() |
| | | }; |
| | | |
| | | return JSON.stringify(model); |
| | | } |
| | | |
| | | function ajax(url, type, data, dataType, contentType, fn) { |
| | | $.ajax({ |
| | | url: url, |
| | |
| | | </script> |
| | | </head> |
| | | <body> |
| | | <a href="http://localhost/JiangSu/Values/Get">Test</a> <br /> |
| | | <a href="http://localhost/JiangSu/Values/Get">Test</a> <br /> <br /> |
| | | |
| | | <a href="http://localhost/JiangSu/Op/SelectByPage?pageSize=10&pageIndex=1&name=">SelectByPage?pageSize=10&pageIndex=1&name=</a> <br /> |
| | | |
| | | <a href="http://localhost/JiangSu/Op/SelectById?id=1">SelectById?id=1</a> <br /> |
| | | |
| | | <a href="http://localhost/JiangSu/Op/DeleteByIds?ids=13&ids=14">DeleteByIds?ids=13&ids=14</a> <br /> |
| | | <a href="http://localhost/JiangSu/Op/DeleteByIds?ids=47&ids=14">DeleteByIds?ids=47&ids=14</a> <br /> <br /> |
| | | |
| | | <form> |
| | | id: <input id="id" name="id" value="3" /> <br /> |
| | |
| | | json: <input id="json" name="json" value="{}" /> <br /> |
| | | |
| | | <input type="button" value="Insert" onclick="Insert();" /> |
| | | <input type="button" value="UpdateById" onclick="UpdateById();" /><br /> |
| | | <input type="button" value="UpdateById" onclick="UpdateById();" /><br /> <br /> |
| | | |
| | | ç½æ ¼ï¼<a href="http://localhost/JiangSu/Grid/SelectAll">SelectAll</a> |
| | | <a href="http://localhost/JiangSu/Grid/SelectById?id=1">SelectById?id=1</a> |
| | | <a href="http://localhost/JiangSu/Grid/DeleteByIds?ids=12&ids=13">DeleteByIds?ids=12&ids=13</a> <br /> |
| | | |
| | | id: <input id="gid" value="12" /> <br /> |
| | | json: <textarea id="gjson" style="width: 1024px;height: 70px;">{ "type": "Feature", "properties": { "ID": "12", "NAME": "Area8", "REMARK": "" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 120.24179180542139, 32.005615085853492, 13.770 ], [ 120.24175240823769, 32.005610442630996, 13.770 ], [ 120.24173428512847, 32.005722106674966, 13.770 ], [ 120.24177368235908, 32.005726749902777, 13.770 ], [ 120.24179180542139, 32.005615085853492, 13.770 ] ] ] } }</textarea> <br /> |
| | | <input type="button" value="InsertGrid" onclick="InsertGrid();" /> |
| | | <input type="button" value="UpdateGridById" onclick="UpdateGridById();" /><br /> <br /> |
| | | </form> |
| | | </body> |
| | | </html> |