Add endpoint for venue lookup with ID

This commit is contained in:
Stedoss 2022-10-30 03:40:17 +00:00
parent d39ef32169
commit 7ae6e4da79
4 changed files with 55 additions and 3 deletions

View File

@ -2,6 +2,7 @@ using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using LeedsBeerQuest.API.Data.Contexts; using LeedsBeerQuest.API.Data.Contexts;
using LeedsBeerQuest.API.Data.Models;
using LeedsBeerQuest.API.Data.Services; using LeedsBeerQuest.API.Data.Services;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NUnit.Framework; using NUnit.Framework;
@ -27,9 +28,7 @@ public class VenueServiceTests
[Test] [Test]
public async Task GetAllVenues_ReturnsAllVenues_WhenAllVenuesArePresentInDatabase() public async Task GetAllVenues_ReturnsAllVenues_WhenAllVenuesArePresentInDatabase()
{ {
var venueTestData = TestData.VenueTestData.ToArray(); var venueTestData = await seedVenueTestData();
_context.Venues.AddRange(venueTestData);
await _context.SaveChangesAsync();
var result = await _venueService.GetAllVenues(); var result = await _venueService.GetAllVenues();
var resultArray = result.ToArray(); var resultArray = result.ToArray();
@ -47,4 +46,35 @@ public class VenueServiceTests
Assert.AreEqual(0, result.Count()); Assert.AreEqual(0, result.Count());
} }
[Test]
public async Task GetVenue_ReturnsVenue_WhenRequestedVenueExists()
{
var venueTestData = await seedVenueTestData();
var expectedVenueId = venueTestData[0].Id;
var expectedVenueName = venueTestData[0].Name;
var result = await _venueService.GetVenue(expectedVenueId);;
Assert.NotNull(result);
Assert.AreEqual(expectedVenueId, result.Id);
Assert.AreEqual(expectedVenueName, result.Name);
}
[Test]
public async Task GetVenue_ReturnsNull_WhenRequestedVenueDoesNotExist()
{
var result = await _venueService.GetVenue(1);
Assert.Null(result);
}
private async Task<Venue[]> seedVenueTestData()
{
var venueTestData = TestData.VenueTestData.ToArray();
_context.Venues.AddRange(venueTestData);
await _context.SaveChangesAsync();
return venueTestData;
}
} }

View File

@ -23,4 +23,17 @@ public class VenueController : ControllerBase
var venues = await _venueService.GetAllVenues(); var venues = await _venueService.GetAllVenues();
return Ok(venues); return Ok(venues);
} }
[HttpGet("{venueId:int}")]
public async Task<IActionResult> Get(int venueId)
{
if (venueId < 1)
{
return BadRequest("Requested venue ID must be more than zero");
}
var venue = await _venueService.GetVenue(venueId);
return venue == null ? NotFound() : Ok(venue);
}
} }

View File

@ -5,4 +5,5 @@ namespace LeedsBeerQuest.API.Data.Services.Interfaces;
public interface IVenueService public interface IVenueService
{ {
public Task<IEnumerable<Venue>> GetAllVenues(); public Task<IEnumerable<Venue>> GetAllVenues();
public Task<Venue?> GetVenue(int venueId);
} }

View File

@ -21,4 +21,12 @@ public class VenueService : IVenueService
.Include(v => v.Tags) .Include(v => v.Tags)
.ToListAsync(); .ToListAsync();
} }
public async Task<Venue?> GetVenue(int venueId)
{
return await _context.Venues
.Include(v => v.Category)
.Include(v => v.Tags)
.SingleOrDefaultAsync(v => v.Id == venueId);
}
} }