Skip to content

Commit

Permalink
Merge pull request #5 from maurobernal/CA-4-crud-for-students
Browse files Browse the repository at this point in the history
Ca 4 crud for students
  • Loading branch information
maurobernal authored May 3, 2024
2 parents dc48197 + 8762daa commit 81c7c55
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 65 deletions.
4 changes: 0 additions & 4 deletions src/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="CQRS\Students\Queries\GetStudentsQueries\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using ca.Application.Common.Exceptions;
using ca.Application.Common.Interfaces;

namespace ca.Application.CQRS.Students.Commands.DeleteStudentCommand;
public class DeleteStudentCommand : IRequest<int>
{
internal int Id { get; set; }
public int AssignId(int id) => Id = id;
}


public class DeleteStudentHandler(IApplicationDbContext _context) : IRequestHandler<DeleteStudentCommand, int>
{
public async Task<int> Handle(DeleteStudentCommand request, CancellationToken cancellationToken)
{
var entity = await _context.Students.FindAsync(request.Id);
if (entity == null) throw new ApiNotFoundException($"el registro no existe:{request.Id}");

_context.Students.Remove(entity);
await _context.SaveChangesAsync(cancellationToken);
return request.Id;


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

using ca.Domain.Entities;

namespace ca.Application.CQRS.Students.Queries.GetStudentsQueries;
public class GetCoursesDtoOfList
{
public string Title { get; set; } = string.Empty;

private class Mapping : Profile
{
public Mapping()
{
CreateMap<Course, GetCoursesDtoOfList>();
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using ca.Application.Common.Bases;
using ca.Application.Common.Interfaces;
using ca.Application.Common.Mappings;
using ca.Application.Common.Models;

namespace ca.Application.CQRS.Students.Queries.GetStudentsQueries;
public class GetListStudentsQueries : PaginatedBaseDTO, IRequest<PaginatedList<GetStudentDtoOfList>>
{

}

public class GetListStudentHandler(IApplicationDbContext _context, IMapper _mapper) : IRequestHandler<GetListStudentsQueries, PaginatedList<GetStudentDtoOfList>>
{
public async Task<PaginatedList<GetStudentDtoOfList>> Handle(GetListStudentsQueries request, CancellationToken cancellationToken)
{
var res = await _context.Students
.ProjectTo<GetStudentDtoOfList>(_mapper.ConfigurationProvider)
.PaginatedListAsync(request.PageNumber, request.PageSize);
return res;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using ca.Domain.Entities;

namespace ca.Application.CQRS.Students.Queries.GetStudentsQueries;
public class GetStudentDtoOfList
{
public string fullName { get; set; } = string.Empty;

public int Id { get; set; }

public int Year { get; set; }

public int Month { get; set; }

public int Day { get; set; }

public List<GetCoursesDtoOfList> courses { get; set; } = new();

private class Mapping : Profile
{
public Mapping()
{
CreateMap<Student, GetStudentDtoOfList>()
.ForMember(dest => dest.fullName, orig => orig.MapFrom(m => $"{m.FirstName} {m.LastName}"))
.ForMember(dest => dest.Year, orig => orig.MapFrom(m => m.Birthdate.Year))
.ForMember(dest => dest.Month, orig => orig.MapFrom(m => m.Birthdate.Month))
.ForMember(dest => dest.Day, orig => orig.MapFrom(m => m.Birthdate.Day))
;
}
}
}


Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
namespace ca.Application.CQRS.Students.Queries.GetStudentQueries;
using ca.Domain.Entities;

namespace ca.Application.CQRS.Students.Queries.GetStudentQueries;
public class GetCoursesDto
{
public string Title { get; set; } = string.Empty;

private class Mapping : Profile
{
public Mapping()
{
CreateMap<Course, GetCoursesDto>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@ namespace ca.Application.CQRS.Students.Queries.GetStudentQueries;
public class GetStudentDto
{
public string fullName { get; set; } = string.Empty;
public int Id;
public int Year;

public int Id { get; set; }

public int Year { get; set; }

public int Month { get; set; }

public int Day { get; set; }

public List<GetCoursesDto> courses {get;set;} = new();
/*

private class Mapping : Profile
{
public Mapping()
{
CreateMap<Student, GetStudentDto>()
.ForMember(dest => dest.fullName, orig => orig.MapFrom(m => $"{ m.FirstName} {m.LastName}") )
.ForMember(dest => dest.fullName, orig => orig.MapFrom(m => $"{m.FirstName} {m.LastName}"))
.ForMember(dest => dest.Year, orig => orig.MapFrom(m => m.Birthdate.Year))
;
.ForMember(dest => dest.Month, orig => orig.MapFrom(m => m.Birthdate.Month))
.ForMember(dest => dest.Day, orig => orig.MapFrom(m => m.Birthdate.Day))
;
}
}
*/

}

Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,19 @@ public class GetStudentQueries : IRequest<GetStudentDto>
}


public class GetStudentQueriesHandler : IRequestHandler<GetStudentQueries, GetStudentDto>
public class GetStudentQueriesHandler(IApplicationDbContext _context, IMapper _mapper) : IRequestHandler<GetStudentQueries, GetStudentDto>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
public GetStudentQueriesHandler(IApplicationDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<GetStudentDto> Handle(GetStudentQueries request, CancellationToken cancellationToken)
{
var student2 = await _context.Students.AsNoTracking()
.SingleOrDefaultAsync(s => s.Id == request.id);

int studentId = 0;
if (student2 != null) studentId = student2.Id;


return new GetStudentDto();
var entity = await _context.Students
.AsNoTracking()
.Include(c => c.Courses)
.ProjectTo<GetStudentDto>(_mapper.ConfigurationProvider)
.FirstOrDefaultAsync(r => r.Id == request.id);

if (entity == null) throw new ApiNotFoundException($"el registro no existe:{request.id}");

return entity;

}
}
38 changes: 30 additions & 8 deletions src/Web/Endpoints/Students.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using ca.Application.CQRS.Students.Commands.CreateStudentCommand;
using ca.Application.Common.Models;
using ca.Application.CQRS.Students.Commands.CreateStudentCommand;
using ca.Application.CQRS.Students.Commands.DeleteStudentCommand;
using ca.Application.CQRS.Students.Commands.PutStudentCommand;
using ca.Application.CQRS.Students.Queries.GetStudentQueries;
using ca.Application.CQRS.Students.Queries.GetStudentsQueries;
using ca.Application.CQRS.TodoItems.Commands.CreateTodoItem;
using Microsoft.AspNetCore.Authorization;

Expand All @@ -9,15 +12,32 @@ namespace ca.Web.Endpoints;
public class Students : EndpointGroupBase
{


public override void Map(WebApplication app)
{
app.MapGroup(this)
.MapGet(GetStudent,"{id}")
.MapGet(GetListStudent)
.MapPost(PostStudent)
.MapPut(PutStudent, "{id}")
.MapDelete(DeleteStudent, "{id}");
}


[AllowAnonymous]
public Task<GetStudentDto> GetStudent(ISender sender, [AsParameters] GetStudentQueries query)
public Task<GetStudentDto> GetStudent(ISender sender, int id)
{
var query = new GetStudentQueries();
query.id = id;
return sender.Send(query);
}

[AllowAnonymous]
public async Task<PaginatedList<GetStudentDtoOfList>> GetListStudent(ISender sender, [AsParameters] GetListStudentsQueries query )
{
var res = await sender.Send(query);
return res;
}


[AllowAnonymous]
public Task<int> PostStudent(ISender sender, CreateStudentCommand query)
Expand All @@ -32,11 +52,13 @@ public async Task<IResult> PutStudent(ISender sender, int id, PutStudentCommand
return Results.Ok(await sender.Send(command));
}

public override void Map(WebApplication app)
[AllowAnonymous]
public async Task<IResult> DeleteStudent(ISender sender, int id)
{
app.MapGroup(this)
.MapGet(GetStudent)
.MapPost(PostStudent)
.MapPut(PutStudent,"{id}");
var command = new DeleteStudentCommand();
command.AssignId(id);
return Results.Ok(await sender.Send(command));
}


}
Loading

0 comments on commit 81c7c55

Please sign in to comment.