コード例 #1
0
        public void Get_Assigned_Book()
        {
            var bookAssignment = new BookAssignToStudent
            {
                Id     = 1,
                BookId = 123,
                Book   = new Book {
                    Id = 123, Name = "Software Engineering", Author = "ABC", IsIssued = true
                },
                IssueDate = DateTime.Now,
                DueDate   = DateTime.Now.AddDays(7),
                StudentId = 100,
                Student   = new Student {
                    Id = 1, Name = "Ali"
                }
            };

            var mockBookService           = new Mock <IBookService>();
            var mockBookAssignmentService = new Mock <IBookAssignmentService>();

            mockBookAssignmentService.Setup(r => r.GetAssignedBook(123)).Returns(bookAssignment);
            var controller = new BooksController(mockBookService.Object, mockBookAssignmentService.Object);

            var actionResult = controller.Assign(123);
            var result       = Assert.IsType <OkObjectResult>(actionResult);
            var assignedBook = Assert.IsType <BookAssignToStudent>(result.Value);

            Assert.True(assignedBook.Book.IsIssued);

            mockBookAssignmentService.Verify(s => s.GetAssignedBook(123), Times.Once());
        }
コード例 #2
0
        public void Extend_DueDate_Of_Assigned_Book()
        {
            var bookAssignment = new BookAssignToStudent
            {
                Id     = 1,
                BookId = 123,
                Book   = new Book {
                    Id = 123, Name = "Software Engineering", Author = "ABC", IsIssued = true
                },
                IssueDate = DateTime.Now,
                DueDate   = DateTime.Now.AddDays(7),
                StudentId = 100,
                Student   = new Student {
                    Id = 1, Name = "Ali"
                }
            };

            var mockBookService           = new Mock <IBookService>();
            var mockBookAssignmentService = new Mock <IBookAssignmentService>();
            var newDueDate = DateTime.Now.AddDays(10);

            mockBookAssignmentService.Setup(r => r.ExtendDueDate(123, 100, newDueDate)).Returns("Success");
            var controller = new BooksController(mockBookService.Object, mockBookAssignmentService.Object);

            var request = new ExtendDueDateRequest {
                BookId = 123, StudentId = 100, NewDueDate = newDueDate
            };
            var actionResult = controller.Extend(request);

            Assert.IsType <NoContentResult>(actionResult);

            mockBookAssignmentService.Verify(s => s.ExtendDueDate(123, 100, newDueDate), Times.Once());
        }
コード例 #3
0
        public string AssignBook(int bookId, int studentId)
        {
            var isValid = ValidateBookAndStudent(bookId, studentId);

            if (!isValid)
            {
                return("Either student or book does not exist!");
            }

            var isBookAlreadyIssued = _context.BookAssignToStudents.Any(a => a.Book.Id == bookId && a.Book.IsIssued == true);

            if (isBookAlreadyIssued)
            {
                return("Book is already issued to the student.");
            }

            var book = _context.Books.First(a => a.Id == bookId);

            book.IsIssued = true;

            _context.Update(book);

            var now = DateTime.Now;

            var student = _context.Students.FirstOrDefault(a => a.Id == studentId);

            var bookAssignToStudent = new BookAssignToStudent
            {
                BookId    = bookId,
                Book      = book,
                StudentId = studentId,
                Student   = student,
                IssueDate = now,
                DueDate   = now.AddDays(7)
            };

            _context.Add(bookAssignToStudent);
            _context.SaveChanges();

            return("Success");
        }