基本业务

master
bicijinlian 5 years ago
parent 2718a04dfe
commit d9c571a6fd

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
namespace SwaggerStudy.Models
{
/// <summary>
/// 性别
/// </summary>
public enum GenderEnum
{
/// <summary>
/// 女性
/// </summary>
[Description("原材料仓")]
Female=0,
/// <summary>
/// 男性
/// </summary>
[Description("原材料仓")]
Male=1,
/// <summary>
/// 保密
/// </summary>
[Description("原材料仓")]
Secrecy=3,
}
}

@ -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; }
}
}

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>

@ -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<Student> AllStudent = Ini();
public List<Student> 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<Student> Ini()
{
return new List<Student>()
{
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="武学理论家",
}
};
}
}
}

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SwaggerStudy.Models\SwaggerStudy.Models.csproj" />
</ItemGroup>
</Project>

@ -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

@ -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<StudentController> _logger;
private readonly StudentServer _studentServer;
public StudentController(ILogger<StudentController> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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;
}
}
}

@ -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; }
}
}

@ -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<StudentServer>();
}
// 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())

@ -4,4 +4,14 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SwaggerStudy.Common\SwaggerStudy.Common.csproj" />
<ProjectReference Include="..\SwaggerStudy.Models\SwaggerStudy.Models.csproj" />
<ProjectReference Include="..\SwaggerStudy.Services\SwaggerStudy.Services.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Swagger\" />
</ItemGroup>
</Project>

@ -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; }
}
}

@ -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<DateTime>
{
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"));
}
}
}
}
Loading…
Cancel
Save