public IHttpActionResult GetApp(Guid appId)
 {
     try
     {
         using (MususAppEntities entity = new MususAppEntities())
         {
             var          appMaster = entity.AppMasters.FirstOrDefault(x => x.Id == appId);
             AppMasterDto app       = new AppMasterDto();
             var          rating    = entity.Ratings.Where(x => x.AppId == appMaster.Id);
             if (rating != null && rating.Count() > 0)
             {
                 app.Rating = rating.Average(x => x.Point);
             }
             else
             {
                 app.Rating = 0;
             }
             var useRating = entity.Ratings.FirstOrDefault(x => x.AppId == appMaster.Id &&
                                                           x.Username.Contains(RequestContext.Principal.Identity.Name));
             if (useRating != null)
             {
                 app.UserRating = useRating.Point;
             }
             GetPostComment(entity, appMaster, app, RequestContext.Principal.Identity.Name.ToString());
             MapHostedAppObject(appMaster, app);
             return(Ok(app));
         }
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
 public IHttpActionResult GetAllApps(int page = 1, int size = 10)
 {
     try
     {
         using (MususAppEntities entity = new MususAppEntities())
         {
             List <AppMaster>    appmasters = entity.AppMasters.OrderBy(x => x.Documents).Skip(page - 1).Take(10).ToList();
             List <AppMasterDto> apps       = new List <AppMasterDto>();
             foreach (var master in appmasters)
             {
                 AppMasterDto dto = new AppMasterDto();
                 MapHostedAppObject(master, dto);
                 apps.Add(dto);
             }
             return(Ok(apps));
         }
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
 private void MapHostedAppObject(AppMaster master, AppMasterDto dto)
 {
     dto.AndriodSmartPhoneBuild = master.AndriodSmartPhoneBuild;
     dto.AndriodTabletBuild     = master.AndriodTabletBuild;
     dto.CategoryId             = master.CategoryId;
     dto.CreatedTime            = master.CreatedTime;
     dto.DeletedTime            = master.DeletedTime;
     dto.Description            = master.Description;
     dto.Icon              = master.Icon;
     dto.Id                = master.Id;
     dto.IpadBuild         = master.IpadBuild;
     dto.IpadPackageName   = master.IpadPackageName;
     dto.IphoneBuild       = master.IphoneBuild;
     dto.IphonePackageName = master.IphonePackageName;
     dto.IsDeleted         = master.IsDeleted;
     dto.Published         = master.Published;
     dto.Title             = master.Title;
     dto.Version           = master.Version;
     dto.Download          = master.Download ?? 0;
     if (master.Documents != null)
     {
         var documents = master.Documents.Split(new char[] { ';' });
         foreach (var doc in documents)
         {
             dto.Documents.Add(doc);
         }
     }
     if (master.ScreenShots != null)
     {
         var screenShots = master.ScreenShots.Split(new char[] { ';' });
         foreach (var doc in screenShots)
         {
             dto.ScreenShots.Add(doc);
         }
     }
 }
        private static void GetPostComment(MususAppEntities entity, AppMaster appMaster, AppMasterDto app, string userName)
        {
            var userPost = entity.Posts.FirstOrDefault(x => x.AppId == appMaster.Id && x.UserName == userName);

            if (userPost != null)
            {
                var userCR = new CommentReview()
                {
                    Comment     = userPost.Txt,
                    CommentDate = userPost.CreateTime,
                    Username    = userName,
                    Id          = userPost.Id
                };
                var userComment = entity.Comments.FirstOrDefault(x => x.PostId == userPost.Id);
                if (userComment != null)
                {
                    userCR.Review         = userComment.Msg;
                    userCR.ReviewDate     = userComment.CreateTime;
                    userCR.ReviewUsername = userComment.UserName;
                }
                app.UserComment = userCR;
            }
            if (app.UserComment == null)
            {
                var posts = entity.Posts.Where(x => x.AppId == appMaster.Id).OrderByDescending(x => x.CreateTime);
                if (posts != null)
                {
                    foreach (var post in posts)
                    {
                        var review = entity.Comments.FirstOrDefault(x => x.PostId == post.Id);
                        if (review != null)
                        {
                            app.CommentReviews.Add(new CommentReview()
                            {
                                Id             = post.Id,
                                Comment        = post.Txt,
                                CommentDate    = post.CreateTime,
                                Review         = review.Msg,
                                ReviewDate     = review.CreateTime,
                                ReviewUsername = review.UserName,
                                Username       = post.UserName
                            });
                        }
                        else
                        {
                            app.CommentReviews.Add(new CommentReview()
                            {
                                Id          = post.Id,
                                Comment     = post.Txt,
                                CommentDate = post.CreateTime
                            });
                        }
                    }
                }
            }
            else
            {
                var posts = entity.Posts.Where(x => x.AppId == appMaster.Id).OrderByDescending(x => x.CreateTime);
                if (posts != null)
                {
                    foreach (var post in posts)
                    {
                        var review = entity.Comments.FirstOrDefault(x => x.PostId == post.Id);
                        if (review != null)
                        {
                            app.CommentReviews.Add(new CommentReview()
                            {
                                Id             = post.Id,
                                Comment        = post.Txt,
                                CommentDate    = post.CreateTime,
                                Review         = review.Msg,
                                ReviewDate     = review.CreateTime,
                                ReviewUsername = review.UserName,
                                Username       = post.UserName
                            });
                        }
                        else
                        {
                            app.CommentReviews.Add(new CommentReview()
                            {
                                Id          = post.Id,
                                Comment     = post.Txt,
                                CommentDate = post.CreateTime
                            });
                        }
                    }
                }
            }
        }