Skip to content

Commit

Permalink
Added the feature that allows the creation of a group of children by …
Browse files Browse the repository at this point in the history
…the list.
  • Loading branch information
andriitsylia committed Feb 14, 2023
1 parent 19c6235 commit 6b7f3ea
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ public async Task CreateChild_WhenModelIsValid_ShouldReturnCreatedAtActionResult
Assert.IsInstanceOf<CreatedAtActionResult>(result);
}

[Test]
public async Task CreateChildren_WhenModelIsValid_ShouldReturnOkObjectResult()
{
// Arrange
service.Setup(x => x.CreateChildrenForUser(children, currentUserId))
.ReturnsAsync(new ChildrenCreationResultDto());

// Act
var result = await controller.CreateChildren(children).ConfigureAwait(false);

// Assert
Assert.IsInstanceOf<OkObjectResult>(result);
}

[Test]
public async Task UpdateChild_WhenModelIsValid_ShouldReturnOkObjectResult()
{
Expand Down
25 changes: 25 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Controllers/V1/ChildController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,31 @@ public async Task<IActionResult> Create(ChildDto childDto)
child);
}

/// <summary>
/// Method for creating the list of the new user's children.
/// </summary>
/// <param name="childrenDtos">The list of the children entities to add.</param>
/// <returns>The list of the children that were created.</returns>
[HasPermission(Permissions.ChildAddNew)]
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(ChildDto))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[HttpPost]
public async Task<IActionResult> CreateChildren(List<ChildDto> childrenDtos)
{
string userId = GettingUserProperties.GetUserId(User);

var children = await service.CreateChildrenForUser(childrenDtos, userId).ConfigureAwait(false);

return Ok(new ChildrenCreationResponse()
{
Parent = children.Parent,
ChildrenCreationResults = children.ChildrenCreationResults,
});
}

/// <summary>
/// Update info about the user's child in the database.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Models/ChildCreationResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using OutOfSchool.WebApi.Common;

namespace OutOfSchool.WebApi.Models;

public class ChildCreationResult
{
public ChildDto Child { get; set; }

public bool IsSuccess { get; set; }

public string Message { get; set; }
}
10 changes: 10 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Models/ChildrenCreationResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using OutOfSchool.WebApi.Common;

namespace OutOfSchool.WebApi.Models;

public class ChildrenCreationResponse
{
public ParentDTO Parent { get; set; }

public List<ChildCreationResult> ChildrenCreationResults { get; set; }
}
10 changes: 10 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Models/ChildrenCreationResultDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using OutOfSchool.WebApi.Common;

namespace OutOfSchool.WebApi.Models;

public class ChildrenCreationResultDto
{
public ParentDTO Parent { get; set; }

public List<ChildCreationResult> ChildrenCreationResults { get; set; } = new List<ChildCreationResult>();
}
50 changes: 50 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Services/ChildService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using OutOfSchool.Services.Enums;
using OutOfSchool.Services.Models;
using OutOfSchool.Services.Repository;
using OutOfSchool.WebApi.Common;
using OutOfSchool.WebApi.Extensions;
using OutOfSchool.WebApi.Models;
using OutOfSchool.WebApi.Models.SocialGroup;
Expand Down Expand Up @@ -107,6 +108,55 @@ async Task<Child> CreateChild()
return mapper.Map<ChildDto>(newChild);
}

/// <inheritdoc/>
public async Task<ChildrenCreationResultDto> CreateChildrenForUser(List<ChildDto> childrenDtos, string userId)
{
var parent = (await parentRepository.GetByFilter(p => p.UserId == userId).ConfigureAwait(false))
.SingleOrDefault()
?? throw new UnauthorizedAccessException($"Trying to create a new children the Parent with {nameof(userId)}:{userId} was not found.");

var children = new ChildrenCreationResultDto()
{
Parent = mapper.Map<ParentDTO>(parent),
};

foreach (var childDto in childrenDtos)
{
try
{
var child = await CreateChildForUser(childDto, userId).ConfigureAwait(false);
children.ChildrenCreationResults.Add(CreateChildResult(child));
}
catch (Exception ex) when (ex is ArgumentNullException
|| ex is ArgumentException
|| ex is UnauthorizedAccessException
|| ex is DbUpdateException)
{
children.ChildrenCreationResults.Add(CreateChildResult(childDto, false, ex.Message));
logger.LogDebug(
$"There is an error while creating a new child with {nameof(Child.ParentId)}:{childDto.ParentId}, {nameof(userId)}:{userId}: {ex.Message}.");
}
catch (Exception ex)
{
children.ChildrenCreationResults.Add(CreateChildResult(childDto, false));
logger.LogDebug(
$"There is an error while creating a new child with {nameof(Child.ParentId)}:{childDto.ParentId}, {nameof(userId)}:{userId}: {ex.Message}.");
}
}

ChildCreationResult CreateChildResult(ChildDto childDto, bool isSuccess = true, string message = null)
{
return new ChildCreationResult()
{
Child = childDto,
IsSuccess = isSuccess,
Message = message,
};
}

return children;
}

/// <inheritdoc/>
public async Task<SearchResult<ChildDto>> GetByFilter(ChildSearchFilter filter)
{
Expand Down
11 changes: 11 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Services/IChildService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ public interface IChildService
/// <exception cref="DbUpdateException">If something wrong occurred while saving to the database.</exception>
Task<ChildDto> CreateChildForUser(ChildDto childDto, string userId);

/// <summary>
/// Create the list of the children for specified user.
/// If child's property ParentId is not equal to the parent's Id that was found by specified userId,
/// the child's property will be changed to the proper value: parent's Id that was found.
/// </summary>
/// <param name="childrenDtos">The list of the children to add.</param>
/// <param name="userId">The key in the User table.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.
/// The result contains a <see cref="ChildrenCreationResponse"/> that was created.</returns>
Task<ChildrenCreationResultDto> CreateChildrenForUser(List<ChildDto> childrenDtos, string userId);

/// <summary>
/// Get all children from the database.
/// </summary>
Expand Down

0 comments on commit 6b7f3ea

Please sign in to comment.