public void ItemTagsDto_constructor_Test() { // arrange List <string> originalTags = new List <string> { "Software", "History", "Science" }; List <string> selectedTags = new List <string> { "History", "Physics" }; // act ItemTagsDto dto = new ItemTagsDto(1, originalTags, selectedTags); // assert Assert.AreEqual(1, dto.TagsToAdd.Count()); Assert.IsTrue(dto.TagsToAdd.Any(t => t.Equals("Physics"))); Assert.AreEqual(2, dto.TagsToRemove.Count()); Assert.IsTrue(dto.TagsToRemove.Any(t => t.Equals("Software"))); Assert.IsTrue(dto.TagsToRemove.Any(t => t.Equals("Science"))); }
public async Task UpdateTags(ItemTagsDto dto) { await Task.Run(() => { // begin transaction IUnitOfWork uow = this._uowProvider.Get(); IMediaItemRepository itemRepo = this._repoProvider.Get(uow); ITagRepository tagRepo = this._tagRepoProvider.Get(uow); uow.Begin(); // add tags foreach (var tag in dto.TagsToAdd) { if (tagRepo.ExistsWithName(tag)) { // tag exists // get the Id int tagId = tagRepo.GetIdByName(tag); // insert record into link table tagRepo.LinkMediaItem(dto.Id, tagId); } else { // tag does not exist // insert it tagRepo.Create(new Tag { Name = tag }); // get the id int tagId = tagRepo.GetIdByName(tag); // insert record into link table tagRepo.LinkMediaItem(dto.Id, tagId); } } // remove tags foreach (var tag in dto.TagsToRemove) { if (tagRepo.ExistsWithName(tag)) { // tag exists // get the id int tagId = tagRepo.GetIdByName(tag); // delete record from link table tagRepo.UnlinkMediaItem(dto.Id, tagId); } } // commit transaction uow.Commit(); }); }
public async Task UpdateTags_Test() { // arrange var fakeUowProvider = A.Fake <IUnitOfWorkProvider>(); var fakeRepoProvider = A.Fake <IBookRepositoryProvider>(); var fakeTagRepoProvider = A.Fake <ITagRepositoryServiceProvider>(); var fakePublisherRepoProvider = A.Fake <IPublisherRepositoryProvider>(); var fakeAuthorRepoProvider = A.Fake <IAuthorRepositoryProvider>(); var fakeUow = A.Fake <IUnitOfWork>(); var fakeRepo = A.Fake <IBookRepository>(); var fakeTagRepo = A.Fake <ITagRepository>(); A.CallTo(() => fakeUowProvider.Get()).Returns(fakeUow); A.CallTo(() => fakeRepoProvider.Get(fakeUow)).Returns(fakeRepo); A.CallTo(() => fakeTagRepoProvider.Get(fakeUow)).Returns(fakeTagRepo); A.CallTo(() => fakeTagRepo.ExistsWithName("tag1")).Returns(true); A.CallTo(() => fakeTagRepo.GetIdByName("tag1")).Returns(1); A.CallTo(() => fakeTagRepo.ExistsWithName("tag2")).Returns(true); A.CallTo(() => fakeTagRepo.GetIdByName("tag2")).Returns(2); A.CallTo(() => fakeTagRepo.ExistsWithName("tag3")).Returns(true); A.CallTo(() => fakeTagRepo.GetIdByName("tag3")).Returns(3); A.CallTo(() => fakeTagRepo.ExistsWithName("tag4")).Returns(false); A.CallTo(() => fakeTagRepo.GetIdByName("tag4")).Returns(4); BookService service = new BookService(fakeUowProvider, fakeRepoProvider, fakePublisherRepoProvider, fakeAuthorRepoProvider, fakeTagRepoProvider); List <string> originalTags = new List <string> { "tag1", "tag2", "tag3" }; List <string> selectedTags = new List <string> { "tag2", "tag4" }; ItemTagsDto itemTags = new ItemTagsDto(1, originalTags, selectedTags); Tag newTag = new Tag { Name = "tag4" }; // act await service.UpdateTags(itemTags); // assert A.CallTo(() => fakeUow.Begin()).MustHaveHappened(); A.CallTo(() => fakeTagRepo.LinkMediaItem(1, 4)); A.CallTo(() => fakeTagRepo.UnlinkMediaItem(1, 1)); A.CallTo(() => fakeUow.Commit()).MustHaveHappened(); }
public ManageTagsForItemDialog(Item item) { InitializeComponent(); this._tagService = new TagService(); this._item = item; this.itemTitleLabel.Text = item.Title; this.tagsList.CheckOnClick = true; this.CenterToParent(); // initially disable add and save buttons this.buttonSave.Enabled = false; this.addNewTagButton.Enabled = false; // register event handlers this.buttonCancel.Click += ((sender, args) => { this.Close(); }); this.buttonSave.Click += (async(sender, args) => { // disable add, save and cancel buttons this.addNewTagButton.Enabled = false; this.buttonSave.Enabled = false; this.buttonCancel.Enabled = false; // save changes List <string> originalTags = new List <string>(); foreach (var tag in this._item.Tags) { originalTags.Add(tag.Name); } ItemTagsDto dto = new ItemTagsDto(this._item.Id, originalTags, this.SelectedTags); try { if (this._item.Type == ItemType.Book) { IBookService _bookService = new BookService(); await _bookService.UpdateTags(dto); } else { IMediaItemService _itemRepo = new MediaItemService(); await _itemRepo.UpdateTags(dto); } this.Close(); } catch (Exception ex) { // something bad happened // notify the user MessageBox.Show("Error updating tags: " + ex.Message, "manage tags", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // re-enable buttons this.addNewTagButton.Enabled = true; this.buttonSave.Enabled = true; this.buttonCancel.Enabled = true; } TagsUpdated?.Invoke(this, args); }); this.newTagField.TextChanged += ((sender, args) => { this.addNewTagButton.Enabled = !string.IsNullOrWhiteSpace(this.newTagField.Text); }); this.addNewTagButton.Click += (async(sender, args) => { // disable add, save and cancel buttons this.addNewTagButton.Enabled = false; this.buttonSave.Enabled = false; this.buttonCancel.Enabled = false; string newTagName = this.newTagField.Text; try { // check for existing tag bool tagAlreadyInList = false; foreach (var tagInList in this.tagsList.Items) { if (tagInList.ToString().Equals(newTagName)) { tagAlreadyInList = true; } } if (await this._tagService.ExistsWithName(newTagName) || tagAlreadyInList) { MessageBox.Show("Tag: \"" + newTagName + "\" already exists.", "Manage tags", MessageBoxButtons.OK, MessageBoxIcon.Warning); // re-enable buttons this.addNewTagButton.Enabled = true; this.buttonSave.Enabled = true; this.buttonCancel.Enabled = true; return; } } catch (Exception ex) { // something bad happened MessageBox.Show("Error checking if tag \"" + newTagName + "\" exists: " + ex.Message, "Manage tags", MessageBoxButtons.OK, MessageBoxIcon.Error); // re-enable buttons this.addNewTagButton.Enabled = true; this.buttonSave.Enabled = true; this.buttonCancel.Enabled = true; return; } this.tagsList.Items.Add(this.newTagField.Text, true); // re-enable buttons this.addNewTagButton.Enabled = true; this.buttonSave.Enabled = true; this.buttonCancel.Enabled = true; }); this.tagsList.ItemCheck += ((sender, args) => { this.buttonSave.Enabled = true; }); }