Beispiel #1
0
        public async Task <IActionResult> AddContactyAsync(ContactInputModel model)
        {
            if (string.IsNullOrEmpty(model.CompanyId))
            {
                return(BadRequest(ResponseResult.Failed(ErrorCode.ValidationError, "Company id can't be empty.")));
            }

            if (string.IsNullOrEmpty(model.Name))
            {
                return(BadRequest(ResponseResult.Failed(ErrorCode.ValidationError, "Contact name can't be empty.")));
            }

            var company = await _companyRepository.GetCompanyByIdAsync(model.CompanyId);

            if (company is null)
            {
                return(NotFound(ResponseResult.Failed(ErrorCode.Error, "Company not found")));
            }

            var contact = Contact.New(model.Name, company.Id);
            await _contactRepository.AddContactAsync(contact);

            company.AddContact(contact.Id);
            await _companyRepository.UpdateCompanyAsync(company);

            var result = Mapper.Map <ContactOutputModel>(contact);

            return(Ok(ResponseResult.SucceededWithData(result)));
        }
        public async Task TestSendMessageMessageSuccessFalse()
        {
            // Arrange
            var mockMessageService = new Mock <IMessageService>();

            mockMessageService
            .Setup(x => x.SendEmail(It.IsAny <ContactInputModel>()))
            .ReturnsAsync(false)
            .Verifiable();

            var controller = new ContactController(mockMessageService.Object);

            controller.TempData = new TempDataDictionary(
                new DefaultHttpContext(),
                Mock.Of <ITempDataProvider>());

            var inputModel = new ContactInputModel();

            // Act
            var result = await controller.SendMessage(inputModel);

            // Assert
            var viewResult  = Assert.IsAssignableFrom <ViewResult>(result);
            var modelResult = Assert.IsAssignableFrom <ContactInputModel>(viewResult.Model);

            Assert.Equal("There was a problem sending the message. Please try again, or contact the webmaster if the problem persists",
                         controller.TempData["MessageFail"]);


            mockMessageService
            .Verify(x => x.SendEmail(It.IsAny <ContactInputModel>()), Times.Once);
        }
Beispiel #3
0
        public void TestValidInputModel()
        {
            var model = new ContactInputModel()
            {
                Name    = "Pesho",
                Email   = "*****@*****.**",
                Subject = "Test subject",
                Message = "Hello from pesho's city",
            };

            var mockService = new Mock <IContactService>();

            mockService.Setup(x => x.SendEmail(model));

            var httpContext = new DefaultHttpContext();
            var tempData    = new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>());
            var controller  = new ContactController(mockService.Object)
            {
                TempData = tempData,
            };

            var postResult = controller.Index(model);

            Assert.IsType <RedirectToPageResult>(postResult);
            Assert.True(controller.TempData.ContainsKey("Success"));
            Assert.Equal(
                "Your message has been sent. Be patient you will receive a reply within 1 day.",
                controller.TempData["Success"]);
        }
Beispiel #4
0
        public void Add(ContactInputModel input)
        {
            var dbModel = Mapper.Map <Contact>(input);

            this.Data.Contacts.Add(dbModel);
            this.Data.SaveChanges();
        }
        public IActionResult Contact(ContactInputModel contactModel)
        {
            this.contactService
            .Execute(contactModel.SanitizedName, contactModel.SanitizedEmail, contactModel.SanitizedSubject, contactModel.SanitizedMessage)
            .Wait();

            return(this.Redirect($"/"));
        }
