public async Task EditeAsyncWithDublicateNameShouldThrowArgumentException() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var cloudinary = new Mock <ICloudinaryService>(); var concertGroupsRepository = new EfRepository <ConcertGroup>(dbContext); var groupsRepository = new EfDeletableEntityRepository <Group>(dbContext); var groupGenresRepository = new EfRepository <GroupGenre>(dbContext); var usertGroupsRepository = new EfRepository <UserGroup>(dbContext); var groupService = new GroupsService(concertGroupsRepository, groupsRepository, groupGenresRepository, usertGroupsRepository, cloudinary.Object); var photo = new Mock <IFormFile>(); await groupService.CreateAsync("Sabaton", photo.Object, "description"); var id = await groupService.CreateAsync("Nightwish", photo.Object, "description"); var model = new GroupEditInputModel { Name = "Sabaton", Photo = photo.Object, Description = "description", }; await Assert.ThrowsAsync <ArgumentException>(async() => { await groupService.EditAsync(id, model); }); }
public async Task EditAsyncWithCorrectDataShouldReturnCorrectResult() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var cloudinary = new Mock <ICloudinaryService>(); var concertGroupsRepository = new EfRepository <ConcertGroup>(dbContext); var groupsRepository = new EfDeletableEntityRepository <Group>(dbContext); var groupGenresRepository = new EfRepository <GroupGenre>(dbContext); var usertGroupsRepository = new EfRepository <UserGroup>(dbContext); var groupService = new GroupsService(concertGroupsRepository, groupsRepository, groupGenresRepository, usertGroupsRepository, cloudinary.Object); var photo = new Mock <IFormFile>(); var id = await groupService.CreateAsync("Sabaton", photo.Object, "description"); var model = new GroupEditInputModel { Name = "Sabaton", Photo = photo.Object, Description = "other", }; Assert.True(await groupService.EditAsync(id, model)); }
public async Task <bool> EditAsync(int id, GroupEditInputModel model) { var group = await this.groupsRepository .All() .FirstOrDefaultAsync(g => g.Id == id); if (group.Name.ToUpper() != model.Name.ToUpper()) { var groupWithSameName = await this.groupsRepository .All() .FirstOrDefaultAsync(c => c.Name.ToUpper() == model.Name.ToUpper()); if (groupWithSameName != null) { throw new ArgumentException(ErrorMessageNameExist); } } var url = model.ImgUrl; if (model.Photo != null) { url = await this.cloudinaryService.UploadPhotoAsync( model.Photo, model.Name, GlobalConstants.CloudFolderForGroupsPhotos); } group.Name = model.Name; group.ImgUrl = model.ImgUrl; group.Description = model.Description; this.groupsRepository.Update(group); var result = await this.groupsRepository.SaveChangesAsync(); return(result > 0); }