From 3fca6a5283ab471842b5e2bdf2db271008633b6d Mon Sep 17 00:00:00 2001 From: Marcel de Vries Date: Tue, 2 Apr 2024 21:57:13 +0200 Subject: [PATCH] Add methods to add artists and events, including unit tests --- catalog/Controllers/EventController.cs | 28 ++++++++++++ catalog/Infrastructure/Database.cs | 2 + catalog/Properties/AssemblyInfo.cs | 4 ++ catalog/Repositories/EventRepository.cs | 16 +++++++ catalog/Repositories/IEventRepository.cs | 4 ++ unittests/EventRepositoryTests.cs | 56 ++++++++++++++++++++++++ unittests/unittests.csproj | 1 + 7 files changed, 111 insertions(+) create mode 100644 catalog/Properties/AssemblyInfo.cs create mode 100644 unittests/EventRepositoryTests.cs diff --git a/catalog/Controllers/EventController.cs b/catalog/Controllers/EventController.cs index c7bd71b..f665e78 100644 --- a/catalog/Controllers/EventController.cs +++ b/catalog/Controllers/EventController.cs @@ -38,4 +38,32 @@ public async Task GetById(Guid id) var evt = await _eventRepository.GetEventById(id); return Ok(evt); } + + [HttpPost(Name = "AddEvent")] + public async Task AddEvent(Event @event) + { + var newEvent = await _eventRepository.AddEvent(@event); + return CreatedAtRoute("GetById", new { id = newEvent.EventId }, newEvent); + } + + [HttpGet("artists", Name = "GetArtists")] + public async Task GetArtists() + { + return Ok(await _eventRepository.GetArtists()); + } + + [HttpGet("artists/{id}", Name = "GetArtistById")] + public async Task GetArtistById(Guid id) + { + var artist = await _eventRepository.GetArtistById(id); + return Ok(artist); + } + + [HttpPost("artists", Name = "AddArtist")] + public async Task AddArtist(Artist artist) + { + var newArtist = await _eventRepository.AddArtist(artist.Name, artist.Genre); + return CreatedAtRoute("GetArtistById", new { id = newArtist.Id }, newArtist); + } + } diff --git a/catalog/Infrastructure/Database.cs b/catalog/Infrastructure/Database.cs index 33fa458..abdff8f 100644 --- a/catalog/Infrastructure/Database.cs +++ b/catalog/Infrastructure/Database.cs @@ -1,3 +1,5 @@ +using System.Runtime.CompilerServices; +[assembly: InternalsVisibleTo("GloboTicket.UnitTests")] namespace GloboTicket.Catalog.Infrastructure; internal static class Database diff --git a/catalog/Properties/AssemblyInfo.cs b/catalog/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..571e126 --- /dev/null +++ b/catalog/Properties/AssemblyInfo.cs @@ -0,0 +1,4 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + + diff --git a/catalog/Repositories/EventRepository.cs b/catalog/Repositories/EventRepository.cs index 6e7a48e..72f0087 100644 --- a/catalog/Repositories/EventRepository.cs +++ b/catalog/Repositories/EventRepository.cs @@ -49,4 +49,20 @@ public Task> GetArtists() { return Task.FromResult((IEnumerable)Database.Artists); } + + public Task 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 AddEvent(Event @event) + { + Database.Events.Add(@event); + return Task.FromResult(@event); + } } diff --git a/catalog/Repositories/IEventRepository.cs b/catalog/Repositories/IEventRepository.cs index d902622..d5c76f0 100644 --- a/catalog/Repositories/IEventRepository.cs +++ b/catalog/Repositories/IEventRepository.cs @@ -6,5 +6,9 @@ public interface IEventRepository Task GetEventById(Guid eventId); void UpdateSpecialOffer(); Task> GetArtists(); + Task GetArtistById(Guid artistId); + Task AddArtist(string name, string genre); + + Task AddEvent(Event @event); } diff --git a/unittests/EventRepositoryTests.cs b/unittests/EventRepositoryTests.cs new file mode 100644 index 0000000..d141486 --- /dev/null +++ b/unittests/EventRepositoryTests.cs @@ -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> mockLogger; + private EventRepository eventRepository; + + [TestInitialize] + public void TestInitialize() + { + mockLogger = new Mock>(); + 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); + } + } +} \ No newline at end of file diff --git a/unittests/unittests.csproj b/unittests/unittests.csproj index 93216d0..e35d098 100644 --- a/unittests/unittests.csproj +++ b/unittests/unittests.csproj @@ -14,6 +14,7 @@ all +