博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
记开发个人图书收藏清单小程序开发(九)Web开发——新增图书信息
阅读量:4878 次
发布时间:2019-06-11

本文共 5520 字,大约阅读时间需要 18 分钟。

书房信息初始化已完成,现在开始处理图书信息新增功能。

主要是实现之前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
Author { get; set; }12 public IEnumerable
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
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
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.InitSpec32 {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.translator49 };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
getDoubanBook(){...//查看之前的博客}73 public async Task
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);

 

?,Web端已解决。去实现DB部分了。

 

转载于:https://www.cnblogs.com/bu-dong/p/9226359.html

你可能感兴趣的文章
某考试T1 game
查看>>
c# 播放音乐
查看>>
mysql备份数据库
查看>>
28-Ubuntu-远程管理命令-02-查看网卡的配置信息
查看>>
sublime text3---Emmet:HTML/CSS代码快速编写神器
查看>>
Android:AysncTask异步加载
查看>>
要找工作啦
查看>>
JSON for java入门总结
查看>>
OpenCV imshow无法显示图片
查看>>
js线程&定时器
查看>>
路漫漫其修远兮
查看>>
java.lang.IllegalStateException: getOutputStream() has already been cal
查看>>
作业一
查看>>
微信支付开发,开通微信免充值代金券和微信免充值立减与折扣,社交立减金
查看>>
Tree - XGBoost with parameter description
查看>>
LearnMenu
查看>>
越狱机器SSH安装与使用
查看>>
使apache解析域名到目录的方法
查看>>
PHPExcel 使用(1)
查看>>
css3: scrollLeft,scrollWidth,clientWidth,offsetWidth 的区别
查看>>