Skip to content

Commit

Permalink
Add methods to add artists and events, including unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vriesmarcel committed Apr 2, 2024
1 parent 7333293 commit 3fca6a5
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 0 deletions.
28 changes: 28 additions & 0 deletions catalog/Controllers/EventController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,32 @@ public async Task<IActionResult> GetById(Guid id)
var evt = await _eventRepository.GetEventById(id);
return Ok(evt);
}

[HttpPost(Name = "AddEvent")]
public async Task<IActionResult> AddEvent(Event @event)
{
var newEvent = await _eventRepository.AddEvent(@event);
return CreatedAtRoute("GetById", new { id = newEvent.EventId }, newEvent);
}

[HttpGet("artists", Name = "GetArtists")]
public async Task<IActionResult> GetArtists()
{
return Ok(await _eventRepository.GetArtists());
}

[HttpGet("artists/{id}", Name = "GetArtistById")]
public async Task<IActionResult> GetArtistById(Guid id)
{
var artist = await _eventRepository.GetArtistById(id);
return Ok(artist);
}

[HttpPost("artists", Name = "AddArtist")]
public async Task<IActionResult> AddArtist(Artist artist)
{
var newArtist = await _eventRepository.AddArtist(artist.Name, artist.Genre);
return CreatedAtRoute("GetArtistById", new { id = newArtist.Id }, newArtist);
}

}
2 changes: 2 additions & 0 deletions catalog/Infrastructure/Database.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("GloboTicket.UnitTests")]
namespace GloboTicket.Catalog.Infrastructure;

internal static class Database
Expand Down
4 changes: 4 additions & 0 deletions catalog/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;


16 changes: 16 additions & 0 deletions catalog/Repositories/EventRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@ public Task<IEnumerable<Artist>> GetArtists()
{
return Task.FromResult((IEnumerable<Artist>)Database.Artists);
}

public Task<Artist> GetArtistById(Guid artistId)
{
var artist = Database.Artists.FirstOrDefault(a => a.Id == artistId);
if (artist == null)
{
throw new InvalidOperationException("Artist not found");
}
return Task.FromResult(artist);
}

public Task<Event> AddEvent(Event @event)
{
Database.Events.Add(@event);
return Task.FromResult(@event);
}
}
4 changes: 4 additions & 0 deletions catalog/Repositories/IEventRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ public interface IEventRepository
Task<Event> GetEventById(Guid eventId);
void UpdateSpecialOffer();
Task<IEnumerable<Artist>> GetArtists();
Task<Artist> GetArtistById(Guid artistId);

Task<Artist> AddArtist(string name, string genre);

Task<Event> AddEvent(Event @event);
}
56 changes: 56 additions & 0 deletions unittests/EventRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GloboTicket.Catalog.Repositories;
using GloboTicket.Catalog.Infrastructure;
using GloboTicket.Catalog;
using Microsoft.Extensions.Logging;
namespace unittests
{
[TestClass]
public class EventRepositoryTests
{
private Mock<ILogger<EventRepository>> mockLogger;
private EventRepository eventRepository;

[TestInitialize]
public void TestInitialize()
{
mockLogger = new Mock<ILogger<EventRepository>>();
eventRepository = new EventRepository(mockLogger.Object);
}

[TestMethod]
public async Task AddArtist_ShouldReturnAddedArtist()
{
var artist = await eventRepository.AddArtist("Artist3", "Genre3");
Assert.AreEqual("Artist3", artist.Name);
Assert.AreEqual("Genre3", artist.Genre);
}



[TestMethod]
public async Task GetArtistById_ShouldReturnCorrectArtist()
{
var addedArtist = await eventRepository.AddArtist("Artist4", "Genre4");

var artist = await eventRepository.GetArtistById(addedArtist.Id);
Assert.AreEqual(addedArtist.Id, artist.Id);
}

[TestMethod]
public async Task AddEvent_ShouldReturnAddedEvent()
{
var @event = new Event { EventId = Guid.NewGuid(), Name = "Event3" };
var result = await eventRepository.AddEvent(@event);
Assert.AreEqual(@event.EventId, result.EventId);

var eventbyId = await eventRepository.GetEventById(@event.EventId);
Assert.AreEqual(@event.EventId, eventbyId.EventId);
}
}
}
1 change: 1 addition & 0 deletions unittests/unittests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
Expand Down

0 comments on commit 3fca6a5

Please sign in to comment.