/// <summary> /// Returns report on users with borrowed tapes on given loan date (if provided, otherwise on this day) /// for at least the amount of days corresponding to loan duration (if provided, otherwise any duration) /// </summary> /// <param name="LoanDate">Date to base record of users borrowing tapes on</param> /// <param name="LoanDuration">Duration in days to base record of users borrowing tapes for</param> /// <returns>List of user borrow record as report</returns> public List <UserDTO> GetUsersReportAtDateForDuration(DateTime?LoanDate, int?LoanDuration) { DateTime loanDate = LoanDate.HasValue ? LoanDate.Value : DateTime.Now; var allUsersAndBorrows = Mapper.Map <List <UserDTO> >(_userRepository.GetAllUsers()); List <UserDTO> usersFiltered = new List <UserDTO>(); var borrowRecords = _borrowRecordRepository.GetAllBorrowRecords(); foreach (var user in allUsersAndBorrows) { // Get borrow records for each user var userBorrowRecords = borrowRecords.Where(u => u.UserId == user.Id); foreach (var record in userBorrowRecords) { // Get detailed borrow record for each user borrow IF tape was on loan at provided loan date // AND for a given duration (if provided) if (MatchesLoanDate(loanDate, record.BorrowDate, record.ReturnDate) && MatchesDuration(LoanDuration, loanDate, record.BorrowDate)) { usersFiltered.Add(user); break; } } } return(usersFiltered); }
/// <summary> /// Bases recommendation first and formost on common borrows, i.e. if user requesting recommendation has had some /// of the same tapes on loan as another user, they will get recommendation of that common borrower's previously borrowed tapes (if user hasn't seen them) /// If that fails, recommend highest rated tape in system that user has not seen /// If that fails, recommend the newest relesed tape in system that user has not seen /// If that fails, no recommendation can be made for user /// </summary> /// <param name="UserId">Id of user requesting recommendation</param> /// <returns>Tape recommendation</returns> public TapeRecommendationDTO GetRecommendationForUser(int UserId) { // Check if user exists, throw error if not var user = _userRepository.GetAllUsers().FirstOrDefault(u => u.Id == UserId); if (user == null) { throw new ResourceNotFoundException($"User with id {UserId} was not found."); } // Extact user borrow records, user borrow records and all tapes var allRecords = _borrowRecordRepository.GetAllBorrowRecords(); var userBorrowRecords = allRecords.Where(r => r.UserId == UserId); var allTapes = _tapeRepository.GetAllTapes(); // Attempt to get recommendation from common borrows var recommendation = GetRecommendationOfTapeFromCommonBorrows(UserId, allRecords, userBorrowRecords, allTapes); // If that did not work, attempt to get recommendation for highest rated tapes if (recommendation == null) { recommendation = GetRecommendationFromReviews(userBorrowRecords, allRecords, allTapes); } // If that did not work, attempt to get recommendation for newest tape user has not seen if (recommendation == null) { recommendation = GetRecommendationForNewestTapes(userBorrowRecords, allRecords, allTapes); } // If all fails we send not found error back to user because they seem to have rented all videos in system if (recommendation == null) { throw new ResourceNotFoundException("User has had all tapes on loan that are currently available in system"); } // Otherwise return the recommendation else { return(recommendation); } }
/// <summary> /// Returns tapes filtered by that they were being loaned to a user at a given date /// </summary> /// <param name="LoanDate">Date to use to output borrow records for</param> /// <returns>List of tape borrow record as report</returns> public List <TapeDTO> GetTapeReportAtDate(DateTime LoanDate) { List <TapeDTO> tapesOnLoan = new List <TapeDTO>(); var allTapes = Mapper.Map <List <TapeDTO> >(_tapeRepository.GetAllTapes()); var allTapeBorrows = _borrowRecordRepository.GetAllBorrowRecords(); foreach (var tape in allTapes) { var tapeBorrows = allTapeBorrows.Where(t => t.TapeId == tape.Id); foreach (var tapeBorrow in tapeBorrows) { if (MatchesLoanDate(LoanDate, tapeBorrow.BorrowDate, tapeBorrow.ReturnDate)) { tapesOnLoan.Add(tape); break; } } } return(tapesOnLoan); }