@lucky4

Как добавить новую запись с вложенностью?

Мне нужно сделать относительно простой функционал для добавления товара в список товара.

Есть сущности DTO:
public class WishlistItemDto
    {
        public int Id { get; set; }

        public string CustomerId { get; set; }

        public ProductDetailsDtoWithPrimaryImage ProductDetails { get; set; }

        public int Quantity { get; set; }
    }

public class WishListItemCreationDto
    {
        public string CustomerId { get; set; }

        public int ProductDetailId { get; set; }

        public int Quantity { get; set; }
    }


Возникла проблема с добавлением товара в список. Вот как выглядит код:
//Controller
        [HttpPost]
        public async Task<IActionResult> Add(WishListItemCreationDto wishListItemDto)
        {
            var itemAdd = _mapper.Map<WishlistItemDto>(wishListItemDto);
            var itemCreated = await _wishListItemService.AddAsync(itemAdd);

            return CreatedAtAction(nameof(GetId), new { id = itemCreated.Id }, wishListItemDto);
        }

        //Service
        public async Task<WishlistItemDto> AddAsync(WishlistItemDto item)
        {
            var entity = _mapper.Map<WishlistItem>(item);
            var entityDetails = await _productDetailsRepository.GetById(item.ProductDetails.Id);
            entity.ProductDetails = entityDetails;
            await _wishListItemRepository.AddAsync(entity);

            return _mapper.Map<WishlistItemDto>(entity);
        }


Переменная entityDetails(в сервисе) всегда null. И я не могу понять как мне передать данные(запрос на добавления товара)
  • Вопрос задан
  • 20 просмотров
Пригласить эксперта
Ответы на вопрос 1
yarosroman
@yarosroman
C# the best
Очевидно, мапер не отражает вам вложенный объект.
itemAdd.ProductDetails = wishListItemDto.ProductDetails;
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы