记开发个人图书收藏清单小程序开发(七)DB设计
前面的书房初始化的前端信息已经完善,所以现在开始实现DB的Script部分。
新增Action:Shelf_Init.sql
svc.sql
1 CREATE SCHEMA [svc] 2 AUTHORIZATION [dbo];
Shelf_Init.sql
1 CREATE PROCEDURE [svc].[Shelf$Init](@json nvarchar(max)) 2 WITH ENCRYPTION 3 AS 4 BEGIN 5 SET NOCOUNT ON; 6 SET XACT_ABORT ON; 7 BEGIN TRY 8 BEGIN TRAN; 9 10 declare @nickName nvarchar(20), @shelfName nvarchar(20); 11 select @nickName=NickName, @shelfName=ShelfName 12 from openjson (@json, '$') 13 with ( 14 NickName nvarchar(20), 15 ShelfName nvarchar(20) 16 ); 17 18 insert core._Party(Type, Alias) select k._User, @nickName 19 from core.Party#Type() k; 20 declare @userID int=@@identity; 21 22 23 insert core._Party(PID, Type, Alias) select @userID, k._Shelf, @shelfName 24 from core.Party#Type() k; 25 26 COMMIT TRAN; 27 END TRY 28 BEGIN CATCH 29 if (xact_state() = -1) ROLLBACK TRAN; throw; 30 END CATCH 31 END
好了,我去试试前端能不能初始化信息进DB
....
在测试之前,我们需要实现一下Init Razor Pages代码:
Init.cshtml.cs
1 using M = Shelf; 2 public class InitModel : PageModel 3 { 4 private readonly IShelfRepo _shelfRepo; 5 public InitModel(IShelfRepo shelfRepo) 6 { 7 _shelfRepo = shelfRepo; 8 } 9 [BindProperty] 10 public InitInputModel Input { get; set; } 11 12 public void OnGet() 13 { 14 15 } 16 17 public async Task<IActionResult> OnPostAsync() 18 { 19 if (ModelState.IsValid) 20 { 21 await _shelfRepo.InitAsync(new M.InitSpec 22 { 23 NickName = Input.NickName.Trim(), 24 ShelfName = Input.ShelfName.Trim() 25 }); 26 return RedirectToPage("New"); 27 } 28 return Page(); 29 } 30 }
页面内容也需要修改一下form部分
Init.cshtml
1 <form method="post"> 2 <div class="form-group form-group-lg"> 3 <label asp-for="Input.NickName"></label> 4 <input class="form-control form-control-lg" asp-for="Input.NickName" autocomplete="off"> 5 <span asp-validation-for="Input.NickName" class="text-danger"></span> 6 </div> 7 <div class="form-group form-group-lg"> 8 <label asp-for="Input.ShelfName"></label> 9 <input class="form-control form-control-lg" asp-for="Input.ShelfName" autocomplete="off"> 10 <span asp-validation-for="Input.ShelfName" class="text-danger"></span> 11 </div> 12 <div class="form-group text-right"> 13 <button class="btn btn-warning btn-lg" type="submit">Save</button> 14 </div> 15 </form>
填写不动书房的信息:
点击Save按钮提交,OK,正常提交了并跳转了。
查看下DB有没有数据:
哈哈,看来一切正常。