mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 18:11:10 +00:00
Adds refactoring
This commit is contained in:
1 parent
9d840db864
commit
f37d06f8df
9 files changed
+109
-49
No files matched your search
+8
-8
@@ -60,7 +60,7 @@ public class BookMappingConfig : IRegister
|
||||
.Map(dest => dest.ISBNs, src => src.ISBNs)
|
||||
.Map(dest => dest.Ratings, src => src.Ratings)
|
||||
.Map(dest => dest.Created, src => src.Created)
|
||||
.Map(dest => dest.Updated, src => src.Updated);
|
||||
.Map(dest => dest.Updated, src => src.Updated.HasValue ? src.Updated : null);
|
||||
|
||||
config.NewConfig<BookRating, BookRatingDto>()
|
||||
.Map(dest => dest.Value, src => src.Value)
|
||||
@@ -71,7 +71,7 @@ public class BookMappingConfig : IRegister
|
||||
config.NewConfig<Optional<BookRatingSource>, BookRatingSource?>()
|
||||
.MapWith(src => src.HasValue ? src.Value : default);
|
||||
|
||||
|
||||
// from BookDto to Book
|
||||
config.NewConfig<BookDto, Book>()
|
||||
.MapWith(src => Book.Create(
|
||||
BookId.Create(src.Id),
|
||||
@@ -101,11 +101,11 @@ public class BookMappingConfig : IRegister
|
||||
).Value : Optional<LanguageInfo>.None(),
|
||||
Optional<string>.FromNullable(src.Publisher),
|
||||
Optional<int>.FromNullable(src.PageCount)
|
||||
),
|
||||
).Value,
|
||||
Enum.Parse<BookFormat>(src.Format),
|
||||
Optional<string>.FromNullable(src.Edition),
|
||||
Optional<int>.FromNullable(src.VolumeNumber),
|
||||
Optional<BookSeries>.None(), // Assuming BookSeries is not part of BookDto
|
||||
Optional<BookSeries>.None(),
|
||||
Optional<string>.FromNullable(src.ASIN),
|
||||
Optional<string>.FromNullable(src.GoodreadsId),
|
||||
Optional<string>.FromNullable(src.LCCN),
|
||||
@@ -115,17 +115,17 @@ public class BookMappingConfig : IRegister
|
||||
Optional<string>.FromNullable(src.GoogleBooksId),
|
||||
Optional<string>.FromNullable(src.BarnesAndNobleId),
|
||||
Optional<string>.FromNullable(src.AppleBooksId),
|
||||
src.Created,
|
||||
Optional<DateTime>.FromNullable(src.Updated),
|
||||
src.ISBNs.Select(i => Isbn.Create(i.Value, i.Format).Value).ToList(),
|
||||
new List<MediaContributorId>(), // Assuming MediaContributorId is not part of BookDto
|
||||
new List<MediaContributorId>(),
|
||||
src.Ratings.Select(r => BookRating.Create(
|
||||
r.Value,
|
||||
r.MaxValue,
|
||||
Optional<BookRatingSource>.FromNullable(r.Source),
|
||||
Optional<int>.FromNullable(r.VoteCount)
|
||||
).Value).ToList()
|
||||
).Value) // Assuming Create returns ErrorOr<Book>, we take the Value
|
||||
.Ignore(dest => dest.Created)
|
||||
.Ignore(dest => dest.Updated);
|
||||
).Value);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+4
-3
@@ -125,7 +125,7 @@ public class AddBookCommandHandler : IRequestHandler<AddBookCommand, ErrorOr<Boo
|
||||
return originalLanguageInfoResult.Errors;
|
||||
originalLanguageInfo = originalLanguageInfoResult.Value;
|
||||
}
|
||||
WrittenContentMetadata metadata = WrittenContentMetadata.Create(
|
||||
var metadataResult = WrittenContentMetadata.Create(
|
||||
request.Metadata.Title,
|
||||
Optional<string>.FromNullable(request.Metadata.OriginalTitle),
|
||||
Optional<string>.FromNullable(request.Metadata.Description),
|
||||
@@ -137,9 +137,10 @@ public class AddBookCommandHandler : IRequestHandler<AddBookCommand, ErrorOr<Boo
|
||||
Optional<string>.FromNullable(request.Metadata.Publisher),
|
||||
Optional<int>.FromNullable(request.Metadata.PageCount)
|
||||
);
|
||||
|
||||
if (metadataResult.IsError)
|
||||
return metadataResult.Errors;
|
||||
var createBookResult = Book.Create(
|
||||
metadata,
|
||||
metadataResult.Value,
|
||||
request.Format,
|
||||
Optional<string>.FromNullable(request.Edition),
|
||||
request.VolumeNumber ?? default,
|
||||
|
||||
+38
-1
@@ -1,6 +1,8 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentValidation;
|
||||
using Lumina.Domain.Common.Enums;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using System.Text.RegularExpressions;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.WrittenContentLibrary.BooksLibrary.Books.Commands;
|
||||
@@ -157,7 +159,12 @@ public class AddBookCommandValidator : AbstractValidator<AddBookCommand>
|
||||
{
|
||||
isbn.RuleFor(i => i.Value)
|
||||
.NotEmpty().WithMessage(Errors.WrittenContent.IsbnValueCannotBeEmpty.Code)
|
||||
.MaximumLength(13).WithMessage(Errors.WrittenContent.InvalidIsbn13Format.Code);
|
||||
.Matches(i => i.Format == IsbnFormat.Isbn13
|
||||
? @"^(?:ISBN(?:-13)?:? )?(?=[0-9]{13}$|(?=(?:[0-9]+[-\ ]){4})[-\ 0-9]{17}$)97[89][-\ ]?[0-9]{1,5}[-\ ]?[0-9]+[-\ ]?[0-9]+[-\ ]?[0-9]$"
|
||||
: @"^(?:ISBN(?:-10)?:? )?(?=[0-9X]{10}$|(?=(?:[0-9]+[-\ ]){3})[-\ 0-9X]{13}$)[0-9]{1,5}[-\ ]?[0-9]+[-\ ]?[0-9]+[-\ ]?[0-9X]$")
|
||||
.WithMessage(i => i.Format == IsbnFormat.Isbn13
|
||||
? Errors.WrittenContent.InvalidIsbn13Format.Code
|
||||
: Errors.WrittenContent.InvalidIsbn10Format.Code);
|
||||
isbn.RuleFor(i => i.Format)
|
||||
.IsInEnum().WithMessage(Errors.WrittenContent.UnknownIsbnFormat.Code);
|
||||
});
|
||||
@@ -196,5 +203,35 @@ public class AddBookCommandValidator : AbstractValidator<AddBookCommand>
|
||||
.WithMessage(Errors.Metadata.RatingVoteCountMustBePositive.Code);
|
||||
});
|
||||
}
|
||||
|
||||
public static bool ValidateIsbn10(string isbn)
|
||||
{
|
||||
isbn = isbn.Replace("-", "").Replace(" ", "").ToUpper();
|
||||
if (!Regex.IsMatch(isbn, @"^(?:\d{9}[\dX])$"))
|
||||
return false;
|
||||
|
||||
int sum = 0;
|
||||
for (int i = 0; i < 9; i++)
|
||||
sum += (10 - i) * (isbn[i] - '0');
|
||||
|
||||
char lastChar = isbn[9];
|
||||
sum += lastChar == 'X' ? 10 : (lastChar - '0');
|
||||
|
||||
return sum % 11 == 0;
|
||||
}
|
||||
|
||||
public static bool ValidateIsbn13(string isbn)
|
||||
{
|
||||
if (!Regex.IsMatch(isbn, @"^(978|979)-[0-9]{1,5}-[0-9]+-[0-9]+-[0-9]$"))
|
||||
return false;
|
||||
|
||||
isbn = isbn.Replace("-", "");
|
||||
int sum = 0;
|
||||
for (int i = 0; i < 12; i++)
|
||||
sum += (i % 2 == 0) ? isbn[i] - '0' : (isbn[i] - '0') * 3;
|
||||
|
||||
int checkDigit = (10 - (sum % 10)) % 10;
|
||||
return checkDigit == (isbn[12] - '0');
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in new issue
Block a user