end of tp2
This commit is contained in:
@@ -12,8 +12,11 @@ public class GetBookDetailsDto
|
||||
public int PageCount { get; set; }
|
||||
public DateOnly PublishedDate { get; set; }
|
||||
public string? Genre { get; set; }
|
||||
public decimal? AverageRating { get; set; }
|
||||
public int? ReviewCount { get; set; }
|
||||
|
||||
public int AuthorId { get; set; }
|
||||
public string? AuthorNationality { get; set; }
|
||||
public GetAuthorDto? Author { get; set; }
|
||||
|
||||
public List<GetReviewDto>? Reviews { get; set; }
|
||||
|
||||
@@ -8,12 +8,11 @@ public class GetLoanDto
|
||||
public int Id { get; set; }
|
||||
|
||||
public int BookId { get; set; }
|
||||
public string? BookTitle { get; set; }
|
||||
public int MemberId { get; set; }
|
||||
public string? MemberFullName { get; set; }
|
||||
|
||||
public DateOnly LoanDate { get; set; }
|
||||
public DateOnly DueDate { get; set; }
|
||||
public DateOnly? ReturnDate { get; set; }
|
||||
|
||||
public GetBookDto? Book { get; set; }
|
||||
public GetMemberDto? Member { get; set; }
|
||||
}
|
||||
@@ -8,12 +8,12 @@ public class GetReviewDto
|
||||
public int Id { get; set; }
|
||||
|
||||
public int BookId { get; set; }
|
||||
public string? BookTitle { get; set; }
|
||||
|
||||
public int MemberId { get; set; }
|
||||
public string? MemberFullName { get; set; }
|
||||
|
||||
public int Rating { get; set; }
|
||||
public string? Comment { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public GetBookDto? Book { get; set; }
|
||||
public GetMemberDto? Member { get; set; }
|
||||
}
|
||||
@@ -13,18 +13,26 @@ public class DtoToEntityMappings : Profile
|
||||
public DtoToEntityMappings()
|
||||
{
|
||||
CreateMap<CreateBookDto, Book>();
|
||||
CreateMap<UpdateBookDto, Book>();
|
||||
CreateMap<UpdateBookDto, Book>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore());
|
||||
|
||||
CreateMap<CreateAuthorDto, Author>();
|
||||
CreateMap<UpdateAuthorDto, Author>();
|
||||
CreateMap<UpdateAuthorDto, Author>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore());
|
||||
|
||||
CreateMap<CreateMemberDto, Member>();
|
||||
CreateMap<UpdateMemberDto, Member>();
|
||||
CreateMap<UpdateMemberDto, Member>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore())
|
||||
.ForMember(
|
||||
dest => dest.Email,
|
||||
opt => opt.PreCondition(src => !string.IsNullOrEmpty(src.Email)));
|
||||
|
||||
CreateMap<CreateReviewDto, Review>();
|
||||
CreateMap<UpdateReviewDto, Review>();
|
||||
CreateMap<UpdateReviewDto, Review>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore());
|
||||
|
||||
CreateMap<CreateLoanDto, Loan>();
|
||||
CreateMap<UpdateLoanDto, Loan>();
|
||||
CreateMap<UpdateLoanDto, Loan>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore());
|
||||
}
|
||||
}
|
||||
@@ -19,16 +19,40 @@ public class EntityToDtoMappings : Profile
|
||||
opt.MapFrom(src =>
|
||||
src.Author!.FirstName + " " + src.Author.LastName)
|
||||
);
|
||||
CreateMap<Book, GetBookDetailsDto>();
|
||||
CreateMap<Book, GetBookDetailsDto>()
|
||||
.ForMember(
|
||||
dest => dest.AverageRating,
|
||||
opt => opt.MapFrom(src => src.Reviews.Any() ? src.Reviews.Average(r => r.Rating) : 0)
|
||||
)
|
||||
.ForMember(
|
||||
dest => dest.ReviewCount,
|
||||
opt => opt.MapFrom(src => src.Reviews.Count)
|
||||
);
|
||||
|
||||
CreateMap<Author, GetAuthorDto>();
|
||||
CreateMap<Author, GetAuthorDetailsDto>();
|
||||
|
||||
CreateMap<Loan, GetLoanDto>();
|
||||
CreateMap<Loan, GetLoanDto>()
|
||||
.ForMember(
|
||||
dest => dest.BookTitle,
|
||||
opt =>
|
||||
opt.MapFrom(src => src.Book!.Title))
|
||||
.ForMember(
|
||||
dest => dest.MemberFullName,
|
||||
opt =>
|
||||
opt.MapFrom(src => src.Member!.FirstName + ' ' + src.Member!.LastName));
|
||||
|
||||
CreateMap<Member, GetMemberDto>();
|
||||
CreateMap<Member, GetMemberDetailsDto>();
|
||||
|
||||
CreateMap<Review, GetReviewDto>();
|
||||
CreateMap<Review, GetReviewDto>()
|
||||
.ForMember(
|
||||
dest => dest.BookTitle,
|
||||
opt =>
|
||||
opt.MapFrom(src => src.Book!.Title))
|
||||
.ForMember(
|
||||
dest => dest.MemberFullName,
|
||||
opt =>
|
||||
opt.MapFrom(src => src.Member!.FirstName + ' ' + src.Member!.LastName));
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
using AutoMapper;
|
||||
using AutoMapper.EquivalencyExpression;
|
||||
using BookHive;
|
||||
using BookHive.MappingProfiles;
|
||||
using BookHive.Repositories;
|
||||
using FastEndpoints;
|
||||
using FastEndpoints.Security;
|
||||
@@ -38,16 +36,20 @@ builder.Services.AddScoped<LoanRepository>();
|
||||
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
MapperConfiguration mappingConfig = new(mc =>
|
||||
builder.Services.AddAutoMapper(cfg =>
|
||||
{
|
||||
mc.AddCollectionMappers();
|
||||
mc.AddProfile(new DtoToEntityMappings());
|
||||
mc.AddProfile(new EntityToDtoMappings());
|
||||
}, new LoggerFactory());
|
||||
cfg.AddCollectionMappers();
|
||||
}, typeof(Program).Assembly);
|
||||
|
||||
// MapperConfiguration mappingConfig = new(mc =>
|
||||
// {
|
||||
// mc.AddCollectionMappers();
|
||||
// mc.AddProfile(new DtoToEntityMappings());
|
||||
// mc.AddProfile(new EntityToDtoMappings());
|
||||
// }, new LoggerFactory());
|
||||
|
||||
AutoMapper.IMapper mapper = mappingConfig.CreateMapper();
|
||||
builder.Services.AddSingleton(mapper);
|
||||
// AutoMapper.IMapper mapper = mappingConfig.CreateMapper();
|
||||
// builder.Services.AddSingleton(mapper);
|
||||
|
||||
WebApplication app = builder.Build();
|
||||
app.UseAuthentication()
|
||||
|
||||
Reference in New Issue
Block a user