EFCore数据操作
程序员文章站
2024-01-28 17:10:40
EFCore数据操作 ......
efcore数据操作
using system; using system.collections.generic; using system.linq; using system.threading.tasks; using microsoft.aspnetcore.mvc; using microsoft.entityframeworkcore; using typecho.enties.models; using typecho.iservice; using typecho.repository; using typecho.service; using typechocore1.jwt; using typechocore1.jwt.filter; using static typechocore1.jwt.itokenhelper; //默认的约定集将应用于程序集中的所有操作: [assembly: apiconventiontype(typeof(defaultapiconventions))] namespace typechocore1.controllers { [route("api/[controller]")] [apicontroller] public class testcontroller : controller { private readonly typechocontext _coredbcontext; private readonly itokenhelper tokenhelper = null; // itypechotestservice service = new typechoservice(); private readonly itypechotestservice service; //ioc依赖注入 public testcontroller(typechocontext coredbcontext,itokenhelper _tokenhelper,itypechotestservice service1) { _coredbcontext = coredbcontext; tokenhelper = _tokenhelper; service=service1; } /// <summary> /// 验证token /// </summary> /// <param name="tokenstr">token</param> /// <returns></returns> [httpget("valitoken")] public returnmodel valitoken(string tokenstr) { var ret = new returnmodel { tntoken = new tntoken() }; bool isvilidate = tokenhelper.valitoken(tokenstr); if(isvilidate) { ret.code = 200; ret.msg = "token验证成功"; ret.tntoken.tokenstr = tokenstr; } else { ret.code = 500; ret.msg = "token验证失败"; ret.tntoken.tokenstr = tokenstr; } return ret; } /// <summary> /// 验证token 带返回状态 /// </summary> /// <param name="tokenstr"></param> /// <returns></returns> [httpget("valitokenstate")] public returnmodel valitokenstate(string tokenstr) { var ret = new returnmodel { tntoken = new tntoken() }; string loginid = ""; tokentype tokentype = tokenhelper.valitokenstate(tokenstr, a => a["iss"] == "wyy" && a["aud"] == "everytestone", action => { loginid = action["loginid"]; }); if (tokentype == tokentype.fail) { ret.code = 202; ret.msg = "token验证失败"; return ret; } if (tokentype == tokentype.expired) { ret.code = 205; ret.msg = "token已经过期"; return ret; } //..............其他逻辑 var data = new list<dictionary<string, string>>(); var bb = new dictionary<string, string> { { "wyy", "123456" } }; data.add(bb); ret.code = 200; ret.msg = "访问成功!"; ret.data =data ; return ret; } /// <summary> /// 登录测试 /// </summary> /// <param name="user"></param> /// <returns></returns> [httppost("login")] public iactionresult login([frombody]logininput user) { var ret = new returnmodel(); try { if (string.isnullorwhitespace(user.username) || string.isnullorwhitespace(user.password)) { ret.code = 201; ret.msg = "用户名密码不能为空"; return notfound(); } //登录操作 我就没写了 || 假设登录成功 if (1 == 1) { dictionary<string, string> keyvaluepairs = new dictionary<string, string> { { "loginid", user.username } }; ret.code = 200; ret.msg = "登录成功"; ret.tntoken= tokenhelper.createtoken(keyvaluepairs); } } catch(exception ex) { ret.code = 500; ret.msg = "登录失败:"+ex.message; } return ok(ret); } /// <summary> /// 异步查询 /// [apiexplorersettings(ignoreapi = true)] 隐藏接口 /// </summary> /// <returns></returns> [servicefilter(typeof(tokenfilter))] [httpget("asygettest")] public async task<iactionresult> asygettest(string token) { var courses = _coredbcontext.typecho_test; return ok(await courses.tolistasync()); } /// <summary> /// 同步查询 /// </summary> /// <returns></returns> [httpget("gettest")] public iactionresult gettest() { return ok(service.gettest()); } /// <summary> /// 条件查询 /// </summary> /// <param name = "typecho_test">model</param> /// <returns></returns> [httpget("asygettestname")] public async task<actionresult<typecho_test>> asygettestname(int id,string name ) { actionresult<typecho_test> courses; try { if (id != 0) { courses = await task.run(()=>_coredbcontext.typecho_test.single(i=>i.id == id)); } else { courses = await task.run(()=>_coredbcontext.typecho_test.single(i=>i.name == name)); } return ok(courses); } catch (exception ex) { return ok(ex.message); } } /// <summary> /// 过滤查询 /// </summary> /// <returns></returns> [httpget("asygettestg")] public async task<iactionresult> asygettestg(string name ) { var courses = _coredbcontext.typecho_test .where(w => w.name.contains(name)); return ok( await courses.firstasync()); } /// <summary> /// 模糊查询 /// </summary> /// <returns></returns> [httpget("asygettestlink")] public async task<iactionresult> asygettestlink(string name ) { var courses = _coredbcontext.typecho_test .where(w=>ef.functions.like(w.name,"%")); return ok( await courses.tolistasync()); } /// <summary> /// 异步删除数据 /// </summary> /// <param name="id"></param> /// <returns></returns> [httpdelete("asydettestid")] public async task<iactionresult> asydettestid(int id) { var todoitem = await _coredbcontext.typecho_test.findasync(id); if (todoitem == null) { return notfound(); } _coredbcontext.typecho_test.remove(todoitem); await _coredbcontext.savechangesasync(); return nocontent(); } /// <summary> /// 同步删除数据 /// </summary> /// <param name="id"></param> /// <returns></returns> [httpdelete("dettestid")] public iactionresult dettestid(int id) { var todoitem = _coredbcontext.typecho_test.find(id); if (todoitem == null) { return notfound(); } _coredbcontext.typecho_test.remove(todoitem); _coredbcontext.savechanges(); return nocontent(); } /// <summary> /// 异步添加数据 /// </summary> /// <returns></returns> [httppost("asyinttest")] public async task<actionresult<typecho_test>> asyinttest(typecho_test test) { _coredbcontext.typecho_test.add(test); return ok(await _coredbcontext.savechangesasync()); } /// <summary> /// 同步添加数据 /// </summary> /// <returns></returns> [httppost("inttest")] public actionresult<typecho_test> inttest(typecho_test test) { _coredbcontext.typecho_test.add(test); return ok(_coredbcontext.savechanges()); } /// <summary> /// 异步更新数据 /// </summary> /// <param name="id"></param> /// <param name="test"></param> /// <returns></returns> [httpput("aysuptest")] public async task<iactionresult> aysuptest(int id, typecho_test test) { if (id != test.id) { return badrequest(); } _coredbcontext.entry(test).state = entitystate.modified; await _coredbcontext.savechangesasync(); return nocontent(); } /// <summary> /// 同步更新数据 /// </summary> /// <param name="id"></param> /// <param name="test"></param> /// <returns></returns> [httpput("uptest")] public iactionresult uptest(int id, typecho_test test) { if (id != test.id) { return badrequest(); } _coredbcontext.entry(test).state = entitystate.modified; _coredbcontext.savechanges(); return nocontent(); } } }