diff --git a/OutOfSchool/OutOfSchool.WebApi.Tests/Controllers/WorkshopControllerTests.cs b/OutOfSchool/OutOfSchool.WebApi.Tests/Controllers/WorkshopControllerTests.cs index c94b545c73..7594bbbf30 100644 --- a/OutOfSchool/OutOfSchool.WebApi.Tests/Controllers/WorkshopControllerTests.cs +++ b/OutOfSchool/OutOfSchool.WebApi.Tests/Controllers/WorkshopControllerTests.cs @@ -55,7 +55,7 @@ public void Setup() #region GetWorkshops [Test] - public async Task GetWorkshops_WhenThereAreWOrkshops_ShouldReturnOkResultObject() + public async Task GetWorkshops_WhenThereAreWorkshops_ShouldReturnOkResultObject() { // Arrange workshopServiceMoq.Setup(x => x.GetAll()).ReturnsAsync(workshops); @@ -69,7 +69,7 @@ public async Task GetWorkshops_WhenThereAreWOrkshops_ShouldReturnOkResultObject( } [Test] - public async Task GetWorkshops_WhenThereIsNoTAnyWorkshop_ShouldReturnNoConterntResult() + public async Task GetWorkshops_WhenThereIsNoAnyWorkshop_ShouldReturnNoConterntResult() { // Arrange var emptyList = new List(); @@ -129,6 +129,53 @@ public async Task GetWorkshopById_WhenThereIsNoWorkshopWithId_ShouldReturnNoCont } #endregion + #region GetByProviderId + [Test] + [TestCase(0)] + public void GetByProviderId_WhenIdIsInvalid_ShouldThrowArgumentOutOfRangeException(long id) + { + // Arrange + workshopServiceMoq.Setup(x => x.GetWorkshopsByProviderId(id)).ReturnsAsync(workshops.Where(x => x.ProviderId == id)); + + // Assert + Assert.That( + async () => await controller.GetByProviderId(id), + Throws.Exception.TypeOf()); + } + + [Test] + [TestCase(1)] + public async Task GetByProviderId_WhenThereAreWorkshops_ShouldReturnOkResultObject(long id) + { + // Arrange + workshopServiceMoq.Setup(x => x.GetWorkshopsByProviderId(id)).ReturnsAsync(workshops.Where(x => x.ProviderId == id)); + + // Act + var result = await controller.GetByProviderId(id).ConfigureAwait(false) as OkObjectResult; + + // Assert + Assert.That(result, Is.Not.Null); + Assert.AreEqual(200, result.StatusCode); + Assert.AreEqual(2, (result.Value as IEnumerable).Count()); + } + + [Test] + [TestCase(3)] + public async Task GetWorkshops_WhenThereIsNoAnyWorkshop_ShouldReturnNoConterntResult(long id) + { + // Arrange + var emptyList = new List(); + workshopServiceMoq.Setup(x => x.GetWorkshopsByProviderId(id)).ReturnsAsync(emptyList); + + // Act + var result = await controller.GetByProviderId(id).ConfigureAwait(false) as NoContentResult; + + // Assert + Assert.That(result, Is.Not.Null); + Assert.AreEqual(204, result.StatusCode); + } + #endregion + #region CreateWorkshop [Test] public async Task CreateWorkshop_WhenModelIsValid_ShouldReturnCreatedAtActionResult() @@ -375,6 +422,7 @@ private IEnumerable FakeWorkshops() DaysPerWeek = 1, Head = "Head1", HeadDateOfBirth = new DateTime(1980, month: 12, 28), + ProviderId = 1, ProviderTitle = "ProviderTitle", DisabilityOptionsDesc = "Desc1", Website = "website1", @@ -396,6 +444,8 @@ private IEnumerable FakeWorkshops() DaysPerWeek = 2, Head = "Head2", HeadDateOfBirth = new DateTime(1980, month: 12, 28), + ProviderId = 1, + ProviderTitle = "ProviderTitle", DisabilityOptionsDesc = "Desc2", Website = "website2", Instagram = "insta2", @@ -416,7 +466,8 @@ private IEnumerable FakeWorkshops() DaysPerWeek = 3, Head = "Head3", HeadDateOfBirth = new DateTime(1980, month: 12, 28), - ProviderTitle = "ProviderTitle", + ProviderId = 2, + ProviderTitle = "ProviderTitleNew", DisabilityOptionsDesc = "Desc3", Website = "website3", Instagram = "insta3", @@ -437,7 +488,8 @@ private IEnumerable FakeWorkshops() DaysPerWeek = 4, Head = "Head4", HeadDateOfBirth = new DateTime(1980, month: 12, 28), - ProviderTitle = "ProviderTitle", + ProviderId = 2, + ProviderTitle = "ProviderTitleNew", DisabilityOptionsDesc = "Desc4", Website = "website4", Instagram = "insta4", @@ -458,7 +510,8 @@ private IEnumerable FakeWorkshops() DaysPerWeek = 5, Head = "Head5", HeadDateOfBirth = new DateTime(1980, month: 12, 28), - ProviderTitle = "ProviderTitle", + ProviderId = 2, + ProviderTitle = "ProviderTitleNew", DisabilityOptionsDesc = "Desc5", Website = "website5", Instagram = "insta5", diff --git a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/UnitTests/WorkshopServiceTests.cs b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/UnitTests/WorkshopServiceTests.cs index 242a628f9c..c74c56f470 100644 --- a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/UnitTests/WorkshopServiceTests.cs +++ b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/UnitTests/WorkshopServiceTests.cs @@ -136,7 +136,7 @@ public async Task GetWorkshopsByOrganization_WhenIdIsValid_ShouldReturnEntities( .ReturnsAsync(workshops); // Act - var result = await workshopService.GetWorkshopsByOrganization(id).ConfigureAwait(false); + var result = await workshopService.GetWorkshopsByProviderId(id).ConfigureAwait(false); // Assert Assert.Multiple(() => @@ -156,7 +156,7 @@ public async Task GetWorkshopsByOrganization_WhenThereIsNoWorkshop_ShouldReturnE .ReturnsAsync(emptyList); // Act - var result = await workshopService.GetWorkshopsByOrganization(id).ConfigureAwait(false); + var result = await workshopService.GetWorkshopsByProviderId(id).ConfigureAwait(false); // Assert Assert.Multiple(() => diff --git a/OutOfSchool/OutOfSchool.WebApi/Controllers/WorkshopController.cs b/OutOfSchool/OutOfSchool.WebApi/Controllers/WorkshopController.cs index aad870dd54..f21ab2e9c7 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Controllers/WorkshopController.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Controllers/WorkshopController.cs @@ -88,6 +88,33 @@ public async Task GetById(long id) return Ok(workshop); } + /// + /// Get workshop by Provider's Id. + /// + /// Provider's id. + /// , or no content. + /// The list of found entities by given Id. + /// No entity with given Id was found. + /// If any server error occures. For example: Id was less than one. + [AllowAnonymous] + [HttpGet("{id}")] + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable))] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task GetByProviderId(long id) + { + this.ValidateId(id, localizer); + + var workshops = await workshopService.GetWorkshopsByProviderId(id).ConfigureAwait(false); + + if (!workshops.Any()) + { + return NoContent(); + } + + return Ok(workshops); + } + /// /// Add new workshop to the database. /// diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopService.cs index 2fcb8ea30b..768b13959a 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopService.cs @@ -36,11 +36,11 @@ public interface IWorkshopService Task GetById(long id); /// - /// Get all workshops by organization Id. + /// Get all workshops by provider Id. /// - /// Organization's key. + /// Provider's key. /// A representing the result of the asynchronous operation. The task result contains a that contains elements from the input sequence. - Task> GetWorkshopsByOrganization(long id); + Task> GetWorkshopsByProviderId(long id); /// /// Update entity. diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopService.cs index afd01a4b92..a873f0fd9b 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopService.cs @@ -122,7 +122,8 @@ public async Task GetById(long id) return workshopDTO; } - public async Task> GetWorkshopsByOrganization(long id) + /// + public async Task> GetWorkshopsByProviderId(long id) { logger.Information($"Getting Workshop by organization started. Looking ProviderId = {id}.");