记开发个人图书收藏清单小程序开发(九)Web开发——新增图书信息
书房信息初始化已完成,现在开始处理图书信息新增功能。
主要是实现之前New Razor Pages的后台部分。
新增需要保存的Model:Book.InitSpec.cs
/Models/Book.InitSpec.cs
using System.Collections.Generic; namespace PTager.BL
{
public partial class Book
{
public class InitSpec
{
public string Title { get; set; }
public string Subtitle { get; set; }
public IEnumerable<string> Author { get; set; }
public IEnumerable<string> Translator { get; set; }
public string Isbn13 { get; set; }
public string Isbn10 { get; set; }
public string AuthorIntro { get; set; }
public string Summary { get; set; }
public string Publisher { get; set; }
public string Binding { get; set; }
public string OriginTitle { get; set; }
public int Pages { get; set; }
public string ImageUrl { get; set; }
public string Pubdate { get; set; }
public string Catalog { get; set; }
public IEnumerable<string> Tags { get; set; }
}
}
}
实现Post请求部分:
/Pages/Shelves/New.cshtml.cs
namespace PTager.BL.WebUI.Pages.Shelves
{
using M = Book;
public class NewModel : PageModel
{
private readonly IHostingEnvironment _env;
private readonly string _savePath;
private readonly string _relativePath;
public NewModel(IHostingEnvironment env)
{
_env = env;
_relativePath = Path.Combine("App_Data", "Images/Books", DateTime.Today.ToString("yyyy-MM-dd"));
_savePath = Path.Combine(_env.ContentRootPath, _relativePath);
} [BindProperty]
public string IsbnNbr { get; set; }
public DoubanBookModel DoubanBook { get; set; } public async Task OnGetAsync(string isbn){...//查看之前的博客}
public async Task<IActionResult> OnPostAsync()
{
if (validIsbnNbr(IsbnNbr))
{
DoubanBook = await getDoubanBook();
if (DoubanBook != null)
{
var extention = Path.GetExtension(DoubanBook.image);
var fileName = Guid.NewGuid().ToString() + (string.IsNullOrEmpty(extention) ? ".jpeg" : extention);
await saveImageAsync(fileName, DoubanBook.image);
var spec = new M.InitSpec
{
Author = DoubanBook.author,
AuthorIntro = DoubanBook.author_intro,
Binding = DoubanBook.binding,
Catalog = DoubanBook.catalog,
ImageUrl = Path.Combine(_relativePath, fileName),
Isbn10 = DoubanBook.isbn10,
Isbn13 = DoubanBook.isbn13,
OriginTitle = DoubanBook.origin_title,
Pages = DoubanBook.pages,
Pubdate = DoubanBook.pubdate,
Publisher = DoubanBook.publisher,
Subtitle = DoubanBook.subtitle,
Summary = DoubanBook.summary,
Tags = DoubanBook.tags.Select(x => x.name),
Title = DoubanBook.title,
Translator = DoubanBook.translator
};
}
}
return Page();
}
private async Task saveImageAsync(string fileName, string url)
{
using (var httpClient = new HttpClient())
{
var responseStream = await httpClient.GetStreamAsync(url);
var savePath = Path.Combine(_savePath, fileName);
var stream = new FileStream(savePath, FileMode.Create);
byte[] bArr = new byte[];
int size = responseStream.Read(bArr, , bArr.Length);
while (size > )
{
stream.Write(bArr, , size);
size = responseStream.Read(bArr, , bArr.Length);
}
stream.Close();
responseStream.Close();
}
}
private async Task<DoubanBookModel> getDoubanBook(){...//查看之前的博客}
public async Task<string> HttpGetAsync(string url, Encoding encoding = null){...//查看之前的博客} private bool validIsbnNbr(string isbn){...//查看之前的博客}
}
}
新增IBookRepo和BookRepo:
/Repos/IBookRepo.cs
using System.Threading.Tasks; namespace PTager.BL
{
using M = Book;
public interface IBookRepo
{
Task InitAsync(M.InitSpec spec);
}
}
/Repos/BookRepo.cs
using System.Threading.Tasks;
using PTager.BL.Data.Store; namespace PTager.BL.Data.Repos
{
using M = Book;
public class BookRepo : RepoBase, IBookRepo
{
public BookRepo(BLDbContext context) : base(context)
{
} public async Task InitAsync(M.InitSpec spec)
=> await _context.Book_Init(spec.ToJson());
}
}
/Store/BLDbContext.cs
public async Task Book_Init(string json)
=> await this.ExecuteMethodCallAsync(nameof(Book_Init), args: json);
记开发个人图书收藏清单小程序开发(九)Web开发——新增图书信息的更多相关文章
- 记开发个人图书收藏清单小程序开发(十)DB开发——新增图书信息
昨晚完成了Web端新增图书信息的功能,现在就差DB的具体实现了. 因为我把Book相关的信息拆分的比较多,所以更新有点小麻烦. 首先,我需要创建一个Book Type的Matter: 然后,将图片路径 ...
- 记开发个人图书收藏清单小程序开发(三)DB设计
主要是参考豆瓣的图书查询接口: https://api.douban.com/v2/book/isbn/:9780132350884 返回内容如下: { "rating": { & ...
- 记开发个人图书收藏清单小程序开发(五)Web开发
决定先开发Web端试试. 新增Web应用: 选择ASP.NET Core Web Application,填写好Name和Location,然后点击OK. 注意红框标出来的,基于.NET Core 2 ...
- 记开发个人图书收藏清单小程序开发(六)Web开发
Web页面开发暂时是没有问题了,现在开始接上Ptager.BL的DB部分. 首先需要初始化用户和书房信息.因为还没有给其他多余的设计,所以暂时只有个人昵称和书房名称. 添加 Init Razor Pa ...
- 记开发个人图书收藏清单小程序开发(四)DB设计
早上起来,又改动了一下: 主要是,将非常用信息全部拆分出来,让Table尽量的小,小到不能继续拆分了,这样区分DB逻辑.增加了FileBank存储Book的封面图片,统一管理图片资源. 新添加的Typ ...
- 记开发个人图书收藏清单小程序开发(七)DB设计
前面的书房初始化的前端信息已经完善,所以现在开始实现DB的Script部分. 新增Action:Shelf_Init.sql svc.sql CREATE SCHEMA [svc] AUTHORIZA ...
- 微信小程序开发系列一:微信小程序的申请和开发环境的搭建
我最近也刚刚开始微信小程序的开发,想把我自学的一些心得写出来分享给大家. 这是第一篇,从零开始学习微信小程序开发.主要是小程序的注册和开发环境的搭建. 首先我们要在下列网址申请一个属于自己的微信小程序 ...
- 微信小程序开发系列五:微信小程序中如何响应用户输入事件
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...
- 微信小程序开发系列七:微信小程序的页面跳转
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...
随机推荐
- DrawGrid DrawFocusRect
http://docwiki.embarcadero.com/CodeExamples/XE7/en/GridLineWidth_%28C%2B%2B%29 void __fastcall TForm ...
- 使用WebLogic时控制台输出中文乱码解决方法
使用WebLogic时控制台输出中文乱码解决方法 1.找到weblogic安装目录,当前项目配置的domain 2.找到bin下的setDomainEnv.cmd文件 3.打开文件,从文件最后搜索第一 ...
- 行为型-命令模式(Command)
装修新房的最后几道工序之一是安装插座和开关,通过开关可以控制一些电器的打开和关闭,例如电灯或者排气扇.在购买开关时,我们并不知道它将来到底用于控制什么电器,也就是说,开关与电灯.排气扇并无直接关系,一 ...
- iscroll源码学习(1)
iscroll是移端端开发的两大利器之一(另一个是fastclick),为了将它整合的avalon,需要对它认真学习一番.下面是我的笔记. 第一天看的是它的工具类util.js //用于做函数节流 v ...
- awk使用
[awk使用] 例:awk -F ':' '{print $1"\t"$7}' 参考:http://www.cnblogs.com/ggjucheng/archive/2013/0 ...
- unity3d 移动与旋转 1
移动与旋转 1 player角色随asdw按键左右上下移动并旋转 public void Update() { // Reset player rotation to look in the same ...
- PHP - MongoDB连接攻略
http://blog.163.com/lgh_2002/blog/static/44017526201261111044608/ 使用PHP的Mongo扩展连接MongoDB.通过new Mongo ...
- 使用mybatis提供的各种标签方法实现动态拼接Sql。使用sql片段提取重复的标签内容
Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下: <select id="findUserByNameAndSex" par ...
- C语言中的序列点和副作用
参考: http://www.2cto.com/kf/201210/161225.html
- php识别二维码
php-zbarcode 是 PHP 读取条形码的扩展模块,目前仅支持 php5.x