diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index 88d85028..7f6a2cdd 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -81,51 +81,6 @@ public void GetTagById_TagDoesNotExist_NullReturned() Assert.ThrowsException(() => tagService.GetTagById(1)); } - [TestMethod] - public void UpdateTag_TagExists_TagUpdated() - { - // Arrange - var tagDto = new TagDto - { - Name = "TestTag1" - }; - var tag = new Tag { Id = 1, Name = "TestTag" }; - var eventId = 1; - - var mockContext = new Mock(); - mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { tag }); - mockContext.Setup(c => c.Events).ReturnsDbSet(new List { new Event { Id = eventId, Tags = new List { tag } } }); - - var tagService = new TagService(mockContext.Object); - - // Act - var result = tagService.UpdateTagForEvent(1, tagDto, eventId); - - // Assert - Assert.IsNotNull(result); - Assert.AreEqual(tagDto.Name, result.Name); - Assert.AreEqual(tagDto.Name, tag.Name); // Verify the tag was updated - mockContext.Verify(c => c.SaveChanges(), Times.Once); // Ensure SaveChanges was called - } - - [TestMethod] - public void UpdateTag_TagDoesNotExist_NullReturned() - { - var tagDto = new TagDto - { - Name = "TestTag1" - }; - var eventId = 1; - - var mockContext = new Mock(); - mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - mockContext.Setup(c => c.Events).ReturnsDbSet(new List { new Event { Id = eventId } }); - var tagService = new TagService(mockContext.Object); - - Assert.ThrowsException(() => tagService.UpdateTagForEvent(1, tagDto, eventId)); - mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called - } - [TestMethod] public void DeleteTag_TagExists_TagDeleted() { diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index d503d17c..3bbe5c98 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -22,51 +22,6 @@ public TagDto CreateTag(TagDto tagDto) return tagDto; } - public TagDto UpdateTagForEvent(int tagId, TagDto tagDto, int eventId) - { - using (var scope = new TransactionScope()) - { - var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); - - if (tag is null) - { - throw new NotFoundException("Tag not found"); - } - - var eventsWithTags = context.Events.Include(e => e.Tags).ToList(); - - var eventTags = eventsWithTags.Where(e => e.Tags.Any(t => t.Id == tagId)).ToList(); - var eventToUpdate = eventsWithTags.FirstOrDefault(e => e.Id == eventId); - - if (eventTags.Count > 1 || (eventTags.Count == 1 && eventTags[0].Id != eventId)) - { - // Create new tag, associate it with the event, and remove the old association - var newTag = tagDto.ToEntity(); - context.Tags.Add(newTag); - context.SaveChanges(); - - if (eventToUpdate != null) - { - eventToUpdate.Tags.Remove(tag); - eventToUpdate.Tags.Add(newTag); - context.Events.Update(eventToUpdate); - } - } - else if (eventTags.Count == 1 && eventTags[0].Id == eventId) - { - tag.Name = tagDto.Name; - context.Tags.Update(tag); - } - else - { - throw new NotFoundException("Tag not found"); - } - - context.SaveChanges(); - scope.Complete(); - return tagDto; - } - } public TagDto UpdateTag(int tagId, TagDto tagDto) { var tag = context.Tags.FirstOrDefault(r => r.Id == tagId);