public async Task Create_ValidData_CreatesCorrectDatabaseEntries() { var emails = new[] { "*****@*****.**", "*****@*****.**" }; var dto = new NewEmailGroupDto() { Name = "TestCase2", Emails = emails }; var created = await this.service.Create(dto); this.DbContext.EmailGroups.Any(item => item.Name == dto.Name).Should().BeTrue(); this.DbContext.Emails.Any(item => item.Address == emails[0] && item.EmailGroupId == created.Id).Should().BeTrue(); this.DbContext.Emails.Any(item => item.Address == emails[1] && item.EmailGroupId == created.Id).Should().BeTrue(); }
public async Task <EmailGroupDto> Update(int groupId, NewEmailGroupDto dto) { var emailGroup = await this.GetEmailGroupSafely(groupId); this.dbContext.Emails.RemoveRange(emailGroup.Emails); emailGroup.Name = dto.Name; emailGroup.Emails = dto.Emails.Select(item => new Email() { Address = item }).ToList(); await this.dbContext.SaveChangesAsync(); return(new EmailGroupDto() { Id = emailGroup.Id, Name = emailGroup.Name, Emails = emailGroup.Emails.Select(item => item.Address) }); }
public async Task <EmailGroupDto> Create(NewEmailGroupDto dto) { await this.validatorService.ThrowIfGroupNameAlreadyExist(dto.Name); var newGroup = new EmailGroup() { Name = dto.Name, Emails = dto.Emails.Select(item => new Email() { Address = item }).ToList() }; this.dbContext.EmailGroups.Add(newGroup); await this.dbContext.SaveChangesAsync(); return(new EmailGroupDto() { Id = newGroup.Id, Name = newGroup.Name, Emails = newGroup.Emails.Select(item => item.Address) }); }
public async Task <IActionResult> Update(int groupId, [FromBody] NewEmailGroupDto dto) { var updatedGroup = await this.emailGroupService.Update(groupId, dto); return(this.Ok(updatedGroup)); }
public async Task <IActionResult> Create([FromBody] NewEmailGroupDto dto) { var createdGroup = await this.emailGroupService.Create(dto); return(this.Ok(createdGroup)); }