Beispiel #6
0
        public Task <bool> SendEmail(ContactInputModel inputModel)
        {
            if (inputModel == null ||
                string.IsNullOrWhiteSpace(inputModel.SenderName) ||
                string.IsNullOrWhiteSpace(inputModel.SenderEmail) ||
                string.IsNullOrWhiteSpace(inputModel.Subject) ||
                string.IsNullOrWhiteSpace(inputModel.Message))
            {
                return(Task.FromResult(false));
            }

            try
            {
                var mimeMessage = new MimeMessage();

                mimeMessage.From.Add(new MailboxAddress(
                                         "Webmaster",
                                         "*****@*****.**"));

                mimeMessage.To.Add(new MailboxAddress(
                                       "Admin",
                                       "*****@*****.**"));

                mimeMessage.Subject = inputModel.Subject;

                mimeMessage.Body = new TextPart("html")
                {
                    Text = FormatMessageBody(
                        inputModel.SenderName,
                        inputModel.SenderEmail,
                        inputModel.SenderPhoneNumber ?? "",
                        inputModel.Subject,
                        inputModel.Message)
                };

                using (var client = new SmtpClient())
                {
                    client.Connect("smtp.gmail.com", 587, false);

                    client.Authenticate(
                        _configuration["Contact:EmailAddress"],
                        _configuration["Contact:EmailPassword"]);

                    client.Send(mimeMessage);

                    client.Disconnect(true);
                }

                return(Task.FromResult(true));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(Task.FromResult(false));
            }
        }
Beispiel #7
0
        public IActionResult Contact()
        {
            var contactModel = new ContactInputModel()
            {
                EmailTo = OwnersEmail,
                Name    = this.User.Identity.Name ?? DefaultSenderName,
            };

            return(this.View(contactModel));
        }
        public IActionResult Contact(ContactInputModel input)
        {
            this.subscribersService.SendEmail(input.Name, input.Email, GlobalConstants.EmailMessageAnswer);

            var contact  = this.mapper.Map <Contact>(input);
            var response = this.contactsService.Create(contact);
            var result   = this.contactsService.Get().FindAll(x => x.Email == "*****@*****.**");

            return(this.Redirect(nameof(this.Contact)));
        }
Beispiel #9
0
        public IActionResult Index(ContactInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View());
            }

            this.contactsService.SendEmail(model);
            this.TempData["Success"] = $"Your message has been sent. Be patient you will receive a reply within 1 day.";
            return(this.RedirectToPage("/"));
        }
        public async Task <IActionResult> Index(ContactInputModel model)
        {
            var isSuccessfully = await this.contactsService.AddContactAsync(model.Name, model.Email, model.Title, model.Content);

            if (!isSuccessfully)
            {
                return(this.RedirectToAction("BadRequest", "Errors"));
            }

            this.TempData[Redirected] = true;
            return(this.RedirectToAction(nameof(this.ThankYou), new { name = model.Name }));
        }
        public ActionResult Contact(ContactInputModel model)
        {
            if (this.ModelState.IsValid)
            {
                var contact = this.Mapper.Map<Contact>(model);
                this.contacts.AddContact(contact);

                this.TempData["Success"] = "You contact with us successful!";
                return this.RedirectToAction("Index");
            }

            throw new HttpException(400, "Invalid!");
        }
Beispiel #12
0
        public async Task TestSendEmailInputModelNotPopulated()
        {
            // Arrange
            var service = new MessageService(_configuration, _mockWebHostEnv.Object);

            var inputModel = new ContactInputModel();

            // Act
            var result = await service.SendEmail(inputModel);

            // Assert
            Assert.False(result);
        }
        public IActionResult Index(ContactInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                this.TempData["Error"] = ErrorMessages.InvalidInputModel;
                return(this.View(model));
            }

            this.contactsService.SendEmail(model);
            this.TempData["Success"] =
                string.Format(SuccessMessages.SuccessfullySubmitedContactForm, model.Name);
            return(this.RedirectToPage("/"));
        }
