记开发个人图书收藏清单小程序开发(九)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 ...
- 微信小程序开发系列一:微信小程序的申请和开发环境的搭建
我最近也刚刚开始微信小程序的开发,想把我自学的一些心得写出来分享给大家. 这是第一篇,从零开始学习微信小程序开发.主要是小程序的注册和开发环境的搭建. 首先我们要在下列网址申请一个属于自己的微信小程序 ...
- 微信小程序开发系列五:微信小程序中如何响应用户输入事件
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...
- 微信小程序开发系列七:微信小程序的页面跳转
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...
随机推荐
- 部分流媒体协议及流媒体开发框架vitamio
流媒体协议部分RTP.RTCP.RTSP.MMS.HLS.HTTP progressive streaming 流媒体协议:(RTP.RTCP.RTSP.MMS.HLS.HTTP progress ...
- C# 查找其他应用程序并打开、显示、隐藏、关闭
软件开发中,有时迫不得已要用到第三方的软件,这时就涉及到在C#应用程序需要对第三方软件打开.显示.隐藏以及关闭. 下面列举了几个常用的方式 打开应用程序,下面是2种简单用法: 第一种: public ...
- leetcode28
public class Solution { public int StrStr(string haystack, string needle) { return haystack.IndexOf( ...
- 【python 】装饰器 (多个参数的函数,带参数的装饰器)【转】
最简单的模板是这样的 #-*-coding:utf-8-*- def outer(func): def inner(): print 'before' func() print 'after' # r ...
- Java路程
Java学习这一部分其实也算是今天的重点,这一部分用来回答很多群里的朋友所问过的问题,那就是你是如何学习Java的,能不能给点建议?今天我是打算来点干货,因此咱们就不说一些学习方法和技巧了,直接来谈每 ...
- 【独家】阿里天池IJCAI17大赛第四名方案全解析(附代码)
[独家]阿里天池IJCAI17大赛第四名方案全解析(附代码) https://mp.weixin.qq.com/s?__biz=MzAxMzA2MDYxMw==&mid=2651560625& ...
- unity3d 移动与旋转 2
这次的代码示例是配合动画系统使用的 4.3新的动画系统允许动画带有位置偏移,只需要在Animator组件中勾选Apply Root Motion我们就可以使用它了. using UnityEngine ...
- jQuery的event事件
1.冒泡和默认行为 <div class="aa"> <div class="bb"> <div class="cc&q ...
- Zookeeper 源码(五)Leader 选举
Zookeeper 源码(五)Leader 选举 前面学习了 Zookeeper 服务端的相关细节,其中对于集群启动而言,很重要的一部分就是 Leader 选举,接着就开始深入学习 Leader 选举 ...
- 动态执行SQL语句,接收返回值
一.exec和sp_executesql介绍 当需要根据外部输入的参数来决定要执行的SQL语句时,常常需要动态来构造SQL查询语句.比如,一个比较通用的分页存储过程,可能需要传入表名,字段,过滤条件, ...