Exemplo n.º 1
0
        public async Task <ActionResult> Post([FromForm] ContactFormMessageCreateInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                // TODO - return model state errrors!!!!
                return(this.BadRequest());
            }

            ContactFormMessageExportModel model;

            try
            {
                // var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
                var user = await this.userManager.GetUserAsync(this.User);

                // TODO: Extract to IP provider (service)
                var ip = this.HttpContext.Connection.RemoteIpAddress.ToString();
                input.Ip = ip;

                var inputId = await this.contactFormMessagesService.CreateAsync(input, user?.Id);

                model = await this.contactFormMessagesService.GetByIdAsync <ContactFormMessageExportModel>(inputId);

                // send email to site admin
                await this.emailSender.SendEmailAsync(
                    GlobalConstants.SystemEmail,
                    GlobalConstants.SystemName,
                    GlobalConstants.SystemEmail,
                    GlobalConstants.SystemName,
                    $"You have message from {input.Name}",
                    $"Message content: {input.Subject} - {input.Message}");

                // send email to contact message sender
                await this.emailSender.SendEmailAsync(
                    GlobalConstants.SystemEmail,
                    GlobalConstants.SystemName,
                    input.Email,
                    input.Name,
                    "Thank you for your message",
                    $"Dear {input.Name},\r\n Thank you for your interest on our site and the message sent!\r\n We will contact you as soon as we review your request.\r\n\r\nKind Regards,\r\nMySkills Team");

                this.logger.LogInformation($"Send contact message to user and admin, record message in the DB.");
            }
            catch (Exception ex)
            {
                this.logger.LogError($"RequestID: {Activity.Current?.Id ?? this.HttpContext.TraceIdentifier}; Contact message creation failed: {ex}.");

                // TODO - return model state errrors!!!!
                return(this.BadRequest());
            }

            return(this.CreatedAtAction(nameof(this.GetById), new { id = model.Id }, model));
        }
Exemplo n.º 2
0
        public async Task <int> CreateAsync(ContactFormMessageCreateInputModel input, string userId)
        {
            var entity = input.To <ContactFormMessage>();

            entity.UserId = userId;

            await this.contactFormMessagesRepository.AddAsync(entity);

            await this.contactFormMessagesRepository.SaveChangesAsync();

            return(entity.Id);
        }