成功写入Uesrs数据库表
This commit is contained in:
9
WorkStation.Server/Context/Entity/BaseEntity.cs
Normal file
9
WorkStation.Server/Context/Entity/BaseEntity.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace WorkStation.Server.Context.Entity
|
||||
{
|
||||
public class BaseEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
public DateTime UpdateTime { get; set; }
|
||||
}
|
||||
}
|
8
WorkStation.Server/Context/Entity/User.cs
Normal file
8
WorkStation.Server/Context/Entity/User.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace WorkStation.Server.Context.Entity
|
||||
{
|
||||
public class User : BaseEntity
|
||||
{
|
||||
public string? Account { get; set; }
|
||||
public string? Password { get; set; }
|
||||
}
|
||||
}
|
10
WorkStation.Server/Context/MyDbContext.cs
Normal file
10
WorkStation.Server/Context/MyDbContext.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WorkStation.Server.Context.Entity;
|
||||
|
||||
namespace WorkStation.Server.Context
|
||||
{
|
||||
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<User> Users { get; set; }
|
||||
}
|
||||
}
|
9
WorkStation.Server/Context/Repository/UserRepository.cs
Normal file
9
WorkStation.Server/Context/Repository/UserRepository.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using Arch.EntityFrameworkCore.UnitOfWork;
|
||||
using WorkStation.Server.Context.Entity;
|
||||
|
||||
namespace WorkStation.Server.Context.Repository
|
||||
{
|
||||
public class UserRepository(MyDbContext context) : Repository<User>(context), IRepository<User>
|
||||
{
|
||||
}
|
||||
}
|
17
WorkStation.Server/Controllers/UserController.cs
Normal file
17
WorkStation.Server/Controllers/UserController.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WorkStation.Server.Context.Entity;
|
||||
using WorkStation.Server.Services;
|
||||
using WorkStation.Share;
|
||||
|
||||
namespace WorkStation.Server.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class UserController(IUserService service) : ControllerBase
|
||||
{
|
||||
private readonly IUserService _service = service;
|
||||
[HttpPost]
|
||||
public Task<ApiResponse> Add(User item) => _service.AddAsync(item);
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WorkStation.Server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class WeatherForecastController : ControllerBase
|
||||
{
|
||||
private static readonly string[] Summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet(Name = "GetWeatherForecast")]
|
||||
public IEnumerable<WeatherForecast> Get()
|
||||
{
|
||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,22 @@
|
||||
using Arch.EntityFrameworkCore.UnitOfWork;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WorkStation.Server.Context;
|
||||
using WorkStation.Server.Context.Entity;
|
||||
using WorkStation.Server.Context.Repository;
|
||||
using WorkStation.Server.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
// Add services to the container.
|
||||
var connectionString = builder.Configuration.GetConnectionString("MySQLConnection");
|
||||
builder.Services.AddDbContext<MyDbContext>(options =>
|
||||
{
|
||||
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
|
||||
});
|
||||
builder.Services.AddUnitOfWork<MyDbContext>();
|
||||
builder.Services.AddCustomRepository<User, UserRepository>();
|
||||
builder.Services.AddTransient<IUserService, UserService>();
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
@ -9,6 +24,7 @@ builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
|
14
WorkStation.Server/Services/IBaseSerivice.cs
Normal file
14
WorkStation.Server/Services/IBaseSerivice.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using WorkStation.Share;
|
||||
|
||||
namespace WorkStation.Server.Services
|
||||
{
|
||||
public interface IBaseSerivice<T>
|
||||
{
|
||||
Task<ApiResponse> AddAsync(T item);
|
||||
Task<ApiResponse> DelateAsync(int id);
|
||||
Task<ApiResponse> UpdateAsync(T item);
|
||||
Task<ApiResponse> GetByIdAsync(int id);
|
||||
Task<ApiResponse> GetAllAsync();
|
||||
|
||||
}
|
||||
}
|
9
WorkStation.Server/Services/IUserService.cs
Normal file
9
WorkStation.Server/Services/IUserService.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using WorkStation.Server.Context.Entity;
|
||||
|
||||
namespace WorkStation.Server.Services
|
||||
{
|
||||
public interface IUserService : IBaseSerivice<User>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
38
WorkStation.Server/Services/UserService.cs
Normal file
38
WorkStation.Server/Services/UserService.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using Arch.EntityFrameworkCore.UnitOfWork;
|
||||
using WorkStation.Server.Context.Entity;
|
||||
using WorkStation.Share;
|
||||
|
||||
namespace WorkStation.Server.Services
|
||||
{
|
||||
public class UserService(IUnitOfWork unitOfWork) : IUserService
|
||||
{
|
||||
private IUnitOfWork _unitOfWork = unitOfWork;
|
||||
public async Task<ApiResponse> AddAsync(User item)
|
||||
{
|
||||
var repository = _unitOfWork.GetRepository<User>();
|
||||
await repository.InsertAsync(item);
|
||||
var count = await _unitOfWork.SaveChangesAsync();
|
||||
return new ApiResponse(true, "添加数据", count);
|
||||
}
|
||||
|
||||
public Task<ApiResponse> DelateAsync(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<ApiResponse> GetAllAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<ApiResponse> GetByIdAsync(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<ApiResponse> UpdateAsync(User item)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
namespace WorkStation.Server
|
||||
{
|
||||
public class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
@ -7,7 +7,18 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.13" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.13">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.UnitOfWork" Version="3.1.0" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\WorkStation.Share\WorkStation.Share.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -5,5 +5,8 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"MySQLConnection": "server=106.14.189.125;Database=WorkStationDB;User=root;Password=Mysql!950307"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user