Skip to content

Commit

Permalink
feat: Add scheduling for children
Browse files Browse the repository at this point in the history
Add possibility to add scheduling items to children.
  • Loading branch information
LuukvH committed Jun 26, 2024
1 parent 5be655c commit dd21c6f
Show file tree
Hide file tree
Showing 29 changed files with 886 additions and 24 deletions.
10 changes: 1 addition & 9 deletions src/Services/Scheduling/Api/Controllers/GroupsController.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using KDVManager.Services.Scheduling.Application.Features.Groups.Commands.AddGroup;
using KDVManager.Services.Scheduling.Application.Features.Groups.Commands.AddGroup;
using KDVManager.Services.Scheduling.Application.Features.Groups.Queries.ListGroups;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
using KDVManager.Services.Scheduling.Application.Contracts.Pagination;
using System.Net;
using KDVManager.Services.Scheduling.Application.Contracts.Persistence;
using System.Net.Mime;
using KDVManager.Services.Scheduling.Application.Features.Groups.Commands.DeleteGroup;

namespace KDVManager.Services.Scheduling.Api.Controllers;
Expand Down
40 changes: 40 additions & 0 deletions src/Services/Scheduling/Api/Controllers/ScheduleItemsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using KDVManager.Services.Scheduling.Application.Features.Groups.Queries.ListGroups;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using KDVManager.Services.Scheduling.Application.Contracts.Pagination;
using KDVManager.Services.Scheduling.Application.Features.ScheduleItems.Queries.ListScheduleItems;
using System.Net;
using KDVManager.Services.Scheduling.Application.Features.ScheduleItems.Commands.AddScheduleItem;

namespace KDVManager.Services.Scheduling.Api.Controllers;

