From d9c6a758c8c0a49e9e6ecfa9f714465917842977 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Mon, 16 Nov 2020 00:58:33 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=AE=8C=E6=88=90=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=202=E3=80=81api=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AccessStudy.Core/IStudentIDal.cs | 36 ++++ AccessStudy.Core/Student.cs | 2 +- AccessStudy.Core/StudentOdbcDal.cs | 142 ++++++++++++++ .../{StudentDal.cs => StudentOledbDal.cs} | 2 +- AccessStudy.CoreTest/StudentDalTest.cs | 4 +- AccessStudy.WebApi/AccessStudy.WebApi.csproj | 11 ++ AccessStudy.WebApi/ApiBase/ResultBase.cs | 16 ++ .../Controllers/StudentController.cs | 176 ++++++++++++++++++ .../Controllers/ValuesController.cs | 33 ++++ .../Controllers/WeatherForecastController.cs | 40 ---- .../Properties/launchSettings.json | 2 +- AccessStudy.WebApi/Startup.cs | 28 +++ AccessStudy.WebApi/WeatherForecast.cs | 15 -- 13 files changed, 447 insertions(+), 60 deletions(-) create mode 100644 AccessStudy.Core/IStudentIDal.cs create mode 100644 AccessStudy.Core/StudentOdbcDal.cs rename AccessStudy.Core/{StudentDal.cs => StudentOledbDal.cs} (98%) create mode 100644 AccessStudy.WebApi/ApiBase/ResultBase.cs create mode 100644 AccessStudy.WebApi/Controllers/StudentController.cs create mode 100644 AccessStudy.WebApi/Controllers/ValuesController.cs delete mode 100644 AccessStudy.WebApi/Controllers/WeatherForecastController.cs delete mode 100644 AccessStudy.WebApi/WeatherForecast.cs diff --git a/AccessStudy.Core/IStudentIDal.cs b/AccessStudy.Core/IStudentIDal.cs new file mode 100644 index 0000000..f8c6262 --- /dev/null +++ b/AccessStudy.Core/IStudentIDal.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace AccessStudy.Core +{ + public interface IStudentIDal + { + List GetAll(); + + /// + /// 按标识查询学生 + /// + Student Get(int studentId); + + /// + /// 按名称查询学生 + /// + Student Get(string studentName); + + /// + /// 添加学生 + /// + bool Add(Student student); + + /// + /// 更新学生 + /// + bool Update(Student student); + + /// + /// 删除学生 + /// + bool Delete(int studentId); + } +} diff --git a/AccessStudy.Core/Student.cs b/AccessStudy.Core/Student.cs index d48f97f..b48ce54 100644 --- a/AccessStudy.Core/Student.cs +++ b/AccessStudy.Core/Student.cs @@ -5,7 +5,7 @@ using System.Text; namespace AccessStudy.Core { /// - /// 学生 + /// 学生实体类 /// public class Student { diff --git a/AccessStudy.Core/StudentOdbcDal.cs b/AccessStudy.Core/StudentOdbcDal.cs new file mode 100644 index 0000000..f86b8c4 --- /dev/null +++ b/AccessStudy.Core/StudentOdbcDal.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Odbc; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace AccessStudy.Core +{ + public class StudentOdbcDal:IStudentIDal + { + private OdbcUtil dbUtil = new OdbcUtil(); + + /// + /// 获取所有学生 + /// + /// + public List GetAll() + { + var dataTable = dbUtil.GetDataTable("Student"); + List students = DataTable2Model(dataTable); + + return students; + } + + /// + /// 按标识查询学生 + /// + public Student Get(int studentId) + { + var querySql = "select * from Student where Id=?"; + var paras = new List() + { + new OdbcParameter("studentId",studentId) + }; + var dataTable = dbUtil.GetDataTable(querySql, paras); + + return DataTable2Model(dataTable).FirstOrDefault(); + } + + /// + /// 按名称查询学生 + /// + public Student Get(string studentName) + { + var querySql = "select * from Student where Name=?"; + var paras = new List() + { + new OdbcParameter("studentName",studentName) + }; + var dataTable = dbUtil.GetDataTable(querySql, paras); + + return DataTable2Model(dataTable).FirstOrDefault(); + } + + /// + /// 添加学生 + /// + public bool Add(Student student) + { + var querySql = "INSERT INTO `Student` (Name,Age,Address,School) VALUES(?,?,?,?); "; + var paras = new List() + { + new OdbcParameter("@Name",student.Name), + new OdbcParameter("@Age",student.Age), + new OdbcParameter("@Address",student.Address), + new OdbcParameter("@School",student.School), + }; + + var effectRow = dbUtil.ExecuteNonQuery(querySql, paras); + + return effectRow > 0; + } + + /// + /// 更新学生 + /// + public bool Update(Student student) + { + var querySql = "Update Student Set Name=?,Age=?,Address=?,School=? where Id=?;"; + var paras = new List() + { + new OdbcParameter("Name",student.Name), + new OdbcParameter("Age",student.Age), + new OdbcParameter("Address",student.Address), + new OdbcParameter("School",student.School), + new OdbcParameter("Id",student.Id), + }; + + var effectRow = dbUtil.ExecuteNonQuery(querySql, paras); + + return effectRow > 0; + } + + /// + /// 删除学生 + /// + public bool Delete(int studentId) + { + var querySql = "DELETE FROM Student WHERE Id = ?; "; + var paras = new List() + { + new OdbcParameter("Id",studentId), + }; + + var effectRow = dbUtil.ExecuteNonQuery(querySql, paras); + + return effectRow > 0; + } + + /// + /// DataTable转实体列表 + /// + private List DataTable2Model(DataTable dTable) + { + List students = new List(); + + if (dTable==null) + { + return students; + } + + foreach (DataRow row in dTable.Rows) + { + var student = new Student() + { + Id = (int)row["Id"], + Name = row["Name"]?.ToString() ?? "", + Age = (int)row["Age"], + Address = row["Address"]?.ToString() ?? "", + School = row["School"]?.ToString() ?? "", + }; + + students.Add(student); + } + + return students; + } + + } +} diff --git a/AccessStudy.Core/StudentDal.cs b/AccessStudy.Core/StudentOledbDal.cs similarity index 98% rename from AccessStudy.Core/StudentDal.cs rename to AccessStudy.Core/StudentOledbDal.cs index 89a26e1..b4b34a0 100644 --- a/AccessStudy.Core/StudentDal.cs +++ b/AccessStudy.Core/StudentOledbDal.cs @@ -8,7 +8,7 @@ using System.Text; namespace AccessStudy.Core { - public class StudentDal + public class StudentOledbDal:IStudentIDal { private OledbUtil dbUtil = new OledbUtil(); diff --git a/AccessStudy.CoreTest/StudentDalTest.cs b/AccessStudy.CoreTest/StudentDalTest.cs index b370c51..b65dcd3 100644 --- a/AccessStudy.CoreTest/StudentDalTest.cs +++ b/AccessStudy.CoreTest/StudentDalTest.cs @@ -12,10 +12,10 @@ namespace AccessStudy.CoreTest { public class StudentDalTest:IDisposable { - private readonly StudentDal _dal; + private readonly StudentOledbDal _dal; public StudentDalTest() { - _dal = new StudentDal(); + _dal = new StudentOledbDal(); } [Fact] diff --git a/AccessStudy.WebApi/AccessStudy.WebApi.csproj b/AccessStudy.WebApi/AccessStudy.WebApi.csproj index d12c450..66c232d 100644 --- a/AccessStudy.WebApi/AccessStudy.WebApi.csproj +++ b/AccessStudy.WebApi/AccessStudy.WebApi.csproj @@ -2,7 +2,18 @@ netcoreapp3.1 + true + $(NoWarn);1591 + + + + + + + + + diff --git a/AccessStudy.WebApi/ApiBase/ResultBase.cs b/AccessStudy.WebApi/ApiBase/ResultBase.cs new file mode 100644 index 0000000..86e90bd --- /dev/null +++ b/AccessStudy.WebApi/ApiBase/ResultBase.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace AccessStudy.WebApi +{ + public class ResultBase + { + public int Code { get; set; } + + public string Message { get; set; } + + public dynamic Data { get; set; } + } +} diff --git a/AccessStudy.WebApi/Controllers/StudentController.cs b/AccessStudy.WebApi/Controllers/StudentController.cs new file mode 100644 index 0000000..e1cead7 --- /dev/null +++ b/AccessStudy.WebApi/Controllers/StudentController.cs @@ -0,0 +1,176 @@ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +using AccessStudy.Core; + +namespace AccessStudy.WebApi.Controllers +{ + /// + /// 学生管理接口 + /// + [ApiController] + [Route("api/[controller]/[action]")] + public class StudentController : ControllerBase + { + private readonly ILogger _logger; + private readonly IStudentIDal _studentDal; + + /// + /// 构造 + /// + public StudentController(ILogger logger, IStudentIDal studentIDal) + { + _logger = logger; + _studentDal = studentIDal; + } + + /// + /// 获取所有学生 + /// + /// + [HttpGet] + public ResultBase GetAll() + { + var result = new ResultBase() + { + Code = 0, + Message = "", + Data = _studentDal.GetAll() + }; + + return result; + } + + /// + /// 按标识获取学生 + /// + /// 标识号 + /// + [HttpGet] + public ResultBase GetById(int studentId) + { + var result = new ResultBase() + { + Code = 0, + Message = "", + Data = _studentDal.Get(studentId) + }; + + return result; + } + + /// + /// 按姓名获取学生 + /// + /// 姓名 + /// + /// 请求例子: + /// GET /api/Student/GetByName?studentName=王高峰 + /// + /// + [HttpGet] + public ResultBase GetByName(string studentName) + { + var result = new ResultBase() + { + Code = 0, + Message = "", + Data = _studentDal.Get(studentName) + }; + + return result; + } + + /// + /// 添加学生 + /// + /// 学生信息 + /// + [HttpPost] + public ResultBase Add(Student student) + { + var result = new ResultBase() + { + Code = 0, + Message = "", + Data = null + }; + + if (_studentDal.Add(student)) + { + result.Message = "添加成功"; + result.Data = true; + } + else + { + result.Message = "添加失败"; + result.Data = false; + } + return result; + } + + /// + /// 修改学生 + /// + /// 修改学生信息 + /// + [HttpPost] + public ResultBase Update(Student student) + { + var result = new ResultBase() + { + Code = 0, + Message = "", + Data = null + }; + + if (_studentDal.Update(student)) + { + result.Message = "更新成功"; + result.Data = true; + } + else + { + result.Message = "更新失败"; + result.Data = false; + } + return result; + } + + /// + /// 删除学生 + /// + /// 学生标识 + /// + [HttpPost] + public ResultBase Delete(int studentId) + { + var result = new ResultBase() + { + Code = 0, + Message = "", + Data = null + }; + + if (_studentDal.Delete(studentId)) + { + result.Message = "删除成功"; + result.Data = true; + } + else + { + result.Message = "删除失败"; + result.Data = false; + } + + return result; + } + } +} diff --git a/AccessStudy.WebApi/Controllers/ValuesController.cs b/AccessStudy.WebApi/Controllers/ValuesController.cs new file mode 100644 index 0000000..d072629 --- /dev/null +++ b/AccessStudy.WebApi/Controllers/ValuesController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace AccessStudy.WebApi.Controllers +{ + /// + /// 示例控制器 + /// + [Route("api/[controller]")] + [ApiController] + public class ValuesController : ControllerBase + { + + /// + /// 默认请求 + /// + /// + [HttpGet] + public IActionResult Get() + { + var result = new { Code=0,Message="请求成功"}; + + return new JsonResult(result); + } + } + + +} diff --git a/AccessStudy.WebApi/Controllers/WeatherForecastController.cs b/AccessStudy.WebApi/Controllers/WeatherForecastController.cs deleted file mode 100644 index 03c9242..0000000 --- a/AccessStudy.WebApi/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace AccessStudy.WebApi.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet] - public IEnumerable Get() - { - var rng = new Random(); - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateTime.Now.AddDays(index), - TemperatureC = rng.Next(-20, 55), - Summary = Summaries[rng.Next(Summaries.Length)] - }) - .ToArray(); - } - } -} diff --git a/AccessStudy.WebApi/Properties/launchSettings.json b/AccessStudy.WebApi/Properties/launchSettings.json index 509bb83..075cb34 100644 --- a/AccessStudy.WebApi/Properties/launchSettings.json +++ b/AccessStudy.WebApi/Properties/launchSettings.json @@ -20,7 +20,7 @@ "AccessStudy.WebApi": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "weatherforecast", + "launchUrl": "swagger", "applicationUrl": "http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/AccessStudy.WebApi/Startup.cs b/AccessStudy.WebApi/Startup.cs index 8ad109c..3213654 100644 --- a/AccessStudy.WebApi/Startup.cs +++ b/AccessStudy.WebApi/Startup.cs @@ -11,6 +11,9 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using AccessStudy.Core; +using System.Reflection; + namespace AccessStudy.WebApi { public class Startup @@ -26,6 +29,25 @@ namespace AccessStudy.WebApi public void ConfigureServices(IServiceCollection services) { services.AddControllers(); + + //实现Oledb和ODBC的切换 + //services.AddScoped(); + services.AddScoped(); + + services.AddSwaggerGen(option => + { + option.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo() { Title = "API", Version = "v1" }); + + // Set the comments path for the Swagger JSON and UI. + var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; + var xmlPath = System.IO.Path.Combine(AppContext.BaseDirectory, xmlFile); + option.IncludeXmlComments(xmlPath); + }); + + //services.ConfigureSwaggerGen(option => + //{ + + //}); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -36,6 +58,12 @@ namespace AccessStudy.WebApi app.UseDeveloperExceptionPage(); } + app.UseSwagger(); + app.UseSwaggerUI(option => + { + option.SwaggerEndpoint("/swagger/v1/swagger.json", "AccessStudy WebApi v1"); + }); + app.UseRouting(); app.UseAuthorization(); diff --git a/AccessStudy.WebApi/WeatherForecast.cs b/AccessStudy.WebApi/WeatherForecast.cs deleted file mode 100644 index 775e0c0..0000000 --- a/AccessStudy.WebApi/WeatherForecast.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace AccessStudy.WebApi -{ - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string Summary { get; set; } - } -}