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

Ca 5 error in table students #4

Merged
merged 5 commits into from
May 3, 2024
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 @@ -20,7 +20,7 @@ public async Task<int> Handle(DeleteSkillsComands request, CancellationToken can
var ent= await _context.Skills.AsNoTracking()
.FirstOrDefaultAsync(f => f.Id == request.Id);

if (ent == null) throw new Common.Exceptions.NotFoundException($"La entidad no existe. Id:{request.Id}");
if (ent == null) throw new Common.Exceptions.ApiNotFoundException($"La entidad no existe. Id:{request.Id}");

var res= _context.Skills.Remove(ent);
await _context.SaveChangesAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public UpdateSkillHandler(IMapper mapper, IApplicationDbContext context)
public async Task<int> Handle(UpdateSkillComands request, CancellationToken cancellationToken)
{
var entity = await _context.Skills.FirstOrDefaultAsync(s => s.Id == request.Id);
if (entity == null) throw new Common.Exceptions.NotFoundException($"No existe el registro {request.Id}");
if (entity == null) throw new Common.Exceptions.ApiNotFoundException($"No existe el registro {request.Id}");

entity.Title = request.Title;
await _context.SaveChangesAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task<GetSkillDTO> Handle(GetSkillsQueries request, CancellationToke
.ProjectTo<GetSkillDTO>(_mapper.ConfigurationProvider)
.SingleOrDefaultAsync(s => s.Id == request.Id);

if (res == null) throw new Common.Exceptions.NotFoundException($"No existe el registro {request.Id}");
if (res == null) throw new Common.Exceptions.ApiNotFoundException($"No existe el registro {request.Id}");

return res;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

using ca.Application.Common.Exceptions;
using ca.Application.Common.Interfaces;

namespace ca.Application.CQRS.Students.Commands.PutStudentCommand;
public class PutStudentCommand : IRequest<int>
{
public int id { get; set; }
public string name { get; set; } = string.Empty;
public string surname { get; set; } = string.Empty;
public int year { get; set; }
public int month { get; set; }
public int day { get; set; }
}

public class PutStudentHandler(IApplicationDbContext _context) :
IRequestHandler<PutStudentCommand, int>
{
public async Task<int> Handle(PutStudentCommand request, CancellationToken cancellationToken)
{
// validate day/month/year is date
DateOnly dateOut;
if (!DateOnly.TryParse($"{request.year}-{request.month}-{request.day}", out dateOut))
throw new ApiValidationException($"los campos enviados no corresponden a una fecha: {request.year}-{request.month}-{request.day}");

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

// Update values
entity.FirstName = request.name;
entity.LastName = request.surname;
entity.Birthdate = dateOut;

await _context.SaveChangesAsync(cancellationToken);

return entity.Id;


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

using ca.Application.CQRS.Students.Commands.PutStudentCommand;

public class PutStudentValidator : AbstractValidator<PutStudentCommand>
{
public PutStudentValidator()
{
RuleFor(f => f.name).NotEmpty().NotNull().MinimumLength(10).MaximumLength(200);
RuleFor(f => f.surname).NotEmpty().NotNull().MinimumLength(10).MaximumLength(200);
RuleFor(f => f.year).GreaterThanOrEqualTo(1980).LessThanOrEqualTo(2099);
}
}
4 changes: 2 additions & 2 deletions src/Application/Common/Behaviours/AuthorizationBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
// Must be a member of at least one role in roles
if (!authorized)
{
throw new ForbiddenAccessException();
throw new ApiForbiddenAccessException();
}
}

Expand All @@ -67,7 +67,7 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe

if (!authorized)
{
throw new ForbiddenAccessException();
throw new ApiForbiddenAccessException();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Common/Behaviours/ValidationBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ValidationException = ca.Application.Common.Exceptions.ValidationException;
using ValidationException = ca.Application.Common.Exceptions.ApiValidationException;

namespace ca.Application.Common.Behaviours;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ca.Application.Common.Exceptions;

public class ApiForbiddenAccessException : Exception
{
public ApiForbiddenAccessException() : base() { }
}
9 changes: 9 additions & 0 deletions src/Application/Common/Exceptions/ApiNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ca.Application.Common.Exceptions;

public class ApiNotFoundException: Exception
{
public ApiNotFoundException(string title) : base(title) {


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

namespace ca.Application.Common.Exceptions;

public class ValidationException : Exception
public class ApiValidationException : Exception
{
public ValidationException()
public ApiValidationException()
: base("One or more validation failures have occurred.")
{
Errors = new Dictionary<string, string[]>();
}

public ValidationException(IEnumerable<ValidationFailure> failures)
public ApiValidationException(string error)
: base("One or more validation failures have occurred.")
{
Errors = new Dictionary<string, string[]>();
}
public ApiValidationException(IEnumerable<ValidationFailure> failures)
: this()
{
Errors = failures
Expand Down
6 changes: 0 additions & 6 deletions src/Application/Common/Exceptions/ForbiddenAccessException.cs

This file was deleted.

9 changes: 0 additions & 9 deletions src/Application/Common/Exceptions/NotFoundException.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Domain/Entities/Course.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ public class Course : BaseAuditableEntity
{
public string Title { get; set; } = string.Empty;

public virtual IList<Student> StudentsCourses { get; set; } = new List<Student>();
public virtual IList<Student> Students { get; set; } = new List<Student>();

}
7 changes: 2 additions & 5 deletions src/Domain/Entities/Student.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ public class Student: BaseAuditableEntity
public string LastName { get; set; } = string.Empty;
public int Identified { get; set; }
public DateOnly Birthdate { get; set;} = new DateOnly();

public int CourseId { get; set; }
public Course? Course { get; set; }
public virtual IList<Skill> Skills{ get; set; } = new List<Skill>();

public virtual IList<Course> Courses{ get; set; } = new List<Course>();

}
10 changes: 9 additions & 1 deletion src/Infrastructure/Data/ApplicationDbContextInitialiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ namespace ca.Infrastructure.Data;

public static class InitialiserExtensions
{
public static async Task InitialiseDatabaseAsync(this WebApplication app)
public static async Task ApplyMigrations(this WebApplication app)
{
using var scope = app.Services.CreateScope();

var initialiser = scope.ServiceProvider.GetRequiredService<ApplicationDbContextInitialiser>();

await initialiser.InitialiseAsync();

}

public static async Task LoadSeeds(this WebApplication app)
{
using var scope = app.Services.CreateScope();

var initialiser = scope.ServiceProvider.GetRequiredService<ApplicationDbContextInitialiser>();

await initialiser.SeedAsync();
}
}
Expand Down
Loading