mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
add RESTful demo
This commit is contained in:
parent
1c0dedb87e
commit
45bc234160
71
FreeSql.RESTful.Demo/Controllers/EntityController.cs
Normal file
71
FreeSql.RESTful.Demo/Controllers/EntityController.cs
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Linq;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using NpgsqlTypes;
|
||||||
|
using Npgsql.LegacyPostgis;
|
||||||
|
using FreeSql.RESTful.Demo.Entity;
|
||||||
|
|
||||||
|
namespace FreeSql.RESTful.Demo.Controllers {
|
||||||
|
|
||||||
|
|
||||||
|
[Route("restapi/[controller]")]
|
||||||
|
public class SongsController : Controller {
|
||||||
|
|
||||||
|
IFreeSql _fsql;
|
||||||
|
|
||||||
|
public SongsController(IFreeSql fsql) {
|
||||||
|
_fsql = fsql;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public Task<List<Song>> GetItems([FromQuery] string key, [FromQuery] int page = 1, [FromQuery] int limit = 20) {
|
||||||
|
return _fsql.Select<Song>().WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(page, limit).ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public Task<Song> GetItem([FromRoute] int id) {
|
||||||
|
return _fsql.Select<Song>().Where(a => a.Id == id).ToOneAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ModelSong {
|
||||||
|
public string title { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost, ProducesResponseType(201)]
|
||||||
|
async public Task<Song> Create([FromBody] ModelSong model) {
|
||||||
|
var ret = await _fsql.Insert<Song>().AppendData(new Song { Title = model.title }).ExecuteInsertedAsync();
|
||||||
|
return ret.FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
async public Task<Song> Update([FromRoute] int id, [FromBody] ModelSong model) {
|
||||||
|
var ret = await _fsql.Update<Song>().SetSource(new Song { Id = id, Title = model.title }).ExecuteUpdatedAsync();
|
||||||
|
return ret.FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPatch("{id}")]
|
||||||
|
async public Task<Song> UpdateDiy([FromRoute] int id, [FromForm] string title) {
|
||||||
|
var up = _fsql.Update<Song>().Where(a => a.Id == id);
|
||||||
|
if (!string.IsNullOrEmpty(title)) up.Set(a => a.Title, title);
|
||||||
|
var ret = await up.ExecuteUpdatedAsync();
|
||||||
|
return ret.FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("{id}"), ProducesResponseType(204)]
|
||||||
|
async public Task<Song> Delete([FromRoute] int id) {
|
||||||
|
var ret = await _fsql.Delete<Song>().Where(a => a.Id == id).ExecuteDeletedAsync();
|
||||||
|
return ret.FirstOrDefault();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
FreeSql.RESTful.Demo/Entity/Song.cs
Normal file
11
FreeSql.RESTful.Demo/Entity/Song.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FreeSql.RESTful.Demo.Entity {
|
||||||
|
public class Song {
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
}
|
||||||
|
}
|
19
FreeSql.RESTful.Demo/FreeSql.RESTful.Demo.csproj
Normal file
19
FreeSql.RESTful.Demo/FreeSql.RESTful.Demo.csproj
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||||
|
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" />
|
||||||
|
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
24
FreeSql.RESTful.Demo/Program.cs
Normal file
24
FreeSql.RESTful.Demo/Program.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace FreeSql.RESTful.Demo
|
||||||
|
{
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
CreateWebHostBuilder(args).Build().Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||||
|
WebHost.CreateDefaultBuilder(args)
|
||||||
|
.UseStartup<Startup>();
|
||||||
|
}
|
||||||
|
}
|
69
FreeSql.RESTful.Demo/Startup.cs
Normal file
69
FreeSql.RESTful.Demo/Startup.cs
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.HttpsPolicy;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using Swashbuckle.AspNetCore.Swagger;
|
||||||
|
|
||||||
|
namespace FreeSql.RESTful.Demo
|
||||||
|
{
|
||||||
|
public class Startup
|
||||||
|
{
|
||||||
|
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
|
||||||
|
{
|
||||||
|
Configuration = configuration;
|
||||||
|
|
||||||
|
Fsql = new FreeSql.FreeSqlBuilder()
|
||||||
|
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Attachs=xxxtb.db;Pooling=true;Max Pool Size=10")
|
||||||
|
.UseLogger(loggerFactory.CreateLogger<IFreeSql>())
|
||||||
|
.UseAutoSyncStructure(true)
|
||||||
|
.Build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfiguration Configuration { get; }
|
||||||
|
public IFreeSql Fsql { get; }
|
||||||
|
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IFreeSql>(Fsql);
|
||||||
|
|
||||||
|
services.AddMvc();
|
||||||
|
services.AddSwaggerGen(options =>
|
||||||
|
{
|
||||||
|
options.SwaggerDoc("v1", new Info {
|
||||||
|
Version = "v1",
|
||||||
|
Title = "FreeSql.RESTful API"
|
||||||
|
});
|
||||||
|
//options.IncludeXmlComments(xmlPath);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||||
|
{
|
||||||
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||||
|
Console.OutputEncoding = Encoding.GetEncoding("GB2312");
|
||||||
|
Console.InputEncoding = Encoding.GetEncoding("GB2312");
|
||||||
|
|
||||||
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
||||||
|
loggerFactory.AddDebug();
|
||||||
|
|
||||||
|
app.UseHttpMethodOverride(new HttpMethodOverrideOptions { FormFieldName = "X-Http-Method-Override" });
|
||||||
|
app.UseDeveloperExceptionPage();
|
||||||
|
app.UseMvc();
|
||||||
|
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI(c =>
|
||||||
|
{
|
||||||
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "FreeSql.RESTful API V1");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
FreeSql.RESTful.Demo/appsettings.Development.json
Normal file
9
FreeSql.RESTful.Demo/appsettings.Development.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Debug",
|
||||||
|
"System": "Information",
|
||||||
|
"Microsoft": "Information"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
FreeSql.RESTful.Demo/appsettings.json
Normal file
8
FreeSql.RESTful.Demo/appsettings.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
0
FreeSql.RESTful.Demo/xxxtb.db
Normal file
0
FreeSql.RESTful.Demo/xxxtb.db
Normal file
14
FreeSql.sln
14
FreeSql.sln
@ -16,6 +16,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "xxx", "..\..\新建文件
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Tests.PerformanceTests", "FreeSql.Tests.PerformanceTests\FreeSql.Tests.PerformanceTests.csproj", "{446D9CBE-BFE4-4FB3-ADFD-4C1C5EA1B6EE}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Tests.PerformanceTests", "FreeSql.Tests.PerformanceTests\FreeSql.Tests.PerformanceTests.csproj", "{446D9CBE-BFE4-4FB3-ADFD-4C1C5EA1B6EE}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.RESTful.Demo", "FreeSql.RESTful.Demo\FreeSql.RESTful.Demo.csproj", "{A749092B-4C21-4087-B33D-0FBD4DA51C24}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -74,6 +76,18 @@ Global
|
|||||||
{446D9CBE-BFE4-4FB3-ADFD-4C1C5EA1B6EE}.Release|x64.Build.0 = Release|Any CPU
|
{446D9CBE-BFE4-4FB3-ADFD-4C1C5EA1B6EE}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{446D9CBE-BFE4-4FB3-ADFD-4C1C5EA1B6EE}.Release|x86.ActiveCfg = Release|Any CPU
|
{446D9CBE-BFE4-4FB3-ADFD-4C1C5EA1B6EE}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{446D9CBE-BFE4-4FB3-ADFD-4C1C5EA1B6EE}.Release|x86.Build.0 = Release|Any CPU
|
{446D9CBE-BFE4-4FB3-ADFD-4C1C5EA1B6EE}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{A749092B-4C21-4087-B33D-0FBD4DA51C24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{A749092B-4C21-4087-B33D-0FBD4DA51C24}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{A749092B-4C21-4087-B33D-0FBD4DA51C24}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{A749092B-4C21-4087-B33D-0FBD4DA51C24}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{A749092B-4C21-4087-B33D-0FBD4DA51C24}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{A749092B-4C21-4087-B33D-0FBD4DA51C24}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{A749092B-4C21-4087-B33D-0FBD4DA51C24}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{A749092B-4C21-4087-B33D-0FBD4DA51C24}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{A749092B-4C21-4087-B33D-0FBD4DA51C24}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{A749092B-4C21-4087-B33D-0FBD4DA51C24}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{A749092B-4C21-4087-B33D-0FBD4DA51C24}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{A749092B-4C21-4087-B33D-0FBD4DA51C24}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user