diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TestData.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TestData.cs new file mode 100644 index 0000000..e77bde4 --- /dev/null +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TestData.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using LeedsBeerQuest.API.Data.Models; + +namespace LeedsBeerQuest.API.Tests.Data.Services; + +public static class TestData +{ + public static IEnumerable VenueTestData = new[] + { + new Venue + { + Id = 1, + Name = "Venue 1", + CategoryId = 1, + Category = new Category + { + Id = 1, + Name = "Category 1" + }, + Url = "Venue URL", + DateAttended = new DateTime(), + Excerpt = "Venue Excerpt", + Thumbnail = "Venue Thumbnail", + Latitude = 1.23456m, + Longitude = -4.27384m, + Address = "Venue 1 Address", + Phone = "07777777777", + Twitter = "Venue Twitter", + StarsBeer = 1.5m, + StarsAtmosphere = 3, + StarsAmenities = 4.5m, + StarsValue = 4, + Tags = new List() + } + }; +} \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/VenueServiceTests.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/VenueServiceTests.cs new file mode 100644 index 0000000..0c99a40 --- /dev/null +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/VenueServiceTests.cs @@ -0,0 +1,50 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using LeedsBeerQuest.API.Data.Contexts; +using LeedsBeerQuest.API.Data.Services; +using Microsoft.EntityFrameworkCore; +using NUnit.Framework; + +namespace LeedsBeerQuest.API.Tests.Data.Services; + +[TestFixture] +public class VenueServiceTests +{ + private LeedsBeerQuestDbContext _context; + private VenueService _venueService; + + [SetUp] + public void Setup() + { + var builder = new DbContextOptionsBuilder() + .UseInMemoryDatabase($"VenueServiceTests.{Guid.NewGuid().ToString()}"); + _context = new LeedsBeerQuestDbContext(builder.Options); + + _venueService = new VenueService(_context); + } + + [Test] + public async Task GetAllVenues_ReturnsAllVenues_WhenAllVenuesArePresentInDatabase() + { + var venueTestData = TestData.VenueTestData.ToArray(); + _context.Venues.AddRange(venueTestData); + await _context.SaveChangesAsync(); + + var result = await _venueService.GetAllVenues(); + var resultArray = result.ToArray(); + + Assert.AreEqual(1, resultArray.Length); + + Assert.AreEqual(venueTestData[0].Id, resultArray[0].Id); + Assert.AreEqual(venueTestData[0].Name, resultArray[0].Name); + } + + [Test] + public async Task GetAllVenues_ReturnsNoVenues_WhenNoVenuesArePresentInDatabase() + { + var result = await _venueService.GetAllVenues(); + + Assert.AreEqual(0, result.Count()); + } +} \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/LeedsBeerQuest.API.Tests.csproj b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/LeedsBeerQuest.API.Tests.csproj index 4e170f5..263a1e1 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/LeedsBeerQuest.API.Tests.csproj +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/LeedsBeerQuest.API.Tests.csproj @@ -8,10 +8,16 @@ + + + + + + diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Controllers/VenueController.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Controllers/VenueController.cs index 4a51204..4a54567 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Controllers/VenueController.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Controllers/VenueController.cs @@ -1,5 +1,6 @@ using LeedsBeerQuest.API.Data.Contexts; using LeedsBeerQuest.API.Data.Models; +using LeedsBeerQuest.API.Data.Services.Interfaces; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -7,20 +8,19 @@ namespace LeedsBeerQuest.API.Controllers; [ApiController] [Route("[controller]")] -public class VenueController +public class VenueController : ControllerBase { - private readonly LeedsBeerQuestDbContext _lbqContext; + private readonly IVenueService _venueService; - public VenueController(LeedsBeerQuestDbContext lbqContext) + public VenueController(IVenueService venueService) { - _lbqContext = lbqContext; + _venueService = venueService; } [HttpGet] - public async Task> Get() + public async Task Get() { - return await _lbqContext.Venues - .Include(v => v.Category) - .ToListAsync(); + var venues = await _venueService.GetAllVenues(); + return Ok(venues); } } \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Contexts/LeedsBeerQuestDbContext.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Contexts/LeedsBeerQuestDbContext.cs index cbaa27b..e4a9013 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Contexts/LeedsBeerQuestDbContext.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Contexts/LeedsBeerQuestDbContext.cs @@ -6,7 +6,7 @@ namespace LeedsBeerQuest.API.Data.Contexts; public class LeedsBeerQuestDbContext : DbContext { - public LeedsBeerQuestDbContext() + public LeedsBeerQuestDbContext(DbContextOptions options) : base(options) { } @@ -16,12 +16,12 @@ public class LeedsBeerQuestDbContext : DbContext public DbSet Categories { get; set; } public DbSet Tags { get; set; } - protected override void OnConfiguring(DbContextOptionsBuilder options) - => options.UseSqlite("Data Source=lbq.db"); + // protected override void OnConfiguring(DbContextOptionsBuilder options) + // => options.UseSqlite("Data Source=lbq.db"); - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - var seeder = new LeedsBeerQuestSeeder(modelBuilder, ""); - seeder.Seed(); - } + // protected override void OnModelCreating(ModelBuilder modelBuilder) + // { + // var seeder = new LeedsBeerQuestSeeder(modelBuilder, ""); + // seeder.Seed(); + // } } \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/Interfaces/IVenueService.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/Interfaces/IVenueService.cs index 88eee3a..017d5f7 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/Interfaces/IVenueService.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/Interfaces/IVenueService.cs @@ -1,6 +1,8 @@ +using LeedsBeerQuest.API.Data.Models; + namespace LeedsBeerQuest.API.Data.Services.Interfaces; public interface IVenueService { - + public Task> GetAllVenues(); } \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/VenueService.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/VenueService.cs index 1159f26..283414d 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/VenueService.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/VenueService.cs @@ -1,6 +1,24 @@ +using LeedsBeerQuest.API.Data.Contexts; +using LeedsBeerQuest.API.Data.Models; +using LeedsBeerQuest.API.Data.Services.Interfaces; +using Microsoft.EntityFrameworkCore; + namespace LeedsBeerQuest.API.Data.Services; -public class VenueService +public class VenueService : IVenueService { - + private readonly LeedsBeerQuestDbContext _context; + + public VenueService(LeedsBeerQuestDbContext context) + { + _context = context; + } + + public async Task> GetAllVenues() + { + return await _context.Venues + .Include(v => v.Category) + .Include(v => v.Tags) + .ToListAsync(); + } } \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Program.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Program.cs index 383db7c..0d7497b 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Program.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Program.cs @@ -1,4 +1,4 @@ -using LeedsBeerQuest.API.Data.Contexts; +using LeedsBeerQuest.API; var builder = WebApplication.CreateBuilder(args); @@ -10,7 +10,7 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); // App services -builder.Services.AddScoped(); +builder.Services.AddServiceDependencies(); var app = builder.Build(); diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs new file mode 100644 index 0000000..c5cc4b5 --- /dev/null +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs @@ -0,0 +1,18 @@ +using LeedsBeerQuest.API.Data.Contexts; +using LeedsBeerQuest.API.Data.Services; +using LeedsBeerQuest.API.Data.Services.Interfaces; +using Microsoft.EntityFrameworkCore; + +namespace LeedsBeerQuest.API; + +public static class ServiceDependencies +{ + public static void AddServiceDependencies(this IServiceCollection serviceCollection) + { + var dbOptions = new DbContextOptionsBuilder(); + dbOptions.UseSqlite("Data Source=lbq.db"); + + serviceCollection.AddScoped(_ => new LeedsBeerQuestDbContext(dbOptions.Options)); + serviceCollection.AddScoped(); + } +} \ No newline at end of file