Changeset d300631 for ChapterX.Application
- Timestamp:
- 04/28/26 14:53:32 (2 months ago)
- Branches:
- main
- Children:
- b373fea
- Parents:
- dc383dd
- Location:
- ChapterX.Application
- Files:
-
- 3 edited
-
Abstractions/IApplicationDbContext.cs (modified) (2 diffs)
-
Auth/RegisterHandler.cs (modified) (3 diffs)
-
Story/Commands/AddHandler.cs (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ChapterX.Application/Abstractions/IApplicationDbContext.cs
rdc383dd rd300631 1 1 using Microsoft.EntityFrameworkCore; 2 using Microsoft.EntityFrameworkCore.Storage; 2 3 using UserEntity = ChapterX.Domain.Entities.User; 3 4 using StoryEntity = ChapterX.Domain.Entities.Story; … … 40 41 DbSet<NeedApprovalEntity> NeedApprovals { get; } 41 42 Task<int> SaveChangesAsync(CancellationToken cancellationToken = default); 43 Task<IDbContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken = default); 42 44 } 43 45 } -
ChapterX.Application/Auth/RegisterHandler.cs
rdc383dd rd300631 1 using ChapterX.Application.Abstractions; 1 2 using ChapterX.Domain.Repositories; 2 3 using MediatR; … … 8 9 private readonly IUserRepository _userRepository; 9 10 private readonly IWriterRepository _writerRepository; 11 private readonly IApplicationDbContext _context; 10 12 11 public RegisterHandler(IUserRepository userRepository, IWriterRepository writerRepository )13 public RegisterHandler(IUserRepository userRepository, IWriterRepository writerRepository, IApplicationDbContext context) 12 14 { 13 15 _userRepository = userRepository; 14 16 _writerRepository = writerRepository; 17 _context = context; 15 18 } 16 19 … … 21 24 throw new InvalidOperationException("Email already in use."); 22 25 23 var user = new Domain.Entities.User 26 await using var transaction = await _context.BeginTransactionAsync(cancellationToken); 27 try 24 28 { 25 Username = request.Username, 26 Email = request.Email, 27 Name = request.Name, 28 Surname = request.Surname, 29 Password = BCrypt.Net.BCrypt.HashPassword(request.Password), 30 CreatedAt = DateTime.UtcNow, 31 UpdatedAt = DateTime.UtcNow 32 }; 29 var user = new Domain.Entities.User 30 { 31 Username = request.Username, 32 Email = request.Email, 33 Name = request.Name, 34 Surname = request.Surname, 35 Password = BCrypt.Net.BCrypt.HashPassword(request.Password), 36 CreatedAt = DateTime.UtcNow, 37 UpdatedAt = DateTime.UtcNow 38 }; 33 39 34 await _userRepository.AddAsync(user, cancellationToken);40 await _userRepository.AddAsync(user, cancellationToken); 35 41 36 var writer = new Domain.Entities.Writer { Id = user.Id };37 await _writerRepository.AddAsync(writer, cancellationToken);42 var writer = new Domain.Entities.Writer { Id = user.Id }; 43 await _writerRepository.AddAsync(writer, cancellationToken); 38 44 39 return new RegisterResponse(user.Id, user.Username, user.Email); 45 await transaction.CommitAsync(cancellationToken); 46 return new RegisterResponse(user.Id, user.Username, user.Email); 47 } 48 catch 49 { 50 await transaction.RollbackAsync(cancellationToken); 51 throw; 52 } 40 53 } 41 54 } -
ChapterX.Application/Story/Commands/AddHandler.cs
rdc383dd rd300631 1 using ChapterX.Application.Abstractions; 1 2 using ChapterX.Domain.Repositories; 2 3 using MediatR; … … 10 11 private readonly IGenreRepository _genreRepository; 11 12 private readonly IHasGenreRepository _hasGenreRepository; 13 private readonly IApplicationDbContext _context; 12 14 private readonly ILogger<AddHandler> _logger; 13 15 14 public AddHandler(IStoryRepository storyRepository, IGenreRepository genreRepository, IHasGenreRepository hasGenreRepository, I Logger<AddHandler> logger)16 public AddHandler(IStoryRepository storyRepository, IGenreRepository genreRepository, IHasGenreRepository hasGenreRepository, IApplicationDbContext context, ILogger<AddHandler> logger) 15 17 { 16 18 _storyRepository = storyRepository; 17 19 _genreRepository = genreRepository; 18 20 _hasGenreRepository = hasGenreRepository; 21 _context = context; 19 22 _logger = logger; 20 23 } … … 22 25 public async Task<AddResponse> Handle(AddRequest request, CancellationToken cancellationToken) 23 26 { 24 var story = new Domain.Entities.Story 27 await using var transaction = await _context.BeginTransactionAsync(cancellationToken); 28 try 25 29 { 26 MatureContent = request.MatureContent, 27 ShortDescription = request.ShortDescription, 28 Image = request.Image, 29 Content = request.Content, 30 UserId = request.UserId, 31 CreatedAt = DateTime.UtcNow, 32 UpdatedAt = DateTime.UtcNow 33 }; 30 var story = new Domain.Entities.Story 31 { 32 MatureContent = request.MatureContent, 33 ShortDescription = request.ShortDescription, 34 Image = request.Image, 35 Content = request.Content, 36 UserId = request.UserId, 37 CreatedAt = DateTime.UtcNow, 38 UpdatedAt = DateTime.UtcNow 39 }; 34 40 35 await _storyRepository.AddAsync(story, cancellationToken);41 await _storyRepository.AddAsync(story, cancellationToken); 36 42 37 foreach (var genreName in request.Genres ?? []) 43 foreach (var genreName in request.Genres ?? []) 44 { 45 var genre = await _genreRepository.GetByNameAsync(genreName, cancellationToken); 46 if (genre == null) continue; 47 await _hasGenreRepository.AddAsync(new Domain.Entities.HasGenre { StoryId = story.Id, GenreId = genre.Id }, cancellationToken); 48 } 49 50 await transaction.CommitAsync(cancellationToken); 51 return new AddResponse(story.Id); 52 } 53 catch 38 54 { 39 var genre = await _genreRepository.GetByNameAsync(genreName, cancellationToken); 40 if (genre == null) continue; 41 await _hasGenreRepository.AddAsync(new Domain.Entities.HasGenre { StoryId = story.Id, GenreId = genre.Id }, cancellationToken); 55 await transaction.RollbackAsync(cancellationToken); 56 throw; 42 57 } 43 44 return new AddResponse(story.Id);45 58 } 46 59 }
Note:
See TracChangeset
for help on using the changeset viewer.