[ApiController]
[Route("v1/[controller]")]
public class ScheduleItemsController : ControllerBase
{
private readonly IMediator _mediator;
private readonly ILogger<ScheduleItemsController> _logger;

public ScheduleItemsController(IMediator mediator, ILogger<ScheduleItemsController> logger)
{
_logger = logger;
_mediator = mediator;
}

[HttpGet("", Name = "ListScheduleItems")]
public async Task<ActionResult<PagedList<ScheduleItemListVM>>> ListScheduleItems([FromQuery] ListScheduleItemsQuery listScheduleItemsQuery)
{
var dtos = await _mediator.Send(listScheduleItemsQuery);
Response.Headers.Add("x-Total", dtos.TotalCount.ToString());
return Ok(dtos);
}

[HttpPost(Name = "AddScheduleItem")]
[ProducesResponseType(typeof(Guid), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(UnprocessableEntityResponse), (int)HttpStatusCode.UnprocessableEntity)]
public async Task<ActionResult<Guid>> AddScheduleItem([FromBody] AddScheduleItemCommand addScheduleItemCommand)
{
var id = await _mediator.Send(addScheduleItemCommand);
return Ok(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Net;
using System.Threading.Tasks;
using System.Net;
using KDVManager.Services.Scheduling.Application.Exceptions;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http;

namespace KDVManager.Services.Scheduling.Api.Middleware;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using Microsoft.AspNetCore.Builder;

namespace KDVManager.Services.Scheduling.Api.Middleware;
namespace KDVManager.Services.Scheduling.Api.Middleware;

public static class ExceptionHandlerMiddlewareExtension
{
Expand Down
2 changes: 0 additions & 2 deletions src/Services/Scheduling/Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using KDVManager.Services.Scheduling.Api.Middleware;
using KDVManager.Services.Scheduling.Application;
using KDVManager.Services.Scheduling.Infrastructure;

var builder = WebApplication.CreateBuilder(args);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel.DataAnnotations;
using KDVManager.Services.Scheduling.Application.Exceptions;
using ValidationException = KDVManager.Services.Scheduling.Application.Exceptions.ValidationException;

public class ValidationError
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using KDVManager.Services.Scheduling.Application.Contracts.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using KDVManager.Services.Scheduling.Application.Exceptions;

namespace KDVManager.Services.Scheduling.Api.Services;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using KDVManager.Services.Scheduling.Application.Contracts.Persistence;
using KDVManager.Services.Scheduling.Domain.Entities;
using KDVManager.Services.Scheduling.Domain.Interfaces;

namespace KDVManager.Services.Scheduling.Application.Contracts.Persistence;

public interface IScheduleItemRepository : IAsyncRepository<ScheduleItem>
{
Task<IReadOnlyList<ScheduleItem>> GetScheduleItemsByChildIdAsync(Guid childId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using MediatR;

namespace KDVManager.Services.Scheduling.Application.Features.ScheduleItems.Commands.AddScheduleItem;

public class AddScheduleItemCommand : IRequest<Guid>
{
public string Name { get; set; }
public Guid ChildId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using KDVManager.Services.Scheduling.Application.Contracts.Persistence;
using KDVManager.Services.Scheduling.Domain.Entities;
using MediatR;

namespace KDVManager.Services.Scheduling.Application.Features.ScheduleItems.Commands.AddScheduleItem;

public class AddScheduleItemCommandHandler : IRequestHandler<AddScheduleItemCommand, Guid>
{
private readonly IScheduleItemRepository _scheduleItemRepository;
private readonly IMapper _mapper;

public AddScheduleItemCommandHandler(IScheduleItemRepository scheduleItemRepository, IMapper mapper)
{
_scheduleItemRepository = scheduleItemRepository;
_mapper = mapper;
}

public async Task<Guid> Handle(AddScheduleItemCommand request, CancellationToken cancellationToken)
{
var validator = new AddScheduleItemCommandValidator(_scheduleItemRepository);
var validationResult = await validator.ValidateAsync(request);

if (!validationResult.IsValid)
throw new Exceptions.ValidationException(validationResult);

var scheduleItem = _mapper.Map<ScheduleItem>(request);

scheduleItem = await _scheduleItemRepository.AddAsync(scheduleItem);

return scheduleItem.Id;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using FluentValidation;
using KDVManager.Services.Scheduling.Application.Contracts.Persistence;

namespace KDVManager.Services.Scheduling.Application.Features.ScheduleItems.Commands.AddScheduleItem;

public class AddScheduleItemCommandValidator : AbstractValidator<AddScheduleItemCommand>
{
private readonly IScheduleItemRepository _scheduleItemRepository;

public AddScheduleItemCommandValidator(IScheduleItemRepository scheduleItemRepository)
{
_scheduleItemRepository = scheduleItemRepository;

RuleFor(AddScheduleItemCommand => AddScheduleItemCommand.Name)
.NotEmpty()
.NotNull()
.MaximumLength(25);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using KDVManager.Services.Scheduling.Domain;
using KDVManager.Services.Scheduling.Application.Contracts.Pagination;
using MediatR;

namespace KDVManager.Services.Scheduling.Application.Features.ScheduleItems.Queries.ListScheduleItems;

public class ListScheduleItemsQuery : PageParameters, IRequest<PagedList<ScheduleItemListVM>>
{
public Guid ChildId { get; set; }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using KDVManager.Services.Scheduling.Application.Contracts.Persistence;
using KDVManager.Services.Scheduling.Application.Contracts.Pagination;
using KDVManager.Services.Scheduling.Domain.Entities;
using MediatR;

namespace KDVManager.Services.Scheduling.Application.Features.ScheduleItems.Queries.ListScheduleItems;

public class ListScheduleItemsQueryHandler : IRequestHandler<ListScheduleItemsQuery, List<ScheduleItemListVM>>
{
private readonly IScheduleItemRepository _scheduleItemRepository;
private readonly IMapper _mapper;

public ListScheduleItemsQueryHandler(IMapper mapper, IScheduleItemRepository scheduleItemRepository)
{
_scheduleItemRepository = scheduleItemRepository;
_mapper = mapper;
}

public async Task<List<ScheduleItemListVM>> Handle(ListScheduleItemsQuery request, CancellationToken cancellationToken)
{
var scheduleItems = await _scheduleItemRepository.GetScheduleItemsByChildIdAsync(request.ChildId);


List<ScheduleItemListVM> scheduleItemListVMs = _mapper.Map<List<ScheduleItemListVM>>(scheduleItems);

return new List<ScheduleItemListVM>(scheduleItemListVMs);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace KDVManager.Services.Scheduling.Application.Features.ScheduleItems.Queries.ListScheduleItems;

public class ScheduleItemListVM
{
public Guid Id { get; set; }
public Guid ChildId { get; set; }
public string Title { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using KDVManager.Services.Scheduling.Application.Contracts.Pagination;
using KDVManager.Services.Scheduling.Application.Features.Groups.Commands.AddGroup;
using KDVManager.Services.Scheduling.Application.Features.Groups.Queries.ListGroups;
using KDVManager.Services.Scheduling.Application.Features.ScheduleItems.Queries.ListScheduleItems;
using KDVManager.Services.Scheduling.Domain.Entities;

namespace KDVManager.Services.Scheduling.Application.Profiles;
Expand All @@ -14,6 +15,9 @@ public MappingProfile()
CreateMap<Group, GroupListVM>();
CreateMap<Group, AddGroupCommand>().ReverseMap();
CreateMap<ListGroupsQuery, PageParameters>();

CreateMap<ScheduleItem, ScheduleItemListVM>();
CreateMap<ListScheduleItemsQuery, PageParameters>();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
public class RecurringSchedulePattern
{
public Guid Id { get; set; }
public Guid TenantId { get; set; }
public Guid ScheduleItemId { get; set; }
public DayOfWeek StartDay { get; set; }
public TimeSpan StartTime { get; set; }
public DayOfWeek EndDay { get; set; }
public TimeSpan EndTime { get; set; }
}
12 changes: 12 additions & 0 deletions src/Services/Scheduling/Domain/Entities/ScheduleItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
public class ScheduleItem
{
public Guid Id { get; set; }
public Guid TenantId { get; set; }
public Guid ChildId { get; set; }
public string Title { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public ICollection<RecurringSchedulePattern> recurringSchedulePatterns { get; set; }
}
2 changes: 2 additions & 0 deletions src/Services/Scheduling/Infrastructure/ConfigureServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static IServiceCollection AddInfrastructureServices(this IServiceCollecti

services.AddScoped<IGroupRepository, GroupRepository>();

services.AddScoped<IScheduleItemRepository, ScheduleItemRepository>();

return services;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Services/Scheduling/Infrastructure/MigrationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public MigrationDbContext(DbContextOptions<MigrationDbContext> options) : base(o
}

public DbSet<Group> Groups { get; set; }
public DbSet<ScheduleItem> ScheduleItems { get; set; }
public DbSet<RecurringSchedulePattern> RecurringSchedulePatterns { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand Down
Loading

0 comments on commit dd21c6f

Please sign in to comment.