public ApiResponse <int> Add(BaseBookModel model) { if (model == null) { throw new Exception("无效的图书信息。"); } var response = new ApiResponse <int>() { Result = this.BookService.Add(model) }; return(response); }
public ActionResult Edit(BookModel model) { string action = Request["Submit"]; if (action == "cancel") { return(RedirectToAction("Index")); } if (ModelState.IsValid) { try { if (model.Id > 0) { this.BookService.UpdateBookInfo(model); } else { var book = new BaseBookModel() { Name = model.Name, ISBN = model.ISBN, Author = model.Author, Desc = model.Desc, Source = model.Source, Donor = model.Donor, Type = model.Type }; this.BookService.Add(book); } return(RedirectToAction("Index")); } catch (Exception ex) { ViewBag.Message = ex.Message; } } return(View(model)); }
/// <summary> /// 添加图书 /// </summary> /// <param name="model">图书基本信息</param> /// <returns>是否添加成功</returns> public int Add(BaseBookModel model) { if (model == null) { throw new InvalidOperationException("图书信息无效。"); } if (string.IsNullOrEmpty(model.Name)) { throw new InvalidOperationException("图书名称不能为空。"); } if (model.Source == BookSource.None) { throw new InvalidOperationException("未选择图书来源。"); } var book = new Book() { Name = model.Name, ISBN = model.ISBN, Author = model.Author, Desc = model.Desc, Source = (int)model.Source, Donor = model.Donor, Type = model.Type, Status = (int)BookStatus.Stored, CreatedDate = DateTime.Now, BarCode = Guid.NewGuid().ToString() }; using (var dbContext = new MissionskyOAEntities()) { book = dbContext.Books.Add(book); dbContext.SaveChanges(); } return(book.Id); }