Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ivanchuk/Child refactor #165

Merged
merged 5 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,46 @@ public async Task GetChildById_WhenIdIsNotValid_ShouldReturnNull(long id)
Assert.AreEqual(result.Value, null);
}

[Test]
[TestCase(1)]
public async Task GetByParentId_WhenIdIsValid_ShouldReturnOkResultObject(long id)
{
// Arrange
service.Setup(x => x.GetAllByParent(id)).ReturnsAsync(children.Where(p => p.ParentId == id));

// Act
var result = await controller.GetByParentId(id).ConfigureAwait(false) as OkObjectResult;

// Assert
Assert.That(result, Is.Not.Null);
Assert.AreEqual(200, result.StatusCode);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to use constants for 200 and 204 status code))
cold use this Assert.IsNotNull(result); for isnot null check))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dear @yfedo,
Thank you a lot for your advice. I have implemented them in the current PR.

}

[Test]
[TestCase(0)]
public void GetByParentId_WhenIdIsNotValid_ShouldThrowArgumentOutOfRangeException(long id)
{
// Assert
Assert.That(
async () => await controller.GetByParentId(id),
Throws.Exception.TypeOf<ArgumentOutOfRangeException>());
}

[Test]
[TestCase(10)]
public async Task GetByParentId_WhenIdIsNotValid_ShouldReturnNull(long id)
{
// Arrange
service.Setup(x => x.GetAllByParent(id)).ReturnsAsync(children.Where(p => p.ParentId == id));

// Act
var result = await controller.GetByParentId(id).ConfigureAwait(false) as NoContentResult;

// Assert
Assert.That(result, Is.Not.Null);
Assert.AreEqual(204, result.StatusCode);
}

[Test]
public async Task CreateChild_WhenModelIsValid_ShouldReturnCreatedAtActionResult()
{
Expand Down
24 changes: 24 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Controllers/ChildController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public ChildController(IChildService service, IStringLocalizer<SharedResource> l
/// Get all children from the database.
/// </summary>
/// <returns>List of all children.</returns>
[Authorize(Roles = "admin")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
Expand Down Expand Up @@ -67,6 +68,29 @@ public async Task<IActionResult> GetById(long id)
return Ok(await service.GetById(id).ConfigureAwait(false));
}

/// <summary>
/// Get all children from the database by parent id.
/// </summary>
/// <param name="id">Id of Parent.</param>
/// <returns>List of all children.</returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[HttpGet("{id}")]
public async Task<IActionResult> GetByParentId(long id)
{
this.ValidateId(id, localizer);

var children = await service.GetAllByParent(id).ConfigureAwait(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to implement checkout whether ParentId belongs to User, who called method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dear @OlhaHoliak,
Thank you a lot for your advice. I think it is a good idea, so I have implemented it.
Please review.
Thank you a lot.


if (!children.Any())
{
return NoContent();
}

return Ok(children);
}

/// <summary>
/// Method for creating a new child.
/// </summary>
Expand Down