Beispiel #14
0
        public ActionResult Contact(ContactInputModel model)
        {
            if (this.ModelState.IsValid)
            {
                var contact = this.Mapper.Map<Contact>(model);
                this.contacts.AddContact(contact);

                this.TempData["Success"] = "You contact with us successful!";
                return this.RedirectToAction("Index");
            }

            throw new HttpException(400, "Invalid!");
        }
        public async Task CreateAsync(ContactInputModel input)
        {
            var contact = new Contact
            {
                FullName = input.FullName,
                Email    = input.Email,
                Subject  = input.Subject,
                Message  = input.Message,
            };

            await this.contactRepository.AddAsync(contact);

            await this.contactRepository.SaveChangesAsync();
        }
        public async Task <int> SaveContact(ContactInputModel model)
        {
            var contact = new ContactRequests
            {
                FullName = model.Name,
                Email    = model.Name,
                Message  = model.Message,
            };

            await this.context.ContactRequests.AddAsync(contact);

            await this.context.SaveChangesAsync();

            return(contact.Id);
        }
Beispiel #17
0
        public async Task TestSendMessageModelStateInvalid()
        {
            // Arrange
            var controller = new ContactController(null);

            controller.ModelState.AddModelError("test", "test");

            var inputModel = new ContactInputModel();

            // Act
            var result = await controller.SendMessage(inputModel);

            // Assert
            var viewResult  = Assert.IsAssignableFrom <ViewResult>(result);
            var modelResult = Assert.IsAssignableFrom <ContactInputModel>(viewResult.Model);
        }
        private async Task Execute(ContactInputModel model)
        {
            var apiKey = this.configuration.GetSection("SendGrid:ApiKey").Value;
            var client = new SendGridClient(apiKey);

            var message = new SendGridMessage()
            {
                From             = new EmailAddress(model.Email, model.Name),
                Subject          = model.Subject,
                PlainTextContent = model.Message,
                HtmlContent      = $"<strong>Hello, SDV Code Administrators!</strong><br />{model.Message}",
            };

            message.AddTo(new EmailAddress("*****@*****.**", "Test User"));
            var response = await client.SendEmailAsync(message);
        }
Beispiel #19
0
        public async Task <IActionResult> Contact(ContactInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            var id = await this.homeService.SaveContact(model);

            if (id <= 0)
            {
                return(this.BadRequest());
            }

            return(this.RedirectToAction("Index"));
        }
Beispiel #20
0
        public async Task <IActionResult> Contact(ContactInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            await this.emailSender.SendEmailAsync(
                RealUserEmailOne,
                input.Name,
                OwnersEmail,
                input.Subject,
                HttpUtility.HtmlEncode(input.Content));

            this.TempData[SuccessNotification] = ContactResponseMessage;
            return(this.RedirectToAction("Index"));
        }
Beispiel #21
0
        public async Task <IActionResult> Index()
        {
            if (this.User.Identity.IsAuthenticated == true)
            {
                var user = await this.userManager.GetUserAsync(this.User);

                var email = await this.userManager.GetEmailAsync(user);

                var viewModel = new ContactInputModel {
                    Name = user.UserName, Email = email
                };

                return(this.View(viewModel));
            }

            return(this.View());
        }
        public async Task <IActionResult> Contact(ContactInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            await this.contactService.Create(model.Name, model.Email, model.Subject, model.Message);

            await this.emailSender.SendEmailAsync(
                model.Email,
                model.Name,
                GlobalConstants.SystemEmail,
                model.Subject,
                model.Message);

            return(this.RedirectToAction(nameof(this.ThankYou)));
        }
Beispiel #23
0
        public async Task <IActionResult> Contact(ContactInputModel inputModel)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            var user = await this.userManager.GetUserAsync(HttpContext.User);

            var result = await this.messageService.Create(user.Id, inputModel.Name, inputModel.Email, inputModel.Phone, inputModel.Message);

            if (!result)
            {
                //to do:
                return(this.Redirect("/"));
            }

            return(this.Redirect("/"));
        }
Beispiel #24
0
        public async Task <ContactResponseModel> SaveContactAsync(ContactInputModel inputModel)
        {
            var contact = new Contact()
            {
                Name  = inputModel.Name,
                Image = inputModel.Image
            };

            _contactRepo.Add(contact);

            await _unitOfWork.CommitAsync();

            return(new ContactResponseModel
            {
                Id = contact.Id,
                Name = contact.Name,
                Image = contact.Image
            });
        }
