记开发个人图书收藏清单小程序开发(九)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 ...
- 微信小程序开发系列一:微信小程序的申请和开发环境的搭建
我最近也刚刚开始微信小程序的开发,想把我自学的一些心得写出来分享给大家. 这是第一篇,从零开始学习微信小程序开发.主要是小程序的注册和开发环境的搭建. 首先我们要在下列网址申请一个属于自己的微信小程序 ...
- 微信小程序开发系列五:微信小程序中如何响应用户输入事件
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...
- 微信小程序开发系列七:微信小程序的页面跳转
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...
随机推荐
- [RHEL7.1]关闭防火墙及SElinux
一.关闭防火墙 1. 先查看防火墙状态 [root@bogon ~]# 1 systemctl status firewalld firewalld.service - firewalld - dyn ...
- CB XE7 C11 64位编译器 成员变量初始化
看到了C++11,看到了XE7的64位,想实现下面方便的类成员初始化,失望. 一.64位用法 clang3,64位编译器,不支持中文变量名,编写代码提示没有32位快,风格简单不用写单独的赋值语句函数, ...
- VMware 克隆网卡无法启动
问题描述: 最近学习 hadoop,环境准备搭建在虚拟机之上,装好一台虚拟机克隆完成后,网卡无法启动. 多年前,初学 Linux 的时候,就遇到过这个问题,记录的笔记找不到了,简单记录一下. shel ...
- Hive的安装和建表
目录 认识Hive 1. 解压 2. 配置mysql metastore(切换到root用户) 3. 配置hive 4. 安装hive和mysq完成后,将MySQL的连接jar包拷贝到$HIVE_HO ...
- CentOS 7 调整 home分区 扩大 root分区
总体过程: 把/home内容备份,然后将/home文件系统所在的逻辑卷删除,扩大/root文件系统,新建/home ,恢复/home内容 1.查看分区 df -h 2.备份home分区文件 tar c ...
- Navigation and Pathfinding
[Navigation and Pathfinding] 术语: 1)NavMesh 2)NavMesh Agent 3)Off-Mesh Link 4)NavMesh Obstacle A comm ...
- Unix高级编程Note1
[Unix Notes] 1./etc/passwd 2.extern int errno; 3.限制, limit.h 4.文件原子操作:O_EXCL & O_CREAT 5.stat操作 ...
- 更改Mysql数据库数据存储位置的具体步骤
首先把mysql的服务先停掉,更改MySQL配置文件My.ini中的数据库存储主路径,将老的数据库存储主路径中的数据库文件和文件夹复制到新的存储主路径,接下来重启搞定. 一.首先把mysql的服务先停 ...
- 17.Letter Combinations of a Phone Number(Back-Track)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- git的基本命令
在当前目录新建一个git代码库$ git init 设置提交代码时的用户信息:$ git config [--global] user.name "[name]"$ git con ...