/// <summary> /// Creates the specified item. /// </summary> /// <param name="item">The item.</param> public void Create(BookRentEvent item) { this.ValidateBookRentEvent(item); this.context.BookRentEvents.Add(item); this.context.SaveChanges(); }
/// <summary> /// Updates the specified item. /// </summary> /// <param name="item">The item.</param> /// <returns> /// Returns the updated item. /// </returns> public BookRentEvent Update(BookRentEvent item) { this.ValidateBookRentEvent(item); var bookRentEvent = this.context.BookRentEvents.Include(br => br.Book).Include(br => br.User) .FirstOrDefault(br => br.BookId == item.BookId && br.UserId == item.UserId && br.DateOfRenting == item.DateOfRenting && br.DateOfReturn == null); //The book was returned if DateOfReturn is not null bookRentEvent.NumberOfCopiesReturned += item.NumberOfCopiesReturned; if (item.NumberOfCopiesReturned == bookRentEvent.NumberOfCopiesRented) { bookRentEvent.DateOfReturn = DateTime.Today; } this.context.Entry(bookRentEvent).State = EntityState.Modified; this.context.SaveChanges(); return(bookRentEvent); }
/// <summary> /// Validates the book rent event. /// </summary> /// <param name="item">The item.</param> /// <exception cref="ArgumentNullException">item</exception> /// <exception cref="ValidationException"> /// Renting user" + basicValidationExceptionMessage /// or /// Renting book" + basicValidationExceptionMessage /// or /// Renting dates" + basicValidationExceptionMessage /// </exception> private void ValidateBookRentEvent(BookRentEvent item) { var basicValidationExceptionMessage = " invalid. Please re-input the data and try again."; if (item == null) { throw new ArgumentNullException(nameof(item)); } if (item.UserId <= 0) { throw new ValidationException("Renting user" + basicValidationExceptionMessage); } if (item.BookId <= 0) { throw new ValidationException("Renting book" + basicValidationExceptionMessage); } if (item.DateOfRenting == DateTime.MinValue || item.DateToReturn == DateTime.MinValue) { throw new ValidationException("Renting dates" + basicValidationExceptionMessage); } }