сделал отправку на почту и присваивание ачивки за ранее
This commit is contained in:
@ -2,7 +2,10 @@ using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
using Hangfire;
|
||||
using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
using MimeKit;
|
||||
namespace CyberBoom.Controllers;
|
||||
|
||||
[ApiController]
|
||||
@ -32,16 +35,19 @@ public class UserWriteToMetingController : ControllerBase
|
||||
|
||||
var user = await _applicationContext.Users.FirstAsync(u => u.Id == write.UserId);
|
||||
|
||||
var newStats = await _applicationContext.GetStatistic(write.UserId);
|
||||
|
||||
var delay = meeting.Time - DateTime.UtcNow - new TimeSpan(2, 0, 0);
|
||||
|
||||
BackgroundJob.Schedule<IEmailService>((_emailService) => _emailService.SendEmailAsync(user.Email!, "Вход в Муза", $"Здравcтвуйте. У вас мероприятие через 2 часа под названием {meeting.Title}"), delay); // за начало до встречи
|
||||
|
||||
var achievments = await WriteAchievment(newStats, write.UserId);
|
||||
BackgroundJob.Schedule<ApplicationContext>((context) => AfterEnd(write.UserId, context), meeting.Time - DateTime.UtcNow); // за начало до встречи
|
||||
|
||||
await _applicationContext.SaveChangesAsync();
|
||||
|
||||
return Ok(new { dbWr.Id, Achievments = achievments });
|
||||
return Ok(new { dbWr.Id});
|
||||
}
|
||||
|
||||
async Task<List<Achievment>> WriteAchievment(StatsData stats, string userId)
|
||||
async Task<List<Achievment>> WriteAchievment(StatsData stats, string userId, ApplicationContext context)
|
||||
{
|
||||
List<Achievment> achievments = new List<Achievment>();
|
||||
if (stats.Count > 0 && stats.Count % 5 == 0)
|
||||
@ -54,8 +60,9 @@ public class UserWriteToMetingController : ControllerBase
|
||||
UserId = userId
|
||||
};
|
||||
achievments.Add(achievment);
|
||||
await _applicationContext.Achievments.AddAsync(achievment);
|
||||
await context.Achievments.AddAsync(achievment);
|
||||
}
|
||||
|
||||
var achievedTags = stats.StatsByTag.Where(st => st.Count > 0 && st.Count % 5 == 0);
|
||||
if (achievedTags.Count() > 0 && stats.Count % 5 == 0)
|
||||
{
|
||||
@ -69,7 +76,7 @@ public class UserWriteToMetingController : ControllerBase
|
||||
UserId = userId
|
||||
};
|
||||
achievments.Add(achievment);
|
||||
await _applicationContext.Achievments.AddAsync(achievment);
|
||||
await context.Achievments.AddAsync(achievment);
|
||||
}
|
||||
}
|
||||
return achievments;
|
||||
@ -93,6 +100,16 @@ public class UserWriteToMetingController : ControllerBase
|
||||
return Ok();
|
||||
}
|
||||
|
||||
|
||||
async Task AfterEnd(string userId, ApplicationContext context)
|
||||
{
|
||||
var newStats = await context.GetStatistic(userId);
|
||||
|
||||
await WriteAchievment(newStats, userId, context);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Get(string id)
|
||||
{
|
||||
@ -112,3 +129,45 @@ public class UserWriteToMetingController : ControllerBase
|
||||
return Ok(reviews);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public interface IEmailService
|
||||
{
|
||||
public Task SendEmailAsync(string email, string subject, string message);
|
||||
}
|
||||
|
||||
public class EmailService : IEmailService
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private ILogger<EmailService> _logger;
|
||||
|
||||
public EmailService(IConfiguration configuration, ILogger<EmailService> logger)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task SendEmailAsync(string email, string subject, string message)
|
||||
{
|
||||
var emailMessage = new MimeMessage();
|
||||
|
||||
emailMessage.From.Add(new MailboxAddress(_configuration["Email:Name"], _configuration["Email:Address"]));
|
||||
emailMessage.To.Add(new MailboxAddress("", email));
|
||||
emailMessage.Subject = subject;
|
||||
emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Html)
|
||||
{
|
||||
Text = message
|
||||
};
|
||||
|
||||
using var client = new SmtpClient();
|
||||
client.Timeout = 15000;
|
||||
await client.ConnectAsync(_configuration["Email:Host"], int.Parse(_configuration["Email:Port"]!), SecureSocketOptions.None);
|
||||
await client.AuthenticateAsync(_configuration["Email:Address"], _configuration["Email:Password"]);
|
||||
await client.SendAsync(emailMessage);
|
||||
|
||||
_logger.LogInformation("Письмо отправлено на почту {Email}", email);
|
||||
|
||||
await client.DisconnectAsync(true);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user