123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Dynamic.Core;
- using System.Net.Mail;
- using System.Text;
- using UnivateProperties_API.Context;
- using UnivateProperties_API.Helpers;
- using UnivateProperties_API.Model.Communication;
- using UnivateProperties_API.Model.Users;
-
- namespace UnivateProperties_API.Repository.Communication
- {
- public interface IMailRepository
- {
- void ContactUs(MailModel mm);
- void EnquireNow(MailModel mm);
- void AddRecipient(MailRecipient rec);
- List<MailRecipient> GetMailRecipients();
- MailRecipient GetMailRecipientById(int id);
- void UpdateMailRecipient(MailRecipient rec);
- void DeleteMailRecipient(int id);
- }
-
- public class MailRepository : IMailRepository
- {
- private readonly DataContext _dbContext;
-
- public MailRepository(DataContext db)
- {
- _dbContext = db;
- }
-
- public MailRepository()
- {
- }
-
- public void ContactUs(MailModel mm)
- {
- string property = mm.Property;
- string phone = mm.Phone;
- string name = mm.Name;
- string email = mm.Email;
- string message = mm.Message;
- var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "ContactUs").ToList();
- string body = "<div style=\"margin: 5px\">" +
- "<h4>Contact from: " + name + "!</h4>" +
- "<h4>Email: " + email + "</h4>" +
- "<h4>Phone: " + phone + "</h4>" +
- "<h4>Property: " + property + "</h4>" +
- "<div>" +
- "<h4>Message: </h4>" +
- "<p>" + message + "</p>" +
- "</div>" +
- "</div>" +
- "</div>";
-
- string toList = "";
- int emailCount = 0;
- foreach (var recipient in recipients)
- {
- toList += recipient.RecipientMail + ";, ";
- emailCount++;
- }
-
- if (toList.Length > 0)
- {
- if (emailCount == 1)
- toList = toList.Substring(0, toList.Length - 3);
- else
- toList = toList.Substring(0, toList.Length - 2);
- }
-
- var host = _dbContext.Hosts.FirstOrDefault();
- using (SmtpClient smtp = new SmtpClient(host.Host))
- {
- MailMessage mail = new MailMessage();
- mail.To.Add(toList);
- mail.Subject = "Uni-Vate - New Contact Request";
- mail.Body = body;
- mail.IsBodyHtml = true;
- mail.BodyEncoding = Encoding.ASCII;
- mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
- mail.Sender = new MailAddress(host.User, "UniVate Properties");
- mail.From = new MailAddress(mm.Email, mm.Name);
-
- smtp.UseDefaultCredentials = host.NeedsAuthorize;
- smtp.Credentials = host.GetNetworkCredential();
- smtp.EnableSsl = host.UseSSL;
- smtp.Send(mail);
- }
- }
-
- public void EnquireNow(MailModel mm)
- {
- string phone = mm.Phone;
- string name = mm.Name;
- string email = mm.Email;
- string message = mm.Message;
- var prop = _dbContext.Properties.Where(p => p.Id == int.Parse(mm.Property)).FirstOrDefault();
- var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "EnquireNow").ToList();
- string body = "<div style=\"margin: 5px\">" +
- "<h4>Contact from: " + name + "!</h4>" +
- "<h4>Email: " + email + "</h4>" +
- "<h4>Phone: " + phone + "</h4>" +
- "<div>" +
- "<h4>Property: </h4>" +
- "<p>" + prop.Id + "</p>" +
- "<p>" + prop.PropertyName + "</p>" +
- "<p>" + prop.PropertyRef + "</p>" +
- "<p>" + prop.Price + "</p>" +
- "</div>" +
- "</div>" +
- "</div>";
-
- string toList = "";
- foreach (var recipient in recipients)
- {
- toList += recipient.RecipientMail + ";, ";
- }
-
- if (toList.Length > 0)
- toList = toList.Substring(0, toList.Length - 2);
-
- var host = _dbContext.Hosts.FirstOrDefault();
- using (SmtpClient smtp = new SmtpClient(host.Host))
- {
- MailMessage mail = new MailMessage();
- mail.To.Add(toList);
- mail.Subject = "Uni-Vate - Enquiry to view property";
- mail.Body = body;
- mail.IsBodyHtml = true;
- mail.BodyEncoding = Encoding.ASCII;
- mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
- mail.Sender = new MailAddress(host.User, "UniVate Properties");
- mail.From = new MailAddress(mm.ToAddress, "Admin");
-
- smtp.UseDefaultCredentials = host.NeedsAuthorize;
- smtp.Credentials = host.GetNetworkCredential();
- smtp.EnableSsl = host.UseSSL;
- smtp.Send(mail);
- }
- }
-
- public void ForgotPassword(Individual toPerson, string link)
- {
- string body = "<div style=\"margin: 5px\">" +
- "<h4>Dear " + toPerson.FullName + "</h4>" +
- "<h4>There has been a request to reset your password. If this is incorrect please send an email to info@univateproperties.co.za</h4>" +
- "<div>" +
- "<h4><a href='" + link + "'>Click here</a> to reset your password.</h4>" +
- "<br />"+
- "<h4>Thank You</h4>" +
- "<h4>Team Uni-Vate</h4>" +
- "</div>" +
- "</div>";
-
- var host = _dbContext.Hosts.FirstOrDefault();
- using (SmtpClient smtp = new SmtpClient(host.Host))
- {
- MailMessage mail = new MailMessage();
- mail.To.Add(new MailAddress(toPerson.Email, toPerson.FullName));
- mail.Subject = "Uni-Vate - Password Reset Request";
- mail.Body = body;
- mail.IsBodyHtml = true;
- mail.BodyEncoding = Encoding.ASCII;
- mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
- mail.Sender = new MailAddress(host.User, "UniVate Properties");
- mail.From = mail.Sender;
-
- smtp.UseDefaultCredentials = host.NeedsAuthorize;
- smtp.Credentials = host.GetNetworkCredential();
- smtp.EnableSsl = host.UseSSL;
- smtp.Send(mail);
- }
- }
-
- public void AddRecipient(MailRecipient rec)
- {
- if (MyCommon.IsValidEmail(rec.RecipientMail))
- {
- _dbContext.MailRecipients.Add(rec);
- _dbContext.SaveChanges();
- }
- else
- {
- throw new Exception();
- }
-
- }
-
- public List<MailRecipient> GetMailRecipients()
- {
- return _dbContext.MailRecipients.Where(x => x.IsDeleted == false).ToList();
- }
-
- public MailRecipient GetMailRecipientById(int id)
- {
- return _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
- }
-
- public void UpdateMailRecipient(MailRecipient rec)
- {
- var recipient = _dbContext.MailRecipients.Where(x => x.Id == rec.Id).FirstOrDefault();
-
- if (recipient.RecipientMail != rec.RecipientMail)
- {
- recipient.RecipientMail = rec.RecipientMail;
- }
-
- if (recipient.RecipientName != rec.RecipientName)
- {
- recipient.RecipientName = rec.RecipientName;
- }
-
- if (recipient.RecipientUsage != rec.RecipientUsage)
- {
- recipient.RecipientUsage = rec.RecipientUsage;
- }
-
-
- _dbContext.MailRecipients.Update(recipient);
- _dbContext.SaveChanges();
- }
-
- public void DeleteMailRecipient(int id)
- {
- var rec = _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
- _dbContext.MailRecipients.Remove(rec);
- _dbContext.SaveChanges();
- }
- }
- }
|