diff --git a/AspNetCoreApi/AspNetCoreApi.csproj b/AspNetCoreApi/AspNetCoreApi.csproj index 9daa180..3bb40ba 100644 --- a/AspNetCoreApi/AspNetCoreApi.csproj +++ b/AspNetCoreApi/AspNetCoreApi.csproj @@ -7,6 +7,17 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/AspNetCoreApi/Controllers/ProductController.cs b/AspNetCoreApi/Controllers/ProductController.cs new file mode 100644 index 0000000..6267406 --- /dev/null +++ b/AspNetCoreApi/Controllers/ProductController.cs @@ -0,0 +1,26 @@ +using AspNetCoreApi.DbContexts; +using AspNetCoreApi.Models; +using Microsoft.AspNetCore.Mvc; + +namespace AspNetCoreApi.Controllers +{ + [ApiController] + [Route("[controller]")] + public class ProductController : ControllerBase + { + private readonly ILogger _logger; + private readonly AppDbContext _dbContext; + + public ProductController(ILogger logger, AppDbContext dbContext) + { + _logger = logger; + _dbContext = dbContext; + } + + [HttpGet(Name = "GetProduct")] + public IEnumerable Get() + { + return _dbContext.Products.ToList(); + } + } +} diff --git a/AspNetCoreApi/DbContexts/AppDbContext.cs b/AspNetCoreApi/DbContexts/AppDbContext.cs new file mode 100644 index 0000000..823da57 --- /dev/null +++ b/AspNetCoreApi/DbContexts/AppDbContext.cs @@ -0,0 +1,15 @@ +using AspNetCoreApi.Models; +using Microsoft.EntityFrameworkCore; + +namespace AspNetCoreApi.DbContexts +{ + public class AppDbContext : DbContext + { + public DbSet Products { get; set; } + + public AppDbContext(DbContextOptions options) : base(options) + { + + } + } +} diff --git a/AspNetCoreApi/Migrations/20240614040515_ef_inital_commit.Designer.cs b/AspNetCoreApi/Migrations/20240614040515_ef_inital_commit.Designer.cs new file mode 100644 index 0000000..72d577f --- /dev/null +++ b/AspNetCoreApi/Migrations/20240614040515_ef_inital_commit.Designer.cs @@ -0,0 +1,49 @@ +// +using AspNetCoreApi.DbContexts; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AspNetCoreApi.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20240614040515_ef_inital_commit")] + partial class ef_inital_commit + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.6") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder); + + modelBuilder.Entity("AspNetCoreApi.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("Price") + .HasColumnType("decimal(65,30)"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AspNetCoreApi/Migrations/20240614040515_ef_inital_commit.cs b/AspNetCoreApi/Migrations/20240614040515_ef_inital_commit.cs new file mode 100644 index 0000000..7450068 --- /dev/null +++ b/AspNetCoreApi/Migrations/20240614040515_ef_inital_commit.cs @@ -0,0 +1,41 @@ +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AspNetCoreApi.Migrations +{ + /// + public partial class ef_inital_commit : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterDatabase() + .Annotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.CreateTable( + name: "Products", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), + Name = table.Column(type: "longtext", nullable: false) + .Annotation("MySql:CharSet", "utf8mb4"), + Price = table.Column(type: "decimal(65,30)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Products", x => x.Id); + }) + .Annotation("MySql:CharSet", "utf8mb4"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Products"); + } + } +} diff --git a/AspNetCoreApi/Migrations/AppDbContextModelSnapshot.cs b/AspNetCoreApi/Migrations/AppDbContextModelSnapshot.cs new file mode 100644 index 0000000..e30aa82 --- /dev/null +++ b/AspNetCoreApi/Migrations/AppDbContextModelSnapshot.cs @@ -0,0 +1,46 @@ +// +using AspNetCoreApi.DbContexts; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AspNetCoreApi.Migrations +{ + [DbContext(typeof(AppDbContext))] + partial class AppDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.6") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder); + + modelBuilder.Entity("AspNetCoreApi.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("Price") + .HasColumnType("decimal(65,30)"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AspNetCoreApi/Models/Product.cs b/AspNetCoreApi/Models/Product.cs new file mode 100644 index 0000000..c39a619 --- /dev/null +++ b/AspNetCoreApi/Models/Product.cs @@ -0,0 +1,9 @@ +namespace AspNetCoreApi.Models +{ + public class Product + { + public int Id { get; set; } + public string Name { get; set; } + public decimal Price { get; set; } + } +} diff --git a/AspNetCoreApi/Program.cs b/AspNetCoreApi/Program.cs index 3847e83..54ecc85 100644 --- a/AspNetCoreApi/Program.cs +++ b/AspNetCoreApi/Program.cs @@ -1,5 +1,8 @@ +using AspNetCoreApi.DbContexts; using AspNetCoreApi.Middlewares; +using Microsoft.EntityFrameworkCore; +using Pomelo.EntityFrameworkCore.MySql; namespace AspNetCoreApi { @@ -9,6 +12,15 @@ namespace AspNetCoreApi { var builder = WebApplication.CreateBuilder(args); + // Add EF context + var connString = "server=peacecloud.synology.me;port=23306;database=Study;uid=study;password=Study1234!"; + var dbServerVersion = new MySqlServerVersion(new Version(10, 3, 32)); + + + builder.Services.AddDbContext( + options => options + .UseMySql(connString, dbServerVersion)); + // Add services to the container. builder.Services.AddControllers(); @@ -34,20 +46,22 @@ namespace AspNetCoreApi app.UseAuthorization(); - app.UseRouting(); - app.UseEndpoints(endpoints => - { - endpoints.MapGet("/routingtest", async context => - { - await context.Response.WriteAsync("Hello, World!"); - }); - - endpoints.MapGet("/routingtest/hello/{name}", async context => - { - var name = context.Request.RouteValues["name"]; - await context.Response.WriteAsync($"Hello, {name}!"); - }); - }); + + //// Add Routing + //app.UseRouting(); + //app.UseEndpoints(endpoints => + //{ + // endpoints.MapGet("/routingtest", async context => + // { + // await context.Response.WriteAsync("Hello, World!"); + // }); + + // endpoints.MapGet("/routingtest/hello/{name}", async context => + // { + // var name = context.Request.RouteValues["name"]; + // await context.Response.WriteAsync($"Hello, {name}!"); + // }); + //}); app.MapControllers(); diff --git a/README.md b/README.md index c822632..5d697e3 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,6 @@ - API Server - Middleware - - Routing \ No newline at end of file + - Routing + - EntityFramework + - Controller \ No newline at end of file