Beispiel #25
0
        public async Task TestSendMailPopulatedProduct()
        {
            // Arrange
            var service = new MessageService(_configuration, _mockWebHostEnv.Object);

            var inputModel = new ContactInputModel
            {
                SenderName        = "test name",
                SenderEmail       = "test email",
                SenderPhoneNumber = "(111) 222-3333",
                Subject           = "test subject",
                Message           = "test message"
            };

            // Act
            var result = await service.SendEmail(inputModel);

            // Assert
            Assert.True(result);
        }
Beispiel #26
0
        public async Task <IActionResult> PostContact([FromBody] ContactInputModel contact)
        {
            //In this api we are using this type of model that complicates the code, because swagger generates the
            // scheme in the documentation more precise with this one
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                ICollection <ContactSkillExpertiseModel> ContactSkillList = new HashSet <ContactSkillExpertiseModel>();

                foreach (var item in contact.ContactSkillExpertise)
                {
                    ContactSkillExpertiseModel ContactSkill = new ContactSkillExpertiseModel();
                    //ContactSkill.ContactId = item.ContactId;
                    ContactSkill.ExpertiseLvlid = item.ExpertiseLvlid;
                    ContactSkill.SkillId        = item.SkillId;
                    ContactSkillList.Add(ContactSkill);
                }
                _context.Contact.Add(new ContactModel()
                {
                    Firstname             = contact.Firstname,
                    Lastname              = contact.Lastname,
                    Fullname              = contact.Fullname,
                    Email                 = contact.Email,
                    Address               = contact.Address,
                    MobileNum             = contact.MobileNum,
                    ContactSkillExpertise = ContactSkillList
                });
                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                return(BadRequest(e.InnerException.Message));
            }



            return(Ok("Success"));
        }
Beispiel #27
0
        public async Task <IActionResult> Contact(ContactInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            await this.contactService.CreateAsync(input);

            // SendGrid not working with abv.bg
            var html = new StringBuilder();

            html.AppendLine($"<h1>Hello {input.FullName}!</h1>");
            html.AppendLine($"<h1>Thank you for contacting us at Coffee Blend.</h1>");
            html.AppendLine($"<h1>We will get back to you as soon as possible!</h1>");
            html.AppendLine($"<h1>Best regards, Emil Spasov</h1>");
            await this.emailSender
            .SendEmailAsync("*****@*****.**", "CoffeeBlend", input.Email, "Your message has been received:", html.ToString());

            return(this.RedirectToAction(nameof(this.SuccessfulContact)));
        }
Beispiel #28
0
        public async Task TestSendEmailInputModelOneFieldNotPopulated(
            string name,
            string email,
            string subject,
            string message)
        {
            // Arrange
            var service = new MessageService(_configuration, _mockWebHostEnv.Object);

            var inputModel = new ContactInputModel
            {
                SenderName  = name,
                SenderEmail = email,
                Subject     = subject,
                Message     = message
            };

            // Act
            var result = await service.SendEmail(inputModel);

            // Assert
            Assert.False(result);
        }
Beispiel #29
0
        public async Task <IActionResult> SendMessage(ContactInputModel inputModel)
        {
            if (inputModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(View("Index", inputModel));
            }

            var messageSuccess = await _messageService.SendEmail(inputModel);

            if (!messageSuccess)
            {
                TempData["MessageFail"] =
                    "There was a problem sending the message. Please try again, or contact the webmaster if the problem persists";

                return(View("Index", inputModel));
            }

            return(RedirectToAction("Index", "Home", inputModel));
        }
 public void SendEmail(ContactInputModel model)
 {
     this.Execute(model).Wait();
 }
Beispiel #31
0
 public IActionResult CreateNewContact([FromBody] ContactInputModel contact)
 {
     return(Ok(_contactAppService.CreateContact(contact)));
 }