Skip to content

Commit

Permalink
Added the limit of the children that are created.
Browse files Browse the repository at this point in the history
  • Loading branch information
andriitsylia committed Feb 27, 2023
1 parent 51230ed commit fa71ede
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 215 deletions.
8 changes: 8 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Config/ParentConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OutOfSchool.WebApi.Config;

public class ParentConfig
{
public const string Name = "Parent";

public int ChildrenMaxNumber { get; set; }
}
30 changes: 26 additions & 4 deletions OutOfSchool/OutOfSchool.WebApi/Services/ChildService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Google.Type;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Nest;
using OutOfSchool.Services.Enums;
using OutOfSchool.Services.Models;
Expand All @@ -31,6 +32,7 @@ public class ChildService : IChildService
private readonly IEntityRepository<long, SocialGroup> socialGroupRepository;
private readonly ILogger<ChildService> logger;
private readonly IMapper mapper;
private readonly IOptions<ParentConfig> parentConfig;

/// <summary>
/// Initializes a new instance of the <see cref="ChildService"/> class.
Expand All @@ -40,13 +42,15 @@ public class ChildService : IChildService
/// <param name="socialGroupRepository">Repository for the social groups.</param>
/// <param name="logger">Logger.</param>
/// <param name="mapper">Automapper DI service.</param>
/// <param name="parentConfig">Parent configuration.</param>
public ChildService(
IEntityRepository<Guid, Child> childRepository,
IParentRepository parentRepository,
IEntityRepository<long, SocialGroup> socialGroupRepository,
ILogger<ChildService> logger,
IMapper mapper,
IApplicationRepository applicationRepository)
IApplicationRepository applicationRepository,
IOptions<ParentConfig> parentConfig)
{
this.childRepository = childRepository ?? throw new ArgumentNullException(nameof(childRepository));
this.parentRepository = parentRepository ?? throw new ArgumentNullException(nameof(parentRepository));
Expand All @@ -56,6 +60,7 @@ public ChildService(
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
this.applicationRepository =
applicationRepository ?? throw new ArgumentNullException(nameof(applicationRepository));
this.parentConfig = parentConfig ?? throw new ArgumentNullException(nameof(parentConfig));
}

/// <inheritdoc/>
Expand Down Expand Up @@ -111,10 +116,14 @@ async Task<Child> CreateChild()
/// <inheritdoc/>
public async Task<ChildrenCreationResultDto> CreateChildrenForUser(List<ChildDto> childrenDtos, string userId)
{
var parent = (await parentRepository.GetByFilter(p => p.UserId == userId).ConfigureAwait(false))
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 parentChildrenCount = (await GetChildrenListByParentId(parent.Id, false).ConfigureAwait(false))?.Count;

var children = new ChildrenCreationResultDto()
{
Parent = mapper.Map<ParentDTO>(parent),
Expand All @@ -124,8 +133,21 @@ public async Task<ChildrenCreationResultDto> CreateChildrenForUser(List<ChildDto
{
try
{
var child = await CreateChildForUser(childDto, userId).ConfigureAwait(false);
children.ChildrenCreationResults.Add(CreateChildResult(child));
if (parentChildrenCount < parentConfig.Value.ChildrenMaxNumber)
{
var child = await CreateChildForUser(childDto, userId).ConfigureAwait(false);
children.ChildrenCreationResults.Add(CreateChildResult(child));
parentChildrenCount++;
}
else
{
children.ChildrenCreationResults
.Add(CreateChildResult(
childDto,
false,
$"Refused to create a new child with {nameof(Child.ParentId)}:{childDto.ParentId}, {nameof(userId)}:{userId}: " +
$"the limit ({parentConfig.Value.ChildrenMaxNumber}) of the children for parents was reached."));
}
}
catch (Exception ex) when (ex is ArgumentNullException
|| ex is ArgumentException
Expand Down
1 change: 1 addition & 0 deletions OutOfSchool/OutOfSchool.WebApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static void AddApplicationServices(this WebApplicationBuilder builder)
services.Configure<ProviderAdminConfig>(configuration.GetSection(ProviderAdminConfig.Name));
services.Configure<CommunicationConfig>(configuration.GetSection(CommunicationConfig.Name));
services.Configure<GeocodingConfig>(configuration.GetSection(GeocodingConfig.Name));
services.Configure<ParentConfig>(configuration.GetSection(ParentConfig.Name));

services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddAuthentication("Bearer")
Expand Down
Loading

0 comments on commit fa71ede

Please sign in to comment.