记开发个人图书收藏清单小程序开发(九)Web开发——新增图书信息
程序员文章站
2022-07-05 11:59:07
书房信息初始化已完成,现在开始处理图书信息新增功能。 主要是实现之前New Razor Pages的后台部分。 新增需要保存的Model:Book.InitSpec.cs /Models/Book.InitSpec.cs 实现Post请求部分: /Pages/Shelves/New.cshtml.c ......
书房信息初始化已完成,现在开始处理图书信息新增功能。
主要是实现之前New Razor Pages的后台部分。
新增需要保存的Model:Book.InitSpec.cs
/Models/Book.InitSpec.cs
1 using System.Collections.Generic; 2 3 namespace PTager.BL 4 { 5 public partial class Book 6 { 7 public class InitSpec 8 { 9 public string Title { get; set; } 10 public string Subtitle { get; set; } 11 public IEnumerable<string> Author { get; set; } 12 public IEnumerable<string> Translator { get; set; } 13 public string Isbn13 { get; set; } 14 public string Isbn10 { get; set; } 15 public string AuthorIntro { get; set; } 16 public string Summary { get; set; } 17 public string Publisher { get; set; } 18 public string Binding { get; set; } 19 public string OriginTitle { get; set; } 20 public int Pages { get; set; } 21 public string ImageUrl { get; set; } 22 public string Pubdate { get; set; } 23 public string Catalog { get; set; } 24 public IEnumerable<string> Tags { get; set; } 25 } 26 } 27 }
实现Post请求部分:
/Pages/Shelves/New.cshtml.cs
1 namespace PTager.BL.WebUI.Pages.Shelves 2 { 3 using M = Book; 4 public class NewModel : PageModel 5 { 6 private readonly IHostingEnvironment _env; 7 private readonly string _savePath; 8 private readonly string _relativePath; 9 public NewModel(IHostingEnvironment env) 10 { 11 _env = env; 12 _relativePath = Path.Combine("App_Data", "Images/Books", DateTime.Today.ToString("yyyy-MM-dd")); 13 _savePath = Path.Combine(_env.ContentRootPath, _relativePath); 14 } 15 16 [BindProperty] 17 public string IsbnNbr { get; set; } 18 public DoubanBookModel DoubanBook { get; set; } 19 20 public async Task OnGetAsync(string isbn){...//查看之前的博客} 21 public async Task<IActionResult> OnPostAsync() 22 { 23 if (validIsbnNbr(IsbnNbr)) 24 { 25 DoubanBook = await getDoubanBook(); 26 if (DoubanBook != null) 27 { 28 var extention = Path.GetExtension(DoubanBook.image); 29 var fileName = Guid.NewGuid().ToString() + (string.IsNullOrEmpty(extention) ? ".jpeg" : extention); 30 await saveImageAsync(fileName, DoubanBook.image); 31 var spec = new M.InitSpec 32 { 33 Author = DoubanBook.author, 34 AuthorIntro = DoubanBook.author_intro, 35 Binding = DoubanBook.binding, 36 Catalog = DoubanBook.catalog, 37 ImageUrl = Path.Combine(_relativePath, fileName), 38 Isbn10 = DoubanBook.isbn10, 39 Isbn13 = DoubanBook.isbn13, 40 OriginTitle = DoubanBook.origin_title, 41 Pages = DoubanBook.pages, 42 Pubdate = DoubanBook.pubdate, 43 Publisher = DoubanBook.publisher, 44 Subtitle = DoubanBook.subtitle, 45 Summary = DoubanBook.summary, 46 Tags = DoubanBook.tags.Select(x => x.name), 47 Title = DoubanBook.title, 48 Translator = DoubanBook.translator 49 }; 50 } 51 } 52 return Page(); 53 } 54 private async Task saveImageAsync(string fileName, string url) 55 { 56 using (var httpClient = new HttpClient()) 57 { 58 var responseStream = await httpClient.GetStreamAsync(url); 59 var savePath = Path.Combine(_savePath, fileName); 60 var stream = new FileStream(savePath, FileMode.Create); 61 byte[] bArr = new byte[1024]; 62 int size = responseStream.Read(bArr, 0, bArr.Length); 63 while (size > 0) 64 { 65 stream.Write(bArr, 0, size); 66 size = responseStream.Read(bArr, 0, bArr.Length); 67 } 68 stream.Close(); 69 responseStream.Close(); 70 } 71 } 72 private async Task<DoubanBookModel> getDoubanBook(){...//查看之前的博客} 73 public async Task<string> HttpGetAsync(string url, Encoding encoding = null){...//查看之前的博客} 74 75 private bool validIsbnNbr(string isbn){...//查看之前的博客} 76 } 77 }
新增IBookRepo和BookRepo:
/Repos/IBookRepo.cs
1 using System.Threading.Tasks; 2 3 namespace PTager.BL 4 { 5 using M = Book; 6 public interface IBookRepo 7 { 8 Task InitAsync(M.InitSpec spec); 9 } 10 }
/Repos/BookRepo.cs
1 using System.Threading.Tasks; 2 using PTager.BL.Data.Store; 3 4 namespace PTager.BL.Data.Repos 5 { 6 using M = Book; 7 public class BookRepo : RepoBase, IBookRepo 8 { 9 public BookRepo(BLDbContext context) : base(context) 10 { 11 } 12 13 public async Task InitAsync(M.InitSpec spec) 14 => await _context.Book_Init(spec.ToJson()); 15 } 16 }
/Store/BLDbContext.cs
1 public async Task Book_Init(string json) 2 => await this.ExecuteMethodCallAsync(nameof(Book_Init), args: json);
上一篇: 头一次听见做梦还要收费的