Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bilimig committed Jun 12, 2024
1 parent c343f40 commit 55b9e7d
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 90 deletions.
45 changes: 0 additions & 45 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,51 +81,6 @@ public void GetTagById_TagDoesNotExist_NullReturned()
Assert.ThrowsException<NotFoundException>(() => 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<ReasnContext>();
mockContext.Setup(c => c.Tags).ReturnsDbSet(new List<Tag> { tag });
mockContext.Setup(c => c.Events).ReturnsDbSet(new List<Event> { new Event { Id = eventId, Tags = new List<Tag> { 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<ReasnContext>();
mockContext.Setup(c => c.Tags).ReturnsDbSet(new List<Tag>());
mockContext.Setup(c => c.Events).ReturnsDbSet(new List<Event> { new Event { Id = eventId } });
var tagService = new TagService(mockContext.Object);

Assert.ThrowsException<NotFoundException>(() => tagService.UpdateTagForEvent(1, tagDto, eventId));
mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called
}

[TestMethod]
public void DeleteTag_TagExists_TagDeleted()
{
Expand Down
45 changes: 0 additions & 45 deletions Server/ReasnAPI/ReasnAPI/Services/TagService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 55b9e7d

Please sign in to comment.