Exemplo n.º 1
0
        public async Task <int> UpdateNotificationAsync(NotficationCls entity)
        {
            if (_dbContext.ChangeTracker.QueryTrackingBehavior != QueryTrackingBehavior.NoTracking)
            {
                _dbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
            }
            try
            {
                TblNotification tblNotification = new()
                {
                    Id          = entity.Id,
                    MsgType     = entity.MsgType.ToString(),
                    MsgFrom     = entity.MsgFrom,
                    MsgTo       = entity.MsgTo,
                    MsgCc       = entity.MsgCC,
                    MsgSubject  = entity.MsgSubject,
                    MsgBody     = entity.MsgBody,
                    MsgSatus    = entity.MsgSatus.ToString(),
                    FailDetails = entity.FailDetails,
                    UpdatedDate = entity.UpdatedDate,
                };

                _dbContext.Entry(tblNotification).State = EntityState.Modified;

                //_dbContext.Set<T>().Update(entity);
                return(await _dbContext.SaveChangesAsync());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message + " ~ " + ex.InnerException);
                throw;
            }
        }
Exemplo n.º 2
0
        public async Task <int> SaveNotificationAsync(NotficationCls entity)
        {
            if (_dbContext.ChangeTracker.QueryTrackingBehavior != QueryTrackingBehavior.NoTracking)
            {
                _dbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
            }

            TblNotification tblNotification = new()
            {
                MsgFrom     = entity.MsgFrom,
                MsgTo       = entity.MsgTo,
                MsgSubject  = entity.MsgSubject,
                MsgBody     = entity.MsgBody,
                MsgSatus    = entity.MsgSatus.ToString(),
                MsgType     = entity.MsgType.ToString(),
                CreatedDate = entity.CreatedDate,
            };

            try
            {
                await _dbContext.Set <TblNotification>().AddAsync(tblNotification);

                return(await _dbContext.SaveChangesAsync());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message + " ~ " + ex.InnerException);
                throw;
            }
        }
        public async Task <bool> SendAsync(NotficationCls notfication)
        {
            switch (notfication.MsgType.ToString())
            {
            case "Mail":
                return(await SendMailAsync(notfication));

            case "SMS":
                return(true);

            case "Whatsapp":
                return(true);

            default:
                return(true);
            }
        }
        private async Task <bool> SendMailAsync(NotficationCls notfication)
        {
            try
            {
                // create email message
                MimeMessage email = new MimeMessage();
                email.From.Add(MailboxAddress.Parse(notfication.MsgFrom));
                email.To.Add(MailboxAddress.Parse(notfication.MsgTo));
                email.Subject = notfication.MsgSubject;
                email.Body    = new TextPart(TextFormat.Html)
                {
                    Text = notfication.MsgBody
                };

                // send email
                using SmtpClient smtp = new SmtpClient();
                smtp.Connect(APISetting.EmailConfiguration.SMTPAddress, APISetting.EmailConfiguration.Port, SecureSocketOptions.StartTls);
                smtp.Authenticate(APISetting.EmailConfiguration.UserId, APISetting.EmailConfiguration.Password);
                smtp.Send(email);
                smtp.Disconnect(true);

                notfication.MsgSatus    = NotificationStatus.Success.ToString();
                notfication.UpdatedDate = DateTime.Now;
                await _repository.UpdateNotificationAsync(notfication);

                return(true);
            }
            catch (Exception ex)
            {
                notfication.MsgSatus    = NotificationStatus.Fail.ToString();
                notfication.UpdatedDate = DateTime.Now;
                if (ex.InnerException != null)
                {
                    notfication.FailDetails = ex.Message + ex.InnerException.Message;
                }
                else
                {
                    notfication.FailDetails = ex.Message;
                }

                await _repository.UpdateNotificationAsync(notfication);

                return(false);
            }
        }