mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-08-02 09:55:57 +08:00
initial commit
This commit is contained in:
BIN
Examples/restful/001.png
Normal file
BIN
Examples/restful/001.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
BIN
Examples/restful/002.png
Normal file
BIN
Examples/restful/002.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
BIN
Examples/restful/003.png
Normal file
BIN
Examples/restful/003.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
BIN
Examples/restful/004.png
Normal file
BIN
Examples/restful/004.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
81
Examples/restful/Controllers/SongController.cs
Normal file
81
Examples/restful/Controllers/SongController.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using restful.Entitys;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace restful.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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// curl -X GET "http://localhost:5000/restapi/Songs/GetPagingItems?key=FreeSql&PageNumber=2&PageSize=10" -H "accept: text/plain"
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="pagingInfo"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("GetPagingItems")]
|
||||
public Task<List<Song>> GetPagingItems([FromQuery] string key, [FromQuery] PagingInfo pagingInfo)
|
||||
{
|
||||
return _fsql.Select<Song>().WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(pagingInfo).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();
|
||||
}
|
||||
}
|
||||
}
|
12
Examples/restful/Entitys/Song.cs
Normal file
12
Examples/restful/Entitys/Song.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
|
||||
namespace restful.Entitys
|
||||
{
|
||||
public class Song
|
||||
{
|
||||
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
}
|
||||
}
|
57
Examples/restful/PagingInfo.cs
Normal file
57
Examples/restful/PagingInfo.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using FreeSql.Internal.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace restful
|
||||
{
|
||||
public class PagingInfo : BasePagingInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 无参构造函数
|
||||
/// </summary>
|
||||
public PagingInfo()
|
||||
{
|
||||
}
|
||||
/// <summary>
|
||||
/// 当前为第1页,每页大小的构造函数
|
||||
/// </summary>
|
||||
/// <param name="pageSize"></param>
|
||||
public PagingInfo(int pageSize)
|
||||
{
|
||||
PageNumber = 1;
|
||||
PageSize = pageSize;
|
||||
}
|
||||
/// <summary>
|
||||
/// 带当前页和每页大小的构造函数
|
||||
/// </summary>
|
||||
/// <param name="pageNumber"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
public PagingInfo(int pageNumber, int pageSize)
|
||||
{
|
||||
PageNumber = pageNumber;
|
||||
PageSize = pageSize;
|
||||
}
|
||||
/// <summary>
|
||||
/// 当前有多少页【只读】
|
||||
/// </summary>
|
||||
public long PageCount => PageSize == 0 ? 0 : (Count + PageSize - 1) / PageSize;
|
||||
/// <summary>
|
||||
/// 是否有上一页【只读】
|
||||
/// </summary>
|
||||
public bool HasPrevious => PageNumber > 1 && PageNumber <= PageCount;
|
||||
/// <summary>
|
||||
/// 是否有下一页【只读】
|
||||
/// </summary>
|
||||
public bool HasNext => PageNumber < PageCount;
|
||||
/// <summary>
|
||||
/// 是否在第一页【只读】
|
||||
/// </summary>
|
||||
public bool IsFrist => PageNumber == 1;
|
||||
/// <summary>
|
||||
/// 是否在最后一页【只读】
|
||||
/// </summary>
|
||||
public bool IsLast => PageNumber == PageCount;
|
||||
}
|
||||
}
|
24
Examples/restful/Program.cs
Normal file
24
Examples/restful/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 restful
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateWebHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.UseStartup<Startup>();
|
||||
}
|
||||
}
|
27
Examples/restful/Properties/launchSettings.json
Normal file
27
Examples/restful/Properties/launchSettings.json
Normal file
@ -0,0 +1,27 @@
|
||||
{
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:49778/",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"FreeSql.RESTful.Demo": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "http://localhost:49779/"
|
||||
}
|
||||
}
|
||||
}
|
57
Examples/restful/Startup.cs
Normal file
57
Examples/restful/Startup.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace restful
|
||||
{
|
||||
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")
|
||||
.UseAutoSyncStructure(true)
|
||||
.Build();
|
||||
|
||||
Fsql.Aop.CurdAfter += (s, e) =>
|
||||
{
|
||||
if (e.ElapsedMilliseconds > 200)
|
||||
{
|
||||
//记录日志
|
||||
//发送短信给负责人
|
||||
}
|
||||
};
|
||||
|
||||
//Fsql.Aop.Where = (s, e) => {
|
||||
// if (e.Parameters[0]?.ToString() == "1")
|
||||
// e.IsCancel = true;
|
||||
//};
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
public IFreeSql Fsql { get; }
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IFreeSql>(Fsql);
|
||||
services.AddControllersWithViews();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
|
||||
{
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
Console.OutputEncoding = Encoding.GetEncoding("GB2312");
|
||||
Console.InputEncoding = Encoding.GetEncoding("GB2312");
|
||||
|
||||
app.UseHttpMethodOverride(new HttpMethodOverrideOptions { FormFieldName = "X-Http-Method-Override" });
|
||||
app.UseDeveloperExceptionPage();
|
||||
app.UseRouting();
|
||||
app.UseEndpoints(a => a.MapControllers());
|
||||
}
|
||||
}
|
||||
}
|
9
Examples/restful/appsettings.Development.json
Normal file
9
Examples/restful/appsettings.Development.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
}
|
||||
}
|
8
Examples/restful/appsettings.json
Normal file
8
Examples/restful/appsettings.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
17
Examples/restful/restful.csproj
Normal file
17
Examples/restful/restful.csproj
Normal file
@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
||||
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
0
Examples/restful/xxxtb.db
Normal file
0
Examples/restful/xxxtb.db
Normal file
Reference in New Issue
Block a user