/* * Use Task.WhenAll to optimize the code to send newsletters * * Optimize the GenerateNewsletter method by using StringBuilder * Refactor the HttpClient in SendMail to automatically dispose itself */ public async Task SendNewsletter(string body) { var batchSize = 100; var page = 1; var areItemsLeft = true; while (areItemsLeft) { var customers = await _customerReader.GetCustomersAsync( new Shared.Models.PageModel() { Page = page, PageSize = batchSize }); if (customers.PageItems == 0) { areItemsLeft = false; break; } foreach (var customer in customers.Items) { var html = _mailGenerator.GenerateNewsletter(body, customer.FirstName); await _mailClient.SendMail(html, customer.Email); } page++; } }
public async Task <IOutput> Handle(ResolvePracticeInput request, CancellationToken cancellationToken) { var practice = await _context.PracticeOrders .Include(x => x.Author) .Include(x => x.Lesson) .FirstOrDefaultAsync(x => x.Id == request.PracticeId, cancellationToken: cancellationToken); if (practice is null) { return(ActionOutput.Error("Заявка на прохождение не нашлась")); } if (practice.IsDone) { return(ActionOutput.Error("Решение уже принято")); } if (practice.AuthorId == request.TeacherId) { return(ActionOutput.Error("Нельзя принять практику у себя")); } practice.IsDone = true; practice.IsResolved = true; practice.TeacherId = request.TeacherId; _context.PracticeOrders.Update(practice); await _context.SaveChangesAsync(cancellationToken); var progress = new UserProgress(practice.Author, practice.Lesson); await _context.Progresses.AddAsync(progress, cancellationToken); await _context.SaveChangesAsync(cancellationToken); _mailClient.SendMail(practice.Author.Mail, message => { message.Subject = $"Практический урок {practice.Lesson.Name} пройден."; message.Body = _templateCreator.GetResolveLetter(practice.Lesson.Name); }); return(ActionOutput.Success); }
/* * Use Task.WhenAll to optimize the code to send newsletters * * Optimize the GenerateNewsletter method by using StringBuilder * Refactor the HttpClient in SendMail to automatically dispose itself */ public async Task SendNewsletter(string body) { var batchSize = 100; var page = 1; var areItemsLeft = true; while (areItemsLeft) { var customers = await _customerReader.GetCustomersAsync( new Shared.Models.PageModel() { Page = page, PageSize = batchSize }); if (customers.PageItems == 0) { areItemsLeft = false; break; } //foreach (var customer in customers.Items) //{ // var html = _mailGenerator.GenerateNewsletter(body, customer.FirstName); // await _mailClient.SendMail(html, customer.Email); //} var tasks = new List <Task>(customers.PageItems); foreach (var customer in customers.Items) { var html = _mailGenerator.GenerateNewsletter(body, customer.FirstName); tasks.Add(_mailClient.SendMail(html, customer.Email)); } await Task.WhenAll(tasks); page++; } }
public bool SendMail(IMailClient mailClient) { return(mailClient.SendMail(this.From, this.To, this.Subject, this.Body)); }
public bool SendMail(IMailClient mailClient) { return(mailClient.SendMail(From, To, Subject, Body)); }