示例#1
0
        public ActionResult Create(int?replyId)
        {
            YakViewModel yvm = null;

            if (replyId.HasValue)
            {
                yvm = getYakViewModel(replyId.Value);
            }

            return(View(yvm));
        }
示例#2
0
        /*
         * private static double ToRad(double num)
         * {
         *
         *  return
         *  num * Math.PI / 180;
         * }
         *
         * public double DistanceBetweenPoints(double lat1, double long1, double lat2, double long2)
         * {
         *  const int r = 6371;
         *  // radius of earth in km
         *
         *
         *  // Convert to Radians
         *
         *  lat1 = ToRad(lat1);
         *
         *  long1 = ToRad(lat2);
         *
         *  lat2 = ToRad(long1);
         *
         *  long2 = ToRad(long2);
         *
         *
         *  // Spherical Law of Cosines
         *
         *  return
         *  Math.Acos(
         *
         *  Math.Sin(lat1) * Math.Sin(lat2) +
         *
         *  Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(long2 - long1)
         *
         *  ) * r;
         * }*/


        public YakViewModel getYakViewModel(int ID)
        {
            var yak = db.Yaks.Where(y => y.Id == ID).First();

            int likeCount = db.Likes.Count(l => l.YakId == ID);

            YakViewModel yvm = new YakViewModel()
            {
                Yak = yak, LikeCount = likeCount
            };

            return(yvm);
        }
示例#3
0
        /**************** Reports ViewModel*******************/

        /*
         * private List<YakViewModel> GenerateReportViewModels()
         * {
         *  //create a new list of reports
         *  var yakViewModels = new List<YakViewModel>();
         *
         *  var reportCounts = from r in db.Reports
         *
         *                   group r by r.YakId into grouping
         *                   select new
         *                   {
         *                       YakId = grouping.Key,
         *                       Count = grouping.Count()
         *                   };
         *
         *  //refer to this connection to the database
         *  var yaks = db.Yaks.ToList();
         *
         *  foreach (var y in yaks)
         *  {
         *      var yvm = new YakViewModel() { Yak = y };
         *      //ReportCounts
         *      if (reportCounts.Where(rc => rc.YakId == y.Id).Count() > 0)
         *      {
         *          yvm.ReportCount = reportCounts.Where(rc => rc.YakId == y.Id).First().Count;
         *      }
         *      else
         *      {
         *          yvm.ReportCount = 0;
         *      }
         *      yakViewModels.Add(yvm);
         *  }
         *
         *  return yakViewModels;
         * }
         */
        /*public List<YakViewModel> getLikeCount()
         * {
         *  var yakViewModels = new List<YakViewModel>();
         *
         *  var likeCounts =  from c in db.Likes
         *                  group c by c.YakId into grouping
         *                  select new
         *                  {
         *                      LikeId = grouping.Key,
         *                      Count = grouping.Count()
         *                  };
         *
         *
         *
         *
         * return likeCounts;
         * }*/

        //get like view models function

        /*** This is what runs when INDEX is called ****/

        public List <YakViewModel> getYakViewModel(/*double userLat, double userLong*/)

        {
            var yakViewModels = new List <YakViewModel>();

            var yaks = db.Yaks.ToList();

            //Get Like Count
            var likeCounts = from c in db.Likes
                             group c by c.YakId into grouping
                             select new
            {
                LikeId = grouping.Key,
                Count  = grouping.Count()
            };

            //Get Report Count
            var reportCounts = from r in db.Reports

                               group r by r.YakId into grouping
                               select new
            {
                YakId = grouping.Key,
                Count = grouping.Count()
            };



            foreach (var y in yaks)
            {
                var yvm = new YakViewModel()
                {
                    Yak = y
                };

                if (likeCounts.Where(lc => lc.LikeId == y.Id).Count() > 0)
                {
                    yvm.LikeCount = likeCounts.Where(yc => yc.LikeId == y.Id).First().Count;
                }

                //if the like count was less than 0 set it to 0 then add one
                else
                {
                    yvm.LikeCount = 0;
                }

                if (reportCounts.Where(rc => rc.YakId == y.Id).Count() > 0)
                {
                    yvm.ReportCount = reportCounts.Where(rc => rc.YakId == y.Id).First().Count;
                }

                //if the like count was less than 0 set it to 0 then add one
                else
                {
                    yvm.ReportCount = 0;
                }

                //Compute Distance
                // yvm.DistanceAway = DistanceBetweenPoints(userLat, userLong, y.Latitude, y.Longitude);
                yakViewModels.Add(yvm);
            }

            return(yakViewModels);
        }