コード例 #1
0
        public async Task MonthlyReportForAdmin()
        {
            // monthly report for the admin about borrower(how many book borrowed, returned, overdued etc)
            // find the monthly entry(for the previous month as schedule will run first day of the month)
            var startOfTthisMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            var firstDay          = startOfTthisMonth.AddMonths(-1);
            var lastDay           = startOfTthisMonth.AddDays(-1);

            _borrowerEntity = await _context.Borrowers.Include(b => b.Book).Where(
                b => (b.Created >= firstDay && b.Created <= lastDay) || (b.Modified >= firstDay && b.Modified <= lastDay)).ToListAsync();

            // separate the entry as borrowed, returned, overdued,
            var borrowedPastMonth = _borrowerEntity.Where(b => b.BorrowStatus == BorrowStatus.Borrowed).ToList();
            var overduePastMonth  = _borrowerEntity.Where(b => b.BorrowStatus == BorrowStatus.Overdue).ToList();
            var returnedPastMonth = _borrowerEntity.Where(b => b.BorrowStatus == BorrowStatus.Returned).ToList();

            // make some list with email template
            // find the admin user email from the database
            _userEntity = await _context.ApplicationUsers.Where(u => u.Role == "Admin" && u.Status == UserStatus.Active).ToListAsync();

            _userEntity.ForEach(u =>
            {
                // send the email using u.Email with the data found few lines ago
                // currently sending the total number only
                //later email template will be used using details information
                var email   = u.Email;
                var subject = "The Monthly Report from Grace Chapel Book Library";
                var message = _emailTemplate.MakeTemplateForMonthlyReport(u, firstDay, borrowedPastMonth, returnedPastMonth, overduePastMonth);
                var result  = message.Result.ToString();
                _emailService.SendEmail(email, subject, result);
            });
        }