public Post EntityToDtoPostCleanUp(DataAccess.Database.Entities.Objects.Post db, int postId)
{
// clean up the data fetched from db to avoid stack overflow
if (db.Communities != null)
{
foreach (var community in db.Communities)
{
community.Leader = null;
community.Members = null;
community.Posts = null;
}
}
if (db.User != null)
{
db.User.CreatedCommunities = null;
db.User.JoinedCommunities = null;
db.User.Posts = null;
}
var postContents = new List<PostContent>();
if (db.PostContents != null)
{
foreach (var content in db.PostContents)
{
content.Post = null;
if (content.Media != null)
{
content.Media.MediaPath = null;
content.Media.ThumbnailPath = null;
}
postContents.Add(PostContentMapper.ToDto(content));
}
}
else
{
var dbContents = _postContentRepository.Find(a => a.PostId == postId, true).ToList();
dbContents.ForEach(a =>
{
a.Post = null;
if (a.Media != null)
{
a.Media.MediaPath = null;
a.Media.ThumbnailPath = null;
}
postContents.Add(PostContentMapper.ToDto(a));
});
}
var post = PostMapper.ToDto(db);
if (post.User != null)
{
if (post.User.PictureId != null)
post.User.Picture = MediaMapper.ToDto(_mediaRepository.Find(b => b.MediaId == (int)post.User.PictureId, false).FirstOrDefault());
if (post.User.BackgroundId != null)
post.User.Background = MediaMapper.ToDto(_mediaRepository.Find(b => b.MediaId == (int)post.User.BackgroundId, false).FirstOrDefault());
}
post.PostContents = postContents;
return post;
}
}