Question

These are my Entities: In this scenario, each user can make a Post. Each post can have a list of Items and each item can have a list of users that Tagged in it.

public class Post
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public string Body { get; set; }
    public int LikeCount { get; set; }
    public PostStatuses Status { get; set; }

    [ForeignKey("UserId")]
    public User User { get; set; }

    public List<PostItem> PostItems { get; set; }

    public Post()
    {
        PostItems = new List<PostItem>();
    }
}

public class PostItem
{
    public int Id { get; set; }
    public int PostId { get; set; }
    public long AttachmentId { get; set; }  // Refer to CDN file Id

    [ForeignKey("PostId")]
    public Post Post { get; set; }

    public List<PostItemTag> TaggedUsers { get; set; }
}

public class PostItemTag
{
    public int PostItemId { get; set; }
    public int TaggedUserId { get; set; }
    public int X { get; set; }
    public int Y { get; set; }

    [ForeignKey("PostItemId")]
    public PostItem PostItem { get; set; }

    [ForeignKey("TaggedUserId")]
    public User User { get; set; }
}

These are my DTOs:

public class CreatePostRequestDTO
{
    public int UserId { get; set; }
    public string Body { get; set; }
    public List<PostItemDTO> PostItems { get; set; }
}

public class PostItemDTO
{
    public long AttachmentId { get; set; }
    public List<PostItemTagDTO> TaggedUsers { get; set; }
}

public class PostItemTagDTO
{
    public int UserId { get; set; }
    public int X { get; set; }
    public int Y { get; set; }
}

And this is my API:

    public async Task<BaseResponseDTO<bool>> CreatePost(CreatePostRequestDTO createPostRequestDTO)
    {
        Post post = new Post()
        {
            Body = createPostRequestDTO.Body,
            Status = PostStatuses.Posted,
            UserId = createPostRequestDTO.UserId,
        };
        post.Mentions = createPostRequestDTO.Mentions.Select(x => new PostMention()
        {
            Post = post,
            UserId = x,
        }).ToList();

        post.PostItems = createPostRequestDTO.PostItems.Select(x => new PostItem()
        {
            AttachmentId = x.AttachmentId,
            Post = post,
            PostAttachmentType = x.PostAttachmentType,
            TaggedUseres = x.TaggedUseres.Select(c => new PostItemTag()
            {
                UserId = c.UserId,
                X = c.X,
                Y = c.Y,
                PostItemId = ? // My problem is here.
            }).ToList(),
        }).ToList();

        ...
    }

How can I solve it?

Edit: Some extra code removed.

Edit2: And also some extra descriptions removed.

1
0
8/27/2019 5:49:21 AM

Popular Answer

For model binding with Automapper, follow steps below:

  1. Install package AutoMapper and AutoMapper.Extensions.Microsoft.DependencyInjection
  2. Add services.AddAutoMapper(typeof(Startup)); to Startup.cs
  3. Add ModelProfile.cs like

    public class ModelProfile: Profile
    {
        public ModelProfile()
        {
            CreateMap<CreatePostRequestDTO, Post>();
            CreateMap<PostItemDTO, PostItem>();
            CreateMap<PostItemTagDTO, PostItemTag>();
        }
    }
    
  4. UseCase

    public class HomeController : Controller
    {
        private readonly IMapper _mapper;
        private readonly ApplicationDbContext _context;       
        public HomeController(IMapper mapper
            , ApplicationDbContext context)
        {
            _mapper = mapper;
            _context = context;
        }        
        public async Task<IActionResult> Index()
        {
            CreatePostRequestDTO createPostRequestDTO = new CreatePostRequestDTO {
                Body = "B1",
                UserId = 1,
                PostItems = new List<PostItemDTO> {
                    new PostItemDTO { AttachmentId = 1, TaggedUsers = new List<PostItemTagDTO>{
                        new PostItemTagDTO{ UserId = 1, X = 1, Y= 11  }
                    } }
                }
            };
            var post = _mapper.Map<Post>(createPostRequestDTO);
            await _context.AddAsync(post);
            await _context.SaveChangesAsync();            
            return View();
        }        
    }
    
1
7/18/2019 2:20:20 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow