예제 #1
0
        public async Task <OperationResult> CreateEmailRecipient(EmailRecipientsCreateModel createModel)
        {
            //using (var transaction = await _dbContext.Database.BeginTransactionAsync())
//                {
            try
            {
                var tagIds = new List <int>();
                // Create tags

                if (!string.IsNullOrEmpty(createModel.NewTags))
                {
                    var tagNames = createModel.NewTags
                                   .Replace(" ", "")
                                   .Split(',');

                    foreach (var tagName in tagNames)
                    {
                        var emailTag = new EmailTag
                        {
                            Name            = tagName,
                            CreatedAt       = DateTime.UtcNow,
                            CreatedByUserId = _userService.UserId,
                            UpdatedAt       = DateTime.UtcNow,
                            UpdatedByUserId = _userService.UserId,
                            Version         = 1,
                        };
                        await _dbContext.EmailTags.AddAsync(emailTag);

                        await _dbContext.SaveChangesAsync();

                        tagIds.Add(emailTag.Id);
                    }
                }


                tagIds.AddRange(createModel.TagsIds);


                foreach (var recipientCreateModel in createModel.EmailRecipientsList)
                {
                    var emailRecipient = new EmailRecipient
                    {
                        Name            = recipientCreateModel.Name,
                        Email           = recipientCreateModel.Email.Replace(" ", ""),
                        CreatedAt       = DateTime.UtcNow,
                        CreatedByUserId = _userService.UserId,
                        UpdatedAt       = DateTime.UtcNow,
                        UpdatedByUserId = _userService.UserId,
                        Version         = 1,
                        TagRecipients   = new List <EmailTagRecipient>(),
                    };

                    // add new tags
                    foreach (var tagId in tagIds)
                    {
                        emailRecipient.TagRecipients.Add(
                            new EmailTagRecipient
                        {
                            CreatedAt       = DateTime.UtcNow,
                            CreatedByUserId = _userService.UserId,
                            UpdatedAt       = DateTime.UtcNow,
                            UpdatedByUserId = _userService.UserId,
                            Version         = 1,
                            EmailTagId      = tagId,
                        });
                    }

                    await _dbContext.EmailRecipients.AddAsync(emailRecipient);
                }

                await _dbContext.SaveChangesAsync();

                //transaction.Commit();
                return(new OperationResult(true,
                                           _localizationService.GetString("EmailRecipientCreatedSuccessfully")));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                _logger.LogError(e.Message);
                //transaction.Commit();
                return(new OperationResult(false,
                                           _localizationService.GetString("ErrorWhileCreatingEmailRecipient")));
            }
            //}
        }
예제 #2
0
 public async Task <OperationResult> CreateEmailRecipients([FromBody] EmailRecipientsCreateModel model)
 {
     return(await _emailRecipientsService.CreateEmailRecipient(model));
 }