Услуги
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using MediatR;
|
||||
using Nashel.BuildingBlocks.Domain;
|
||||
using Nashel.Modules.Catalog.Domain.Aggregates;
|
||||
using Nashel.Modules.Catalog.Domain.Repositories;
|
||||
|
||||
@@ -8,17 +9,17 @@ namespace Nashel.Modules.Catalog.Application.Commands;
|
||||
/// <summary>
|
||||
/// Команда создания категории.
|
||||
/// </summary>
|
||||
public record CreateCategoryCommand : IRequest<Guid>
|
||||
public record CreateCategoryCommand : IRequest<Result<Guid>>
|
||||
{
|
||||
/// <summary>
|
||||
/// Название категории.
|
||||
/// </summary>
|
||||
public string Title { get; init; }
|
||||
public string Name { get; init; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// URL-friendly идентификатор (слаг).
|
||||
/// </summary>
|
||||
public string Slug { get; init; }
|
||||
public string Slug { get; init; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// ID родительской категории (null для корневых).
|
||||
@@ -30,9 +31,9 @@ public record CreateCategoryCommand : IRequest<Guid>
|
||||
/// </summary>
|
||||
public JsonDocument? AttributeSchema { get; init; }
|
||||
|
||||
public CreateCategoryCommand(string title, string slug, Guid? parentId, JsonDocument? attributeSchema)
|
||||
public CreateCategoryCommand(string name, string slug, Guid? parentId, JsonDocument? attributeSchema)
|
||||
{
|
||||
Title = title;
|
||||
Name = name;
|
||||
Slug = slug;
|
||||
ParentId = parentId;
|
||||
AttributeSchema = attributeSchema;
|
||||
@@ -41,7 +42,7 @@ public record CreateCategoryCommand : IRequest<Guid>
|
||||
public CreateCategoryCommand() { } // For deserialization
|
||||
}
|
||||
|
||||
public class CreateCategoryCommandHandler : IRequestHandler<CreateCategoryCommand, Guid>
|
||||
public class CreateCategoryCommandHandler : IRequestHandler<CreateCategoryCommand, Result<Guid>>
|
||||
{
|
||||
private readonly ICategoryRepository _repository;
|
||||
|
||||
@@ -50,19 +51,19 @@ public class CreateCategoryCommandHandler : IRequestHandler<CreateCategoryComman
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
public async Task<Guid> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
|
||||
public async Task<Result<Guid>> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (request.ParentId.HasValue)
|
||||
{
|
||||
var parentCategory = await _repository.GetByIdAsync(request.ParentId.Value, cancellationToken);
|
||||
if (parentCategory == null)
|
||||
{
|
||||
throw new ApplicationException($"Родительская категория с ID {request.ParentId} не найдена.");
|
||||
return Result<Guid>.Failure($"Родительская категория с ID {request.ParentId} не найдена.");
|
||||
}
|
||||
}
|
||||
|
||||
var category = new Category(
|
||||
request.Title,
|
||||
request.Name,
|
||||
request.Slug,
|
||||
request.ParentId,
|
||||
request.AttributeSchema
|
||||
@@ -70,6 +71,6 @@ public class CreateCategoryCommandHandler : IRequestHandler<CreateCategoryComman
|
||||
|
||||
await _repository.AddAsync(category, cancellationToken);
|
||||
|
||||
return category.Id;
|
||||
return Result<Guid>.Success(category.Id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
using System.Text.Json;
|
||||
using MediatR;
|
||||
using MediatR;
|
||||
using Nashel.BuildingBlocks.Application.Abstractions;
|
||||
using Nashel.BuildingBlocks.Domain;
|
||||
using Nashel.Modules.Catalog.Domain.Aggregates;
|
||||
using Nashel.Modules.Catalog.Domain.Repositories;
|
||||
using Nashel.Modules.Catalog.Domain.ValueObjects;
|
||||
@@ -9,16 +12,8 @@ namespace Nashel.Modules.Catalog.Application.Commands;
|
||||
/// <summary>
|
||||
/// Команда создания услуги (оффера).
|
||||
/// </summary>
|
||||
public record CreateOfferCommand : IRequest<Guid>
|
||||
public record CreateOfferCommand : IRequest<Result<Guid>>
|
||||
{
|
||||
/// <summary>
|
||||
/// ID владельца (пользователя/исполнителя).
|
||||
/// </summary>
|
||||
public Guid OwnerId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// ID категории услуги.
|
||||
/// </summary>
|
||||
public Guid CategoryId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
@@ -41,9 +36,8 @@ public record CreateOfferCommand : IRequest<Guid>
|
||||
/// </summary>
|
||||
public JsonDocument? Attributes { get; init; }
|
||||
|
||||
public CreateOfferCommand(Guid ownerId, Guid categoryId, string title, string description, Price price, JsonDocument? attributes)
|
||||
public CreateOfferCommand(Guid categoryId, string title, string description, Price price, JsonDocument? attributes)
|
||||
{
|
||||
OwnerId = ownerId;
|
||||
CategoryId = categoryId;
|
||||
Title = title;
|
||||
Description = description;
|
||||
@@ -54,19 +48,24 @@ public record CreateOfferCommand : IRequest<Guid>
|
||||
public CreateOfferCommand() { }
|
||||
}
|
||||
|
||||
public class CreateOfferCommandHandler : IRequestHandler<CreateOfferCommand, Guid>
|
||||
public class CreateOfferCommandHandler : IRequestHandler<CreateOfferCommand, Result<Guid>>
|
||||
{
|
||||
private readonly IOfferRepository _repository;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
|
||||
public CreateOfferCommandHandler(IOfferRepository repository)
|
||||
public CreateOfferCommandHandler(IOfferRepository repository, ICurrentUserService currentUserService)
|
||||
{
|
||||
_repository = repository;
|
||||
_currentUserService = currentUserService;
|
||||
}
|
||||
|
||||
public async Task<Guid> Handle(CreateOfferCommand request, CancellationToken cancellationToken)
|
||||
public async Task<Result<Guid>> Handle(CreateOfferCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var userId = _currentUserService.UserId;
|
||||
if (userId == null) return Result<Guid>.Failure("Неавторизован");
|
||||
|
||||
var offer = new Offer(
|
||||
request.OwnerId,
|
||||
userId.Value,
|
||||
request.CategoryId,
|
||||
request.Title,
|
||||
request.Description,
|
||||
@@ -76,6 +75,6 @@ public class CreateOfferCommandHandler : IRequestHandler<CreateOfferCommand, Gui
|
||||
|
||||
await _repository.AddAsync(offer, cancellationToken);
|
||||
|
||||
return offer.Id;
|
||||
return Result<Guid>.Success(offer.Id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user