Create Category controller and service

This commit is contained in:
Stedoss 2022-10-30 17:46:52 +00:00
parent 727ab856f6
commit 1ca97bb03c
7 changed files with 152 additions and 6 deletions

View File

@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LeedsBeerQuest.API.Controllers;
using LeedsBeerQuest.API.Data.Models;
using LeedsBeerQuest.API.Data.Services.Interfaces;
using LeedsBeerQuest.API.Tests.Data.Services;
using LeedsBeerQuest.API.Tests.TestHelpers;
using NSubstitute;
using NUnit.Framework;
namespace LeedsBeerQuest.API.Tests.Controllers;
[TestFixture]
public class CategoryControllerTests
{
private ICategoryService _categoryService;
private CategoryController _categoryController;
[SetUp]
public void SetUp()
{
_categoryService = Substitute.For<ICategoryService>();
_categoryController = new CategoryController(_categoryService);
}
[Test]
public async Task GetCategories_ReturnsAllCategories()
{
_categoryService.GetCategories().Returns(TestData.CategoryTestData);
var result = await _categoryController.GetCategories();
var resultObject = ControllerTestHelper.ActionResultToOkResultValue<IEnumerable<Category>>(result);
Assert.AreEqual(TestData.CategoryTestData.Length, resultObject.Count());
}
}

View File

@ -0,0 +1,47 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using LeedsBeerQuest.API.Data.Contexts;
using LeedsBeerQuest.API.Data.Models;
using LeedsBeerQuest.API.Data.Services;
using LeedsBeerQuest.API.Data.Services.Interfaces;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
namespace LeedsBeerQuest.API.Tests.Data.Services;
[TestFixture]
public class CategoryServiceTests
{
private LeedsBeerQuestDbContext _context;
private ICategoryService _categoryService;
[SetUp]
public void SetUp()
{
var builder = new DbContextOptionsBuilder<LeedsBeerQuestDbContext>()
.UseInMemoryDatabase($"CategoryServiceTests.{Guid.NewGuid().ToString()}");
_context = new LeedsBeerQuestDbContext(builder.Options);
_categoryService = new CategoryService(_context);
}
[Test]
public async Task GetCategories_ReturnsAllCategories_WhenAllCategoriesArePresentInDatabase()
{
var categoryTestData = await seedCategoryTestData();
var result = await _categoryService.GetCategories();
Assert.AreEqual(categoryTestData.Length, result.Count());
}
private async Task<Category[]> seedCategoryTestData()
{
var categoryTestData = TestData.CategoryTestData;
_context.Categories.AddRange(categoryTestData);
await _context.SaveChangesAsync();
return categoryTestData;
}
}

View File

@ -6,7 +6,7 @@ namespace LeedsBeerQuest.API.Tests.Data.Services;
public static class TestData public static class TestData
{ {
public static readonly IEnumerable<Venue> VenueTestData = new[] public static IEnumerable<Venue> VenueTestData => new[]
{ {
new Venue new Venue
{ {
@ -34,4 +34,34 @@ public static class TestData
Tags = new List<Tag>() Tags = new List<Tag>()
} }
}; };
public static Category[] CategoryTestData => new[]
{
new Category
{
Id = 1,
Name = "Category 1"
},
new Category
{
Id = 2,
Name = "Category 2"
},
new Category
{
Id = 3,
Name = "Category 3"
},
new Category
{
Id = 4,
Name = "Category 4"
},
new Category
{
Id = 5,
Name = "Category 5"
},
};
} }

View File

@ -1,6 +1,23 @@
using LeedsBeerQuest.API.Data.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;
namespace LeedsBeerQuest.API.Controllers; namespace LeedsBeerQuest.API.Controllers;
public class CategoryController [ApiController]
[Route("[controller]")]
public class CategoryController : ControllerBase
{ {
private readonly ICategoryService _categoryService;
public CategoryController(ICategoryService categoryService)
{
_categoryService = categoryService;
}
[HttpGet]
public async Task<IActionResult> GetCategories()
{
var categories = await _categoryService.GetCategories();
return Ok(categories);
}
} }

View File

@ -1,6 +1,18 @@
using Microsoft.EntityFrameworkCore;
using LeedsBeerQuest.API.Data.Contexts;
using LeedsBeerQuest.API.Data.Models;
using LeedsBeerQuest.API.Data.Services.Interfaces;
namespace LeedsBeerQuest.API.Data.Services; namespace LeedsBeerQuest.API.Data.Services;
public class CategoryService public class CategoryService : ICategoryService
{ {
private readonly LeedsBeerQuestDbContext _context;
public CategoryService(LeedsBeerQuestDbContext context)
{
_context = context;
}
public async Task<IEnumerable<Category>> GetCategories() =>
await _context.Categories.ToListAsync();
} }

View File

@ -1,6 +1,8 @@
using LeedsBeerQuest.API.Data.Models;
namespace LeedsBeerQuest.API.Data.Services.Interfaces; namespace LeedsBeerQuest.API.Data.Services.Interfaces;
public interface ICategoryService public interface ICategoryService
{ {
public Task<IEnumerable<Category>> GetCategories();
} }

View File

@ -14,5 +14,6 @@ public static class ServiceDependencies
serviceCollection.AddTransient(_ => new LeedsBeerQuestDbContext(dbOptions.Options)); serviceCollection.AddTransient(_ => new LeedsBeerQuestDbContext(dbOptions.Options));
serviceCollection.AddScoped<IVenueService, VenueService>(); serviceCollection.AddScoped<IVenueService, VenueService>();
serviceCollection.AddScoped<ICategoryService, CategoryService>();
} }
} }