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

Tsylia/ Fixed the retrieving children for current parent (with or without him). #1009

Merged
merged 3 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -94,6 +94,7 @@ public async Task<IActionResult> GetByParentIdForAdmin(Guid parentId, [FromQuery
/// <summary>
/// Get all user's children from the database.
/// </summary>
/// <param name="isGetParent">Retrieve the parents along with the children.</param>
/// <param name="offsetFilter">Filter to get a part of all children that were found.</param>
/// <returns>The result is a <see cref="SearchResult{ChildDto}"/> that contains the count of all found children and a list of children that were received.</returns>
[HasPermission(Permissions.ChildRead)]
Expand All @@ -103,11 +104,11 @@ public async Task<IActionResult> GetByParentIdForAdmin(Guid parentId, [FromQuery
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[HttpGet]
public async Task<IActionResult> GetUsersChildren([FromQuery] OffsetFilter offsetFilter)
public async Task<IActionResult> GetUsersChildren([FromQuery] OffsetFilter offsetFilter, [FromQuery] bool isGetParent = false)
{
string userId = GettingUserProperties.GetUserId(User);

return Ok(await service.GetByUserId(userId, offsetFilter).ConfigureAwait(false));
return Ok(await service.GetByUserId(userId, isGetParent, offsetFilter).ConfigureAwait(false));
}

/// <summary>
Expand Down
13 changes: 10 additions & 3 deletions OutOfSchool/OutOfSchool.WebApi/Services/ChildService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,22 +219,29 @@ public async Task<SearchResult<ChildDto>> GetByParentIdOrderedByFirstName(Guid p
}

/// <inheritdoc/>
public async Task<SearchResult<ChildDto>> GetByUserId(string userId, OffsetFilter offsetFilter)
public async Task<SearchResult<ChildDto>> GetByUserId(string userId, bool isGetParent, OffsetFilter offsetFilter)
{
this.ValidateUserId(userId);
this.ValidateOffsetFilter(offsetFilter);

logger.LogDebug($"Getting Child's for User started. Looking UserId = {userId}.");

var totalAmount = await childRepository.Count(x => x.Parent.UserId == userId).ConfigureAwait(false);
Expression<Func<Child, bool>> predicate = x => x.Parent.UserId == userId;

if (!isGetParent)
{
predicate = predicate.And(x => !x.IsParent);
}

var totalAmount = await childRepository.Count(predicate).ConfigureAwait(false);

var sortExpression = new Dictionary<Expression<Func<Child, object>>, SortDirection>
{
{ x => x.FirstName, SortDirection.Ascending },
};

var children = await childRepository
.Get(offsetFilter.From, offsetFilter.Size, string.Empty, x => x.Parent.UserId == userId, sortExpression)
.Get(offsetFilter.From, offsetFilter.Size, string.Empty, predicate, sortExpression)
.ToListAsync()
.ConfigureAwait(false);

Expand Down
3 changes: 2 additions & 1 deletion OutOfSchool/OutOfSchool.WebApi/Services/IChildService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ public interface IChildService
/// Get children with some UserId.
/// </summary>
/// <param name="userId">Key in the User table.</param>
/// <param name="isGetParent">Retrieve the parents along with the children.</param>
/// <param name="offsetFilter">Filter to get a part of all children that were found.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.
/// The result is a <see cref="SearchResult{ChildDto}"/> that contains the count of all found children and a list of children that were received.</returns>
/// <exception cref="ArgumentNullException">If one of the parameters was null.</exception>
/// <exception cref="ArgumentException">If one of the parameters was not valid.</exception>
/// <exception cref="SqlException">If the database cannot execute the query.</exception>
Task<SearchResult<ChildDto>> GetByUserId(string userId, OffsetFilter offsetFilter);
Task<SearchResult<ChildDto>> GetByUserId(string userId, bool isGetParent, OffsetFilter offsetFilter);

/// <summary>
/// Get children by WorkshopId.
Expand Down