public IActionResult DeleteObj([FromBody] MailingGroup obj) { Guid id = obj.Id; if (id == null) { return(BadRequest()); } MailingGroup mailingGroup = db.MailingGroups.Find(id); mailingGroup.Deleted = true; db.Entry(mailingGroup).State = EntityState.Modified; db.SaveChanges(); DeleteChildren(id); Log log = new Log(); log.Date = DateTime.Now; log.Person = @User.Identity.Name; log.ObjectGuid = mailingGroup.Id; log.Place = "MailingGroup"; log.Fact = "Удаление группы рассылки"; db.Logs.Add(log); db.SaveChanges(); return(Ok()); }
public async Task ExecuteShouldReturnFailedResultNameIsRequired() { //Arrange MailingGroupContext mailingGroupContext = ContextBuilder.BuildClean(); AppUser user = new AppUser { UserName = "******", Email = "*****@*****.**" }; mailingGroupContext.Add(user); mailingGroupContext.SaveChanges(); Command cmd = new Command { UserId = user.Id, Name = string.Empty }; //Act ApiResult <MailingGroupDto> result = await new Handler(mailingGroupContext).Handle(cmd, CancellationToken.None); //Assert result.IsSuccess.Should().BeFalse(); result.Message.Should().Be("Name is required"); result.Data.Should().BeNull(); }
public async Task ExecuteShouldReturnSuccess() { //Arrange MailingGroupContext mailingGroupContext = ContextBuilder.BuildClean(); AppUser user = new AppUser { UserName = "******", Email = "*****@*****.**" }; AppUser otherUser = new AppUser { UserName = "******", Email = "*****@*****.**" }; mailingGroupContext.Add(user); mailingGroupContext.Add(otherUser); Model.Entity.MailingGroup oldGroup = new Model.Entity.MailingGroup { Name = "Old group", User = user }; Model.Entity.MailingGroup groupOtherUser = new Model.Entity.MailingGroup { Name = "New group", User = otherUser }; mailingGroupContext.Add(oldGroup); mailingGroupContext.Add(groupOtherUser); mailingGroupContext.SaveChanges(); Command cmd = new Command { UserId = user.Id, Id = oldGroup.Id, Name = "New group" }; //Act ApiResult <MailingGroupDto> result = await new Handler(mailingGroupContext).Handle(cmd, CancellationToken.None); //Assert result.IsSuccess.Should().BeTrue(); result.Message.Should().BeNullOrEmpty(); mailingGroupContext.MailingGroups.Should().HaveCount(2); Model.Entity.MailingGroup mailingGroup = mailingGroupContext.MailingGroups.First(); mailingGroup.Name.Should().Be(cmd.Name); mailingGroup.UserId.Should().Be(cmd.UserId); mailingGroup.Id.Should().Be(1); result.Data.Should().NotBeNull(); result.Data.Id.Should().Be(mailingGroup.Id); }
public async Task ExecuteShouldReturnNameIsTaken() { //Arrange MailingGroupContext mailingGroupContext = ContextBuilder.BuildClean(); AppUser user = new AppUser { UserName = "******", Email = "*****@*****.**" }; mailingGroupContext.Add(user); Model.Entity.MailingGroup oldGroup = new Model.Entity.MailingGroup { Name = "Old group", User = user }; Model.Entity.MailingGroup otherGroup = new Model.Entity.MailingGroup { Name = "New group", User = user }; mailingGroupContext.Add(oldGroup); mailingGroupContext.SaveChanges(); mailingGroupContext.Add(otherGroup); mailingGroupContext.SaveChanges(); Command cmd = new Command { UserId = user.Id, Id = oldGroup.Id, Name = "New group" }; //Act ApiResult <MailingGroupDto> result = await new Handler(mailingGroupContext).Handle(cmd, CancellationToken.None); //Assert result.IsSuccess.Should().BeFalse(); result.Message.Should().Be("This mailing group name is already exists"); result.StatusCode.Should().Be(401); result.Data.Should().BeNull(); }
public IActionResult AddObj([FromBody] MailingGroup mailingGroup) { if (mailingGroup == null) { return(BadRequest()); } mailingGroup.Name = mailingGroup.Name.Trim(); mailingGroup.Name = string.Join(" ", mailingGroup.Name.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); mailingGroup.Name = mailingGroup.Name.Replace("ё", "е"); mailingGroup.Description = mailingGroup.Description.Trim(); mailingGroup.Description = string.Join(" ", mailingGroup.Description.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); mailingGroup.Description = mailingGroup.Description.Replace("ё", "е"); db.MailingGroups.Add(mailingGroup); db.SaveChanges(); Log log = new Log(); log.Date = DateTime.Now; log.Person = @User.Identity.Name; log.ObjectGuid = mailingGroup.Id; log.Place = "MailingGroup"; log.Fact = "Создание группы рассылки"; db.Logs.Add(log); db.SaveChanges(); return(Ok(mailingGroup)); }
public async Task ExecuteShouldReturnFailedWhenGroupIdIsWrong() { //Arrange MailingGroupContext context = ContextBuilder.BuildClean(); AppUser firstUser = new AppUser() { UserName = "******", Email = "*****@*****.**" }; AppUser secondUser = new AppUser() { UserName = "******", Email = "*****@*****.**" }; context.Add(firstUser); context.Add(secondUser); List <MailingGroup> mailingGroups = new List <MailingGroup> { new MailingGroup() { Name = "first", User = firstUser }, new MailingGroup() { Name = "second", User = secondUser } }; context.AddRange(mailingGroups); context.SaveChanges(); Query query = new Query() { Id = 22, UserId = firstUser.Id }; //Act ApiResult <MailingGroupDto> result = await new Handler(context).Handle(query, CancellationToken.None); //Assert result.IsSuccess.Should().BeFalse(); result.Message.Should().Be("Mail group not exists"); result.Data.Should().BeNull(); result.StatusCode.Should().Be(404); }
public async Task ExecuteShouldReturMailingGroupDetailWithEmails() { //Arrange MailingGroupContext context = ContextBuilder.BuildClean(); AppUser firstUser = new AppUser() { UserName = "******", Email = "*****@*****.**" }; AppUser secondUser = new AppUser() { UserName = "******", Email = "*****@*****.**" }; context.Add(firstUser); context.Add(secondUser); MailingGroup group = new MailingGroup() { Name = "first", User = firstUser, }; group.Emails = GenerateEmails(20, group).ToList(); context.Add(group); context.SaveChanges(); Query query = new Query() { Id = 1, UserId = firstUser.Id }; //Act ApiResult <MailingGroupDto> result = await new Handler(context).Handle(query, CancellationToken.None); //Assert result.IsSuccess.Should().BeTrue(); result.Message.Should().BeNullOrEmpty(); MailingGroupDto mailingGroupDto = result.Data; mailingGroupDto.Should().NotBeNull(); mailingGroupDto.Emails.Should().NotBeEmpty(); result.StatusCode.Should().Be(200); }
public IActionResult EditObj([FromBody] MailingGroup mailingGroup) { if (mailingGroup == null) { return(BadRequest()); } mailingGroup.Name = mailingGroup.Name.Trim(); mailingGroup.Name = string.Join(" ", mailingGroup.Name.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); mailingGroup.Name = mailingGroup.Name.Replace("ё", "е"); mailingGroup.Description = mailingGroup.Description.Trim(); mailingGroup.Description = string.Join(" ", mailingGroup.Description.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); mailingGroup.Description = mailingGroup.Description.Replace("ё", "е"); MailingGroup oldMailingGroup = db.MailingGroups.Find(mailingGroup.Id); oldMailingGroup.Name = mailingGroup.Name; oldMailingGroup.Description = mailingGroup.Description; oldMailingGroup.ADgroup = mailingGroup.ADgroup; oldMailingGroup.IsSync = mailingGroup.IsSync; oldMailingGroup.Type = mailingGroup.Type; oldMailingGroup.ParentId = mailingGroup.ParentId; db.Entry(oldMailingGroup).State = EntityState.Modified; db.SaveChanges(); Log log = new Log(); log.Date = DateTime.Now; log.Person = @User.Identity.Name; log.ObjectGuid = mailingGroup.Id; log.Place = "MailingGroup"; log.Fact = "Редактирование группы рассылки"; db.Logs.Add(log); db.SaveChanges(); return(Ok(mailingGroup)); }
public TBuilder Save() { Context.Add(State); Context.SaveChanges(); return((TBuilder)this); }