示例#1
0
        public void ReturnBook(BookId bookId, BookAmount amount)
        {
            var book = Books.First(a => a.BookId == bookId);

            if (book.BookAmount == amount)
            {
                Books.RemoveAll(a => a.BookId == bookId);
            }
            else if (book.BookAmount > amount)
            {
                Books.RemoveAll(a => a.BookId == bookId);
                Books.Add(new BookRecord(book.BookId, book.BookAmount - amount, book.Title));
            }
            else
            {
                throw new ArgumentException($"Cannot return book with title {book.Title.Value}. Amount to return is higher than {book.BookAmount.Amount}");
            }

            AddEvent(new BookRemovedFromLibraryRecord(bookId, amount.Amount, (LibraryRecordId)Id));

            if (ReturnDate < DateTime.UtcNow)
            {
                var oldFineValue = ReturnFine.Value;
                var daysBetweenTodayAndReturnDate = ((DateTime.UtcNow - ReturnDate)).Days;
                var newFineValue = daysBetweenTodayAndReturnDate * 10;
                ReturnFine = new ReturnFine(oldFineValue + newFineValue);
            }

            if (ReturnFine.Value == 0)
            {
                IsClosed = true;
            }
        }
示例#2
0
 private LibraryRecord(LibraryRecordId id, UserRecord user, List <BookRecord> books)
 {
     Id = id;
     if (user.IsNotBanned)
     {
         User = user;
     }
     else
     {
         throw new InvalidEntityStateException();
     }
     this.CreatedDate = DateTime.UtcNow;
     this.ReturnFine  = new ReturnFine(0);
     Books            = books;
     foreach (var book in books)
     {
         AddEvent(new BookAddedToLibraryRecord(book.BookId, id, book.BookAmount.Amount));
     }
 }