mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-08-02 09:55:57 +08:00
initial commit
This commit is contained in:
115
Examples/repository_01/Controllers/SongController.cs
Normal file
115
Examples/repository_01/Controllers/SongController.cs
Normal file
@ -0,0 +1,115 @@
|
||||
using FreeSql;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using repository_01;
|
||||
using restful.Entitys;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace restful.Controllers
|
||||
{
|
||||
|
||||
public class SongRepository : GuidRepository<Song>
|
||||
{
|
||||
public SongRepository(IFreeSql fsql) : base(fsql)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[Route("restapi/[controller]")]
|
||||
public class SongsController : Controller
|
||||
{
|
||||
|
||||
BaseRepository<Song, int> _songRepository;
|
||||
|
||||
public class xxxx
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public bool IsDeleted { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
public SongsController(IFreeSql fsql,
|
||||
BaseRepository<Song> repos3, BaseRepository<Song, int> repos4,
|
||||
IBaseRepository<Song> repos31, IBaseRepository<Song, int> repos41,
|
||||
|
||||
SongRepository reposSong,
|
||||
IBaseRepository<TestSoftDelete> reposTest
|
||||
)
|
||||
{
|
||||
Console.Write(reposTest.Select.ToSql());
|
||||
|
||||
_songRepository = repos4;
|
||||
//test code
|
||||
var curd1 = fsql.GetRepository<Song, int>();
|
||||
var curd2 = fsql.GetRepository<Song, string>();
|
||||
var curd3 = fsql.GetRepository<Song, Guid>();
|
||||
var curd4 = fsql.GetGuidRepository<Song>();
|
||||
|
||||
Console.WriteLine(reposSong.Select.ToSql());
|
||||
|
||||
using (reposSong.DataFilter.DisableAll())
|
||||
{
|
||||
Console.WriteLine(reposSong.Select.ToSql());
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public Task<List<Song>> GetItems([FromQuery] string key, [FromQuery] int page = 1, [FromQuery] int limit = 20)
|
||||
{
|
||||
return _songRepository.Select.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="pagingInfo"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("GetPagingItems")]
|
||||
public Task<List<Song>> GetPagingItems([FromQuery] string key, [FromQuery] PagingInfo pagingInfo)
|
||||
{
|
||||
return _songRepository.Select.WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(pagingInfo).ToListAsync();
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public Task<Song> GetItem([FromRoute] int id)
|
||||
{
|
||||
return _songRepository.FindAsync(id);
|
||||
}
|
||||
|
||||
public class ModelSong
|
||||
{
|
||||
public string title { get; set; }
|
||||
}
|
||||
|
||||
[HttpPost, ProducesResponseType(201)]
|
||||
public Task<Song> Create([FromBody] ModelSong model)
|
||||
{
|
||||
return _songRepository.InsertAsync(new Song { Title = model.title });
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public Task Update([FromRoute] int id, [FromBody] ModelSong model)
|
||||
{
|
||||
return _songRepository.UpdateAsync(new Song { Id = id, Title = model.title });
|
||||
}
|
||||
|
||||
[HttpPatch("{id}")]
|
||||
async public Task<Song> UpdateDiy([FromRoute] int id, [FromForm] string title)
|
||||
{
|
||||
var up = _songRepository.UpdateDiy.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)]
|
||||
public Task Delete([FromRoute] int id)
|
||||
{
|
||||
return _songRepository.DeleteAsync(a => a.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
13
Examples/repository_01/Entitys/Song.cs
Normal file
13
Examples/repository_01/Entitys/Song.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using repository_01;
|
||||
|
||||
namespace restful.Entitys
|
||||
{
|
||||
public class Song
|
||||
{
|
||||
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
}
|
||||
}
|
57
Examples/repository_01/PagingInfo.cs
Normal file
57
Examples/repository_01/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 repository_01
|
||||
{
|
||||
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/repository_01/Program.cs
Normal file
24
Examples/repository_01/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 repository_01
|
||||
{
|
||||
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/repository_01/Properties/launchSettings.json
Normal file
27
Examples/repository_01/Properties/launchSettings.json
Normal file
@ -0,0 +1,27 @@
|
||||
{
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:52751/",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"repository_01": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "http://localhost:52752/"
|
||||
}
|
||||
}
|
||||
}
|
95
Examples/repository_01/Startup.cs
Normal file
95
Examples/repository_01/Startup.cs
Normal file
@ -0,0 +1,95 @@
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace repository_01
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 用户密码信息
|
||||
/// </summary>
|
||||
public class Sys1UserLogOn
|
||||
{
|
||||
[Column(IsPrimary = true, Name = "Id")]
|
||||
public Guid UserLogOnId { get; set; }
|
||||
public virtual Sys1User User { get; set; }
|
||||
}
|
||||
public class Sys1User
|
||||
{
|
||||
[Column(IsPrimary = true, Name = "Id")]
|
||||
public Guid UserId { get; set; }
|
||||
public virtual Sys1UserLogOn UserLogOn { get; set; }
|
||||
}
|
||||
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
|
||||
{
|
||||
Configuration = configuration;
|
||||
|
||||
Fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Pooling=true;Max Pool Size=10")
|
||||
.UseAutoSyncStructure(true)
|
||||
.UseLazyLoading(true)
|
||||
|
||||
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
|
||||
.Build();
|
||||
|
||||
var sysu = new Sys1User { };
|
||||
Fsql.Insert<Sys1User>().AppendData(sysu).ExecuteAffrows();
|
||||
Fsql.Insert<Sys1UserLogOn>().AppendData(new Sys1UserLogOn { UserLogOnId = sysu.UserId }).ExecuteAffrows();
|
||||
var a = Fsql.Select<Sys1UserLogOn>().ToList();
|
||||
var b = Fsql.Select<Sys1UserLogOn>().Any();
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
public static IFreeSql Fsql { get; private set; }
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
|
||||
//services.AddTransient(s => s.)
|
||||
|
||||
services.AddControllersWithViews();
|
||||
services.AddSingleton<IFreeSql>(Fsql);
|
||||
|
||||
services.AddFreeRepository(filter =>
|
||||
{
|
||||
filter
|
||||
//.Apply<Song>("test", a => a.Title == DateTime.Now.ToString() + System.Threading.Thread.CurrentThread.ManagedThreadId)
|
||||
.Apply<ISoftDelete>("softdelete", a => a.IsDeleted == false);
|
||||
}, this.GetType().Assembly);
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
public interface ISoftDelete
|
||||
{
|
||||
bool IsDeleted { get; set; }
|
||||
}
|
||||
|
||||
public class TestSoftDelete : ISoftDelete
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
}
|
||||
}
|
9
Examples/repository_01/appsettings.Development.json
Normal file
9
Examples/repository_01/appsettings.Development.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Warning",
|
||||
"Microsoft": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
8
Examples/repository_01/appsettings.json
Normal file
8
Examples/repository_01/appsettings.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
18
Examples/repository_01/repository_01.csproj
Normal file
18
Examples/repository_01/repository_01.csproj
Normal file
@ -0,0 +1,18 @@
|
||||
<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="..\..\Extensions\FreeSql.Extensions.LazyLoading\FreeSql.Extensions.LazyLoading.csproj" />
|
||||
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
||||
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user