diff --git "a/\351\242\234\346\234\235\345\213\207/20240603-\351\241\271\347\233\256\345\256\236\347\216\260\351\203\250\345\210\206.md" "b/\351\242\234\346\234\235\345\213\207/20240603-\351\241\271\347\233\256\345\256\236\347\216\260\351\203\250\345\210\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..33b84c7783fca502a33bfb4615d13457ffe5bac4 --- /dev/null +++ "b/\351\242\234\346\234\235\345\213\207/20240603-\351\241\271\347\233\256\345\256\236\347\216\260\351\203\250\345\210\206.md" @@ -0,0 +1,73 @@ +### Controllers BooksController.cs + +``` +using BookStore.Api.Db; +using BookStore.Api.Interface; +using Microsoft.AspNetCore.Mvc; + +namespace BookStore.Api.Controller; + +[ApiController] +[Route("api/authors/{authorId}/[controller]/")] +public class BooksController : ControllerBase +{ + + private readonly IBookRepository _bookRepository; + + public BooksController(IBookRepository bookRepository) + { + _bookRepository = bookRepository; + } + + [HttpGet("{bookId?}")] + public IActionResult Get(Guid authorId, Guid bookId) + { + if (bookId.ToString() == "00000000-0000-0000-0000-000000000000") + { + var list = _bookRepository.GetAllBooks(authorId); + return Ok(list); + } + + var result = _bookRepository.GetBookById(authorId, bookId); + + return Ok(result); + } + + [HttpPost] + public IActionResult Post(Guid authorId) + { + return Ok(); + } + + [HttpPut("{id}")] + public IActionResult Put(Guid authorId, Guid id) + { + return Ok(id); + } + + [HttpDelete("{id}")] + public IActionResult Del(Guid authorId, Guid id) + { + return Ok(id); + } +} + +``` + + +### Domain Books.cs + +``` +namespace BookStore.Api.Domain; + +public class Books +{ + public Guid Id { get; set; } + public string BookName { get; set; } = null!; + public string? Publisher { get; set; } + public int Price { get; set; } + public Guid AuthorId { get; set; } +} + +``` + diff --git "a/\351\242\234\346\234\235\345\213\207/20240604-\345\270\270\350\247\201\351\233\206\345\220\210\346\216\245\345\217\243.md" "b/\351\242\234\346\234\235\345\213\207/20240604-\345\270\270\350\247\201\351\233\206\345\220\210\346\216\245\345\217\243.md" new file mode 100644 index 0000000000000000000000000000000000000000..a5e06b9d94069bf9d322a6b57235c8294384e943 --- /dev/null +++ "b/\351\242\234\346\234\235\345\213\207/20240604-\345\270\270\350\247\201\351\233\206\345\220\210\346\216\245\345\217\243.md" @@ -0,0 +1,24 @@ +## 常见集合接口 + +![20240604105805](https://oss.9ihub.com/test/20240604105805.png) + + + +@startuml +interface IEnumerable { + + GetEnumerator(): IEnumerator +} +interface IEnumerator { + + Current: object + + MoveNext(): bool + + Reset() +} +interface ICollection +interface IList +interface IDictionary + +IEnumerable --> IEnumerator +IEnumerable <|.. ICollection +ICollection <|.. IList +ICollection <|.. IDictionary +@enduml \ No newline at end of file diff --git "a/\351\242\234\346\234\235\345\213\207/20240606-\345\256\214\346\210\220\345\233\276\344\271\246\347\232\204\346\226\260\345\242\236\343\200\201\344\277\256\346\224\271\346\216\245\345\217\243.md" "b/\351\242\234\346\234\235\345\213\207/20240606-\345\256\214\346\210\220\345\233\276\344\271\246\347\232\204\346\226\260\345\242\236\343\200\201\344\277\256\346\224\271\346\216\245\345\217\243.md" new file mode 100644 index 0000000000000000000000000000000000000000..27addc16a35e17c0fe1dd72b91f8e64509cbc3f6 --- /dev/null +++ "b/\351\242\234\346\234\235\345\213\207/20240606-\345\256\214\346\210\220\345\233\276\344\271\246\347\232\204\346\226\260\345\242\236\343\200\201\344\277\256\346\224\271\346\216\245\345\217\243.md" @@ -0,0 +1,115 @@ +### Services BookRepository.cs + +``` +using BookStore.Api.Db; +using BookStore.Api.Domain; +using BookStore.Api.Dto; +using BookStore.Api.Interface; + +namespace BookStore.Api.Services; + +public class BookRepository : IBookRepository +{ + public BookDto? Delete(Guid authorId, Guid BookId) + { + return new BookDto(); + } + + public ICollection GetAllBooks(Guid authorId) + { + /* + 较好的做法,是确认对应authorId下有没有对应的作者, + 并且作者的状态是否是正确的(没有被删除,没有被禁用,没有被限制功能) + + 如果业务不允许,则返回作者状态不正确的结果 + */ + var list = BookStoreDb.Instance.Books.Where(item => item.AuthorId == authorId) + .Select(item => new BookDto { Id = item.Id, AuthorId = item.AuthorId, BookName = item.BookName }).ToList(); + return list; + } + + public BookDto? GetBookById(Guid authorId, Guid bookId) + { + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + if (author == null) + { + return null; + } + var book = BookStoreDb.Instance.Books.FirstOrDefault(item => item.AuthorId == authorId && item.Id == bookId); + + if (book == null) + { + return null; + } + return new BookDto { Id = book.Id, BookName = book.BookName, AuthorId = book.AuthorId }; + } + + public BookDto? Insert(Guid authorId, BookCreateDto bookCreateDto) + { + /* + 1.确认传入的authorId存在对应的记录 + 如果存在,继续后面的流程;否则返回null + 2.将输入的数据进行整理转换,插入到数据中 + 1. 将BookCreateDto类型的数据转换成Books类型的数据 + 2. 将转换到books数据插入数据库表 + + 为了简单,目前从未对传入数据进行任何验证,原因就是为了让整个应用尽量简单,后续一定要进行数据验证 + 1. 简单验证 数据类型,是否为空,字符长度,是否一定格式等等 + 2. 复杂验证或者叫业务验证 + + */ + + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + if (author == null) + { + return null; + } + var tmp = new Books + { + Id = Guid.NewGuid(), + BookName = bookCreateDto.BookName, + Publisher = bookCreateDto.Publisher, + Price = bookCreateDto.Price, + AuthorId = authorId + }; + + BookStoreDb.Instance.Books.Add(tmp); + + var result = new BookDto { Id = tmp.Id, BookName = tmp.BookName, AuthorId = tmp.AuthorId, Author = author }; + + return result; + } + + public BookDto? Update(Guid authorId, Guid BookId, BookUpdateDto bookUpdateDto) + { + /* + 1. 要修改的指定的作者是不是在,如果不在则返回null,如果在则继续 + 2. 要修改的指定的图书book是不是存在,如果不存在则返回null,如果存在则继续 + 3. 完成book数据的更改和保存 + 将传入的bookUpdateDto数据赋值给book,就可以了 + + */ + + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + if (author == null) + { + return null; + } + + var book = BookStoreDb.Instance.Books.FirstOrDefault(item => item.Id == BookId); + if (book == null) + { + return null; + } + + book.BookName = bookUpdateDto.BookName; + book.Publisher = bookUpdateDto.Publisher; + book.Price = bookUpdateDto.Price; + book.BookName = bookUpdateDto.BookName; + + var result = new BookDto { Id = book.Id, BookName = book.BookName, AuthorId = book.AuthorId }; + return result; + } +} + +``` \ No newline at end of file diff --git "a/\351\242\234\346\234\235\345\213\207/20240607-\345\274\225\345\205\245EFCore.md" "b/\351\242\234\346\234\235\345\213\207/20240607-\345\274\225\345\205\245EFCore.md" new file mode 100644 index 0000000000000000000000000000000000000000..44ea2ac651d6d5c541a3642842e92ff1d968963b --- /dev/null +++ "b/\351\242\234\346\234\235\345\213\207/20240607-\345\274\225\345\205\245EFCore.md" @@ -0,0 +1,213 @@ +## 引入EFCore + +RepositoryBase.cs + +``` +namespace BookStore.Api.Interface; + +/// +/// 基础仓储接口,定义通用的CRUD功能 +/// +public interface IRepositoryBase +{ + /* + 通过id获取指定模型(对应数据库中的表,模型中的公共属性对应数据表的字段或者叫列) + 获取所有的模型 + + 获取所有实体 + + 新增插入 + + 删除 + + + 更新 + + */ + + T GetById(Guid id); + + List GetAll(); + + T Insert(T entity); + T Update(T entity); + T Delete(T entity); + T Delete(Guid id); +} + +``` + + + +Startup.cs + +``` +using BookStore.Api.Db; +using BookStore.Api.Interface; +using BookStore.Api.Services; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace BookStore.Api; + +public class Startup +{ + private readonly IConfiguration _configuration; + + public Startup(IConfiguration configuration) + { + _configuration = configuration; + } + public void Configure(IApplicationBuilder app, IHostEnvironment env) + { + // 如果是开发环境,则引入Swagger +@@ -16,7 +24,7 @@ public class Startup + } + // 路由中间件 + app.UseRouting(); + + + // 配置路由端点匹配控制器 + app.UseEndpoints(endPoints => + { +@@ -36,5 +44,11 @@ public class Startup + // 将仓储服务注册到容器 + services.AddScoped(); + services.AddScoped(); + + // 注册数据库上下文到容器 + services.AddDbContext( + option => + option.UseSqlServer(_configuration.GetConnectionString("Mssql")) + ); + } +} + + +``` + + +BookStoreDbContexModelSnapshot.cs + +``` +using System; +using BookStore.Api.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BookStore.Api.Migrations +{ + [DbContext(typeof(BookStoreDbContext))] + partial class BookStoreDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.6") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BookStore.Api.Domain.Authors", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("AuthorName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Birthday") + .HasColumnType("datetime2"); + + b.Property("Gender") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Authors"); + }); + + modelBuilder.Entity("BookStore.Api.Domain.Books", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("AuthorId") + .HasColumnType("uniqueidentifier"); + + b.Property("BookName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("int"); + + b.Property("Publisher") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Books"); + }); + + modelBuilder.Entity("BookStore.Api.Domain.Users", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Username") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} + + +``` + + + + +BookStoreDbContext.cs + +``` +using BookStore.Api.Domain; +using Microsoft.EntityFrameworkCore; + +namespace BookStore.Api.Db; + +public class BookStoreDbContext : DbContext +{ + public BookStoreDbContext(DbContextOptions options) : base(options) + { + + } + + + public DbSet Authors { get; set; } + public DbSet Books { get; set; } + public DbSet Users { get; set; } + +} + + +``` \ No newline at end of file