diff --git a/SwaggerStudy.Common/SwaggerStudy.Common.csproj b/SwaggerStudy.Common/SwaggerStudy.Common.csproj
new file mode 100644
index 0000000..9f5c4f4
--- /dev/null
+++ b/SwaggerStudy.Common/SwaggerStudy.Common.csproj
@@ -0,0 +1,7 @@
+
+
+
+ netstandard2.0
+
+
+
diff --git a/SwaggerStudy.Models/Enum.cs b/SwaggerStudy.Models/Enum.cs
new file mode 100644
index 0000000..c57c6da
--- /dev/null
+++ b/SwaggerStudy.Models/Enum.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Text;
+
+namespace SwaggerStudy.Models
+{
+ ///
+ /// 性别
+ ///
+ public enum GenderEnum
+ {
+ ///
+ /// 女性
+ ///
+ [Description("原材料仓")]
+ Female=0,
+
+ ///
+ /// 男性
+ ///
+ [Description("原材料仓")]
+ Male=1,
+
+ ///
+ /// 保密
+ ///
+ [Description("原材料仓")]
+ Secrecy=3,
+ }
+}
diff --git a/SwaggerStudy.Models/Student.cs b/SwaggerStudy.Models/Student.cs
new file mode 100644
index 0000000..0770d49
--- /dev/null
+++ b/SwaggerStudy.Models/Student.cs
@@ -0,0 +1,19 @@
+using System;
+
+namespace SwaggerStudy.Models
+{
+ public class Student
+ {
+ public int Id { get; set; }
+
+ public string Name { get; set; }
+
+ public int Age { get; set; }
+
+ public string Address { get; set; }
+
+ public GenderEnum Gender { get; set; }
+
+ public string School { get; set; }
+ }
+}
diff --git a/SwaggerStudy.Models/SwaggerStudy.Models.csproj b/SwaggerStudy.Models/SwaggerStudy.Models.csproj
new file mode 100644
index 0000000..9f5c4f4
--- /dev/null
+++ b/SwaggerStudy.Models/SwaggerStudy.Models.csproj
@@ -0,0 +1,7 @@
+
+
+
+ netstandard2.0
+
+
+
diff --git a/SwaggerStudy.Services/StudentServer.cs b/SwaggerStudy.Services/StudentServer.cs
new file mode 100644
index 0000000..2ce7337
--- /dev/null
+++ b/SwaggerStudy.Services/StudentServer.cs
@@ -0,0 +1,130 @@
+using SwaggerStudy.Models;
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace SwaggerStudy.Services
+{
+ public class StudentServer
+ {
+ public static List AllStudent = Ini();
+
+ public List GetAll()
+ {
+ return StudentServer.AllStudent;
+ }
+
+ public Student Get(int studentId)
+ {
+ return StudentServer.AllStudent.FirstOrDefault(s => s.Id == studentId);
+ }
+
+ public Student Get(string studentName)
+ {
+ return StudentServer.AllStudent.FirstOrDefault(s => s.Name == studentName);
+ }
+
+ public bool Add(Student student)
+ {
+ var nextId = StudentServer.AllStudent.Count() + 1;
+ student.Id = nextId;
+
+ StudentServer.AllStudent.Add(student);
+ return true;
+ }
+
+ public bool Update(Student student)
+ {
+ var query = StudentServer.AllStudent.FirstOrDefault(s => s.Id == student.Id);
+ if (query==null)
+ {
+ return false;
+ }
+
+ query.Name = student.Name;
+ query.Age = student.Age;
+ query.Gender = student.Gender;
+ query.Address = student.Address;
+ query.School = student.School;
+
+ return true;
+ }
+
+ public bool Delete(int studentId)
+ {
+ var query = StudentServer.AllStudent.FirstOrDefault(s => s.Id == studentId);
+ if (query == null)
+ {
+ return false;
+ }
+ else
+ {
+ StudentServer.AllStudent.Remove(query);
+
+ return true;
+ }
+ }
+
+ private static List Ini()
+ {
+ return new List()
+ {
+ new Student()
+ {
+ Id=1,
+ Name="乔峰",
+ Age=25,
+ Address="少室山",
+ Gender=GenderEnum.Male,
+ School="丐帮",
+ },
+ new Student()
+ {
+ Id=2,
+ Name="虚竹",
+ Age=24,
+ Address="少林寺",
+ Gender=GenderEnum.Male,
+ School="逍遥派",
+ },
+ new Student()
+ {
+ Id=3,
+ Name="段誉",
+ Age=22,
+ Address="大理",
+ Gender=GenderEnum.Male,
+ School="无量山琅环福地",
+ },
+ new Student()
+ {
+ Id=4,
+ Name="阿朱",
+ Age=19,
+ Address="听香水榭",
+ Gender=GenderEnum.Female,
+ School="无",
+ },
+ new Student()
+ {
+ Id=5,
+ Name="阿紫",
+ Age=18,
+ Address="春秋门",
+ Gender=GenderEnum.Female,
+ School="星宿派",
+ },
+ new Student()
+ {
+ Id=6,
+ Name="王语嫣",
+ Age=17,
+ Address="曼陀山庄",
+ Gender=GenderEnum.Female,
+ School="武学理论家",
+ }
+ };
+ }
+ }
+}
diff --git a/SwaggerStudy.Services/SwaggerStudy.Services.csproj b/SwaggerStudy.Services/SwaggerStudy.Services.csproj
new file mode 100644
index 0000000..4edeca6
--- /dev/null
+++ b/SwaggerStudy.Services/SwaggerStudy.Services.csproj
@@ -0,0 +1,11 @@
+
+
+
+ netstandard2.0
+
+
+
+
+
+
+
diff --git a/SwaggerStudy.sln b/SwaggerStudy.sln
index fd25050..6913dc6 100644
--- a/SwaggerStudy.sln
+++ b/SwaggerStudy.sln
@@ -3,7 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30711.63
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwaggerStudy", "SwaggerStudy\SwaggerStudy.csproj", "{6322570B-CB1E-4C88-A1C8-903114CBB926}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SwaggerStudy", "SwaggerStudy\SwaggerStudy.csproj", "{6322570B-CB1E-4C88-A1C8-903114CBB926}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwaggerStudy.Models", "SwaggerStudy.Models\SwaggerStudy.Models.csproj", "{1BDCB9D8-5924-4A8E-BD07-78B89EB5D075}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwaggerStudy.Services", "SwaggerStudy.Services\SwaggerStudy.Services.csproj", "{529BE2CD-269D-4FF5-A82F-D037C6EE1F30}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwaggerStudy.Common", "SwaggerStudy.Common\SwaggerStudy.Common.csproj", "{C22ABA5C-7B61-4A5B-8C8C-9E6A0B65C470}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +21,18 @@ Global
{6322570B-CB1E-4C88-A1C8-903114CBB926}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6322570B-CB1E-4C88-A1C8-903114CBB926}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6322570B-CB1E-4C88-A1C8-903114CBB926}.Release|Any CPU.Build.0 = Release|Any CPU
+ {1BDCB9D8-5924-4A8E-BD07-78B89EB5D075}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {1BDCB9D8-5924-4A8E-BD07-78B89EB5D075}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {1BDCB9D8-5924-4A8E-BD07-78B89EB5D075}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {1BDCB9D8-5924-4A8E-BD07-78B89EB5D075}.Release|Any CPU.Build.0 = Release|Any CPU
+ {529BE2CD-269D-4FF5-A82F-D037C6EE1F30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {529BE2CD-269D-4FF5-A82F-D037C6EE1F30}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {529BE2CD-269D-4FF5-A82F-D037C6EE1F30}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {529BE2CD-269D-4FF5-A82F-D037C6EE1F30}.Release|Any CPU.Build.0 = Release|Any CPU
+ {C22ABA5C-7B61-4A5B-8C8C-9E6A0B65C470}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {C22ABA5C-7B61-4A5B-8C8C-9E6A0B65C470}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {C22ABA5C-7B61-4A5B-8C8C-9E6A0B65C470}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {C22ABA5C-7B61-4A5B-8C8C-9E6A0B65C470}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/SwaggerStudy/Controllers/StudentController.cs b/SwaggerStudy/Controllers/StudentController.cs
new file mode 100644
index 0000000..8bf11e8
--- /dev/null
+++ b/SwaggerStudy/Controllers/StudentController.cs
@@ -0,0 +1,322 @@
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Logging;
+
+using SwaggerStudy.Models;
+using SwaggerStudy.Services;
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace SwaggerStudy.Controllers
+{
+ [Route("api/[controller]/[action]")]
+ [ApiController]
+ public class StudentController : ControllerBase
+ {
+ private readonly ILogger _logger;
+
+ private readonly StudentServer _studentServer;
+
+ public StudentController(ILogger logger, StudentServer studentServer)
+ {
+ _logger = logger;
+ _studentServer = studentServer;
+ }
+
+ [HttpGet]
+ public ResultBase GetAll()
+ {
+ var apiResult = new ResultBase()
+ {
+ Code = 0,
+ Message="获取所有学生",
+ Data = _studentServer.GetAll(),
+ };
+
+ return apiResult;
+ }
+
+ [HttpGet]
+ public async Task GetAllAsync()
+ {
+ var apiResult = new ResultBase()
+ {
+ Code = 0,
+ Message = "获取所有学生成功",
+ Data = _studentServer.GetAll(),
+ };
+
+ return await Task.FromResult(Ok(apiResult));
+ }
+
+ [HttpGet]
+ public IActionResult Get(int studentId)
+ {
+ var apiResult = new ResultBase()
+ {
+ Code = 0,
+ Message = "操作成功",
+ Data = _studentServer.Get(studentId),
+ };
+
+ return Ok(apiResult);
+ }
+
+ [HttpGet]
+ public async Task GetAsync(int studentId)
+ {
+ var apiResult = new ResultBase()
+ {
+ Code = 0,
+ Message = "操作成功",
+ Data = _studentServer.Get(studentId),
+ };
+
+ return await Task.FromResult(Ok(apiResult));
+ }
+
+ [HttpGet]
+ public IActionResult GetByName(string studentName)
+ {
+ var apiResult = new ResultBase()
+ {
+ Code = 0,
+ Message = "操作成功",
+ Data = _studentServer.Get(studentName),
+ };
+
+ return Ok(apiResult);
+ }
+
+ [HttpGet]
+ public async Task GetByNameAsync(string studentName)
+ {
+ var apiResult = new ResultBase()
+ {
+ Code = 0,
+ Message = "操作成功",
+ Data = _studentServer.Get(studentName),
+ };
+
+ return await Task.FromResult(Ok(apiResult));
+ }
+
+
+ [HttpGet]
+ public IActionResult Add(StudentVModel vm)
+ {
+ var apiResult = new ResultBase()
+ {
+ Code = 0,
+ Message = "",
+ };
+
+ if (!ModelState.IsValid)
+ {
+ apiResult.Code = -1;
+ apiResult.Message = "模型验证错误";
+ return BadRequest(apiResult);
+ }
+
+ var addResult = _studentServer.Add(ModelConvert(vm));
+ if (addResult)
+ {
+ apiResult.Code = 0;
+ apiResult.Message = "添加成功";
+ apiResult.Data = vm;
+ }
+ else
+ {
+ apiResult.Code = -2;
+ apiResult.Message = "添加失败";
+ apiResult.Data = null;
+ }
+
+ return Ok(apiResult);
+ }
+
+ [HttpGet]
+ public async Task AddAsync(StudentVModel vm)
+ {
+ var apiResult = new ResultBase()
+ {
+ Code = 0,
+ Message = "",
+ };
+
+ if (!ModelState.IsValid)
+ {
+ apiResult.Code = -1;
+ apiResult.Message = "模型验证错误";
+ return await Task.FromResult(BadRequest(apiResult));
+ }
+
+ var addResult = _studentServer.Add(ModelConvert(vm));
+ if (addResult)
+ {
+ apiResult.Code = 0;
+ apiResult.Message = "添加成功";
+ apiResult.Data = vm;
+ }
+ else
+ {
+ apiResult.Code = -2;
+ apiResult.Message = "添加失败";
+ apiResult.Data = null;
+ }
+
+ return await Task.FromResult(new JsonResult(apiResult));
+ }
+
+ [HttpGet]
+ public IActionResult Update(StudentVModel vm)
+ {
+ var apiResult = new ResultBase()
+ {
+ Code = 0,
+ Message = "",
+ };
+
+ if (!ModelState.IsValid)
+ {
+ apiResult.Code = -1;
+ apiResult.Message = "模型验证错误";
+ return BadRequest(apiResult);
+ }
+
+ var updateResult = _studentServer.Update(ModelConvert(vm));
+ if (updateResult)
+ {
+ apiResult.Code = 0;
+ apiResult.Message = "更新成功";
+ apiResult.Data = vm;
+ }
+ else
+ {
+ apiResult.Code = -2;
+ apiResult.Message = "更新失败";
+ apiResult.Data = null;
+ }
+
+ return Ok(apiResult);
+ }
+
+ [HttpGet]
+ public async Task UpdateAsync(StudentVModel vm)
+ {
+ var apiResult = new ResultBase()
+ {
+ Code = 0,
+ Message = "",
+ };
+
+ if (!ModelState.IsValid)
+ {
+ apiResult.Code = -1;
+ apiResult.Message = "模型验证错误";
+ return await Task.FromResult(BadRequest(apiResult));
+ }
+
+ var updateResult = _studentServer.Update(ModelConvert(vm));
+ if (updateResult)
+ {
+ apiResult.Code = 0;
+ apiResult.Message = "更新成功";
+ apiResult.Data = vm;
+ }
+ else
+ {
+ apiResult.Code = -2;
+ apiResult.Message = "更新失败";
+ apiResult.Data = null;
+ }
+
+ return await Task.FromResult(new JsonResult(apiResult));
+ }
+
+ [HttpGet]
+ public IActionResult Delete(int studentId)
+ {
+ var apiResult = new ResultBase()
+ {
+ Code = 0,
+ Message = "",
+ };
+
+ var operationResults = _studentServer.Delete(studentId);
+ if (operationResults)
+ {
+ apiResult.Code = 0;
+ apiResult.Message = "删除成功";
+ apiResult.Data = true;
+ }
+ else
+ {
+ apiResult.Code = -2;
+ apiResult.Message = "删除失败";
+ apiResult.Data = false;
+ }
+
+ return Ok(apiResult);
+ }
+
+ [HttpGet]
+ public async Task DeleteAsync(int studentId)
+ {
+ var apiResult = new ResultBase()
+ {
+ Code = 0,
+ Message = "",
+ };
+
+ var operationResults = _studentServer.Delete(studentId);
+ if (operationResults)
+ {
+ apiResult.Code = 0;
+ apiResult.Message = "删除成功";
+ apiResult.Data = true;
+ }
+ else
+ {
+ apiResult.Code = -2;
+ apiResult.Message = "删除失败";
+ apiResult.Data = false;
+ }
+
+ return await Task.FromResult(new JsonResult(apiResult));
+ }
+
+
+
+ private StudentVModel ModelConvert(Student student)
+ {
+ var vm = new StudentVModel()
+ {
+ Id = student.Id,
+ Name = student.Name,
+ Age = student.Age,
+ Address = student.Address,
+ School = student.School,
+ };
+
+ return vm;
+ }
+
+ private Student ModelConvert(StudentVModel vm)
+ {
+ var model = new Student()
+ {
+ Id = vm.Id,
+ Name = vm.Name,
+ Age = vm.Age,
+ Address = vm.Address,
+ School = vm.School,
+ };
+
+ return model;
+ }
+ }
+}
diff --git a/SwaggerStudy/Documents/学习.md b/SwaggerStudy/Documents/使用.md
similarity index 100%
rename from SwaggerStudy/Documents/学习.md
rename to SwaggerStudy/Documents/使用.md
diff --git a/SwaggerStudy/ResultBase.cs b/SwaggerStudy/ResultBase.cs
new file mode 100644
index 0000000..26e9073
--- /dev/null
+++ b/SwaggerStudy/ResultBase.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace SwaggerStudy
+{
+ public class ResultBase
+ {
+ public static ResultBase Default => new ResultBase() { Code=-1,Message=string.Empty};
+
+ public int Code { get; set; } = 0;
+
+ public string Message { get; set; } = string.Empty;
+
+ public dynamic Data { get; set; }
+ }
+}
diff --git a/SwaggerStudy/Startup.cs b/SwaggerStudy/Startup.cs
index 9ad5115..de62501 100644
--- a/SwaggerStudy/Startup.cs
+++ b/SwaggerStudy/Startup.cs
@@ -1,3 +1,8 @@
+using System;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Collections.Generic;
+
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
@@ -6,10 +11,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading.Tasks;
+using SwaggerStudy.Services;
namespace SwaggerStudy
{
@@ -22,14 +24,18 @@ namespace SwaggerStudy
public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
-
- services.AddControllers();
+ services.AddControllers()
+ .AddJsonOptions(jsonOption=>
+ {
+ jsonOption.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
+ jsonOption.JsonSerializerOptions.Encoder= System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All);
+ });
+
+ services.AddTransient();
}
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
diff --git a/SwaggerStudy/SwaggerStudy.csproj b/SwaggerStudy/SwaggerStudy.csproj
index 842a770..7b21126 100644
--- a/SwaggerStudy/SwaggerStudy.csproj
+++ b/SwaggerStudy/SwaggerStudy.csproj
@@ -4,4 +4,14 @@
net5.0
+
+
+
+
+
+
+
+
+
+
diff --git a/SwaggerStudy/VModels/StudentVM.cs b/SwaggerStudy/VModels/StudentVM.cs
new file mode 100644
index 0000000..70dff83
--- /dev/null
+++ b/SwaggerStudy/VModels/StudentVM.cs
@@ -0,0 +1,24 @@
+using SwaggerStudy.Models;
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace SwaggerStudy
+{
+ public class StudentVModel
+ {
+ public int Id { get; set; }
+
+ public string Name { get; set; }
+
+ public int Age { get; set; }
+
+ public string Address { get; set; }
+
+ public GenderEnum Gender { get; set; }
+
+ public string School { get; set; }
+ }
+}
diff --git a/SwaggerStudy/WebApp/DatetimeJsonConverter.cs b/SwaggerStudy/WebApp/DatetimeJsonConverter.cs
new file mode 100644
index 0000000..06b20f4
--- /dev/null
+++ b/SwaggerStudy/WebApp/DatetimeJsonConverter.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+using System.Threading.Tasks;
+
+namespace SwaggerStudy
+{
+ public class DatetimeJsonConverter:JsonConverter
+ {
+ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+ {
+ if (reader.TokenType == JsonTokenType.String)
+ {
+ if (DateTime.TryParse(reader.GetString(), out DateTime date))
+ return date;
+ }
+ return reader.GetDateTime();
+ }
+
+ public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
+ {
+ if (value.Hour == 0 && value.Minute == 0 && value.Second == 0)
+ {
+ writer.WriteStringValue(value.ToString("yyyy-MM-dd"));
+ }
+ else
+ {
+ writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
+ }
+ }
+ }
+}