Пример #1
0
        public async Task <IActionResult> AddNewSubscriber(Subscriber newSub)
        {
            int output = await _databaseClient.ExecuteModifyQuery(
                $"INSERT INTO Subscribers (MailAddress, IsVerify, StationId, IsSendVerifyCode) VALUES " +
                $"('{newSub.MailAddress}', {newSub.IsVerify}, {newSub.StationId}, {newSub.IsSendVerifyCode})");

            return(output == 1 ? Ok() : NoContent());
        }
Пример #2
0
        private async Task SendVerifyMail()
        {
            IEnumerable <Subscriber> newSubscribers = allSubscribers.FindAll(s => s.IsSendVerifyCode == 0);

            foreach (Subscriber newSub in newSubscribers)
            {
                if (newSub.IsSendVerifyCode == 1)
                {
                    continue;
                }

                foreach (Subscriber subToUpdate in newSubscribers.Where(s => s.MailAddress == newSub.MailAddress))
                {
                    int isUpdate = await _databaseClient.ExecuteModifyQuery($"UPDATE Subscribers SET [IsSendVerifyCode] = 1 WHERE [Id] = {subToUpdate.Id}");

                    subToUpdate.IsSendVerifyCode = 1;
                }

                _logger.LogInformation("IsSendVerifyCode updated for {mailAddress} at: {time}", newSub.MailAddress, DateTimeOffset.Now);

                string verifyCode = EncryptionHelper.GetMd5Hash(newSub);

                IEnumerable <int> stationsID = newSubscribers
                                               .Where(s => s.MailAddress == newSub.MailAddress)
                                               .Select(s => s.StationId);

                IEnumerable <string> stationsName = allStations
                                                    .Where(station => stationsID.Contains(station.id))
                                                    .Select(station => station?.stationName);

                string mailTemplate = _mailTemplateManager.GetTemplate(TemplateType.Confirm).Run(new
                {
                    KleinAppAddress = $"{_configuration.GetSection("KleinAppAddress").Value}daily/userId/{newSub.Id}/code/{verifyCode}",
                    VerifyCode      = verifyCode,
                    StationsCount   = stationsName.Count(),
                    UserId          = newSub.Id
                });

                _smtpClient.SendMail("*****@*****.**", "*****@*****.**", "KleinMap verify code", mailTemplate);
                _logger.LogInformation("Sended verify e-mail to {mailAddress} at: {time}", newSub.MailAddress, DateTimeOffset.Now);
            }
        }