Advertisement
Danny_Berova

BorrowBook

Jul 18th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1. namespace MyLibrary.App.Pages.Books
  2. {
  3.     using Data;
  4.     using MyLibrary.Models;
  5.     using Microsoft.AspNetCore.Mvc;
  6.     using Microsoft.AspNetCore.Mvc.RazorPages;
  7.     using Microsoft.AspNetCore.Mvc.Rendering;
  8.     using System;
  9.     using System.Threading.Tasks;
  10.  
  11.     public class BorrowModel : PageModel
  12.  
  13.     {
  14.         private readonly MyLibraryDbContext context;
  15.  
  16.         public BorrowModel(MyLibraryDbContext context)
  17.         {
  18.             this.context = context;
  19.         }
  20.  
  21.         [BindProperty]
  22.         public int Id { get; set; }
  23.  
  24.         [BindProperty]
  25.         public int BorrowerId { get; set; }
  26.  
  27.         [BindProperty]
  28.         public DateTime BorrowDate { get; set; }
  29.  
  30.         [BindProperty]
  31.         public DateTime ReturnDate { get; set; }
  32.  
  33.                
  34.         public BookBorrower BookBorrower { get; set; }
  35.  
  36.         public IActionResult OnGet(int id)
  37.         {
  38.             InitializeBorrowers();
  39.             return Page();
  40.         }
  41.  
  42.  
  43.         public async Task<IActionResult> OnPostAsync()
  44.         {
  45.             this.BookBorrower = new BookBorrower()
  46.             {
  47.                 BookId = this.Id,
  48.                 BorrowerId = this.BorrowerId,
  49.                 BorrowDate = this.BorrowDate,
  50.                 ReturnDate = this.ReturnDate
  51.             };
  52.  
  53.             if (!this.ModelState.IsValid)
  54.             {
  55.                 InitializeBorrowers();
  56.                 return Page();
  57.             }
  58.  
  59.             try
  60.             {
  61.                 this.context.BookBorrowers.Add(this.BookBorrower);
  62.                 await this.context.SaveChangesAsync();
  63.  
  64.                 var book = this.context.Books.Find(Id);
  65.                 book.IsBorrowed = true;
  66.                 await this.context.SaveChangesAsync();
  67.  
  68.                 this.TempData["Success"] = $@"You land book ""{this.BookBorrower.Book.Title}"" to a frend!";
  69.  
  70.             }
  71.             catch (Exception e)
  72.             {
  73.                 this.TempData["Success"] = "Unsuccessfull operation!";
  74.  
  75.                 InitializeBorrowers();
  76.                 return Page();
  77.             }
  78.  
  79.             return RedirectToPage("/Index");
  80.         }
  81.  
  82.         private void InitializeBorrowers()
  83.         {
  84.             this.ViewData["BorrowerId"] = new SelectList(this.context.Borrowers, "Id", "Name");
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement