public ActionResult Index()
        {
            var           repo    = new BenchRepo(context);
            IList <Bench> benches = repo.GetAllBenches();
            var           json    = new JavaScriptSerializer().Serialize(benches);

            return(View("Index", benches));
        }
        public ActionResult Create(CreateBench formModel)
        {
            var repo     = new BenchRepo(context);
            var userRepo = new UserRepo(context);

            try
            {
                User user  = userRepo.GetByEmail(User.Identity.Name);
                var  bench = new Bench(formModel.Description, formModel.NumSeats, formModel.Latitude, formModel.Longitude, user);
                repo.Insert(bench);
                return(RedirectToAction("Index"));
            }
            catch (DbUpdateException ex)
            {
                //HandleDbUpdateException(ex);
            }
            return(View("Create", formModel));
        }
        public ActionResult Create(CreateReview formModel, int Id)
        {
            var email = User.Identity.Name;

            var revRepo  = new ReviewsRepo(context);
            var benRepo  = new BenchRepo(context);
            var userRepo = new UserRepo(context);

            try
            {
                Bench bench  = benRepo.GetById(Id);
                User  user   = userRepo.GetByEmail(email);
                var   review = new Review(formModel.Rating, formModel.Comment, bench, user);
                review.CreatedOn = DateTime.UtcNow;
                revRepo.Insert(review);
                return(RedirectToAction("Index", "Bench"));
            }
            catch (DbUpdateException ex)
            {
                //HandleDbUpdateException(ex);
            }

            return(View("Create", formModel));
        }
        public ActionResult Details(int benchId)
        {
            var formModel  = new CreateBench();
            var benchRepo  = new BenchRepo(context);
            var reviewRepo = new ReviewsRepo(context);
            var userRepo   = new UserRepo(context);

            Bench bench = benchRepo.GetById(benchId);
            User  user  = userRepo.GetById(bench.UserId);


            string phrase = user.Name;

            string[]      names    = phrase.Split(' ');
            string        lastName = names[1];
            StringBuilder uTag     = new StringBuilder(names[0]);

            uTag.Append(" " + lastName[0]);
            formModel.userTag = uTag.ToString();

            //string name = user.Name;
            //StringBuilder firstName = new StringBuilder();
            //StringBuilder lastInit = new StringBuilder();
            //bool enterLast = false;
            //for(int i = 0; i<name.Length; i++)
            //{
            //    if (name[i]==' ')
            //    {
            //        firstName.Append(' ');
            //    }
            //    else if(name[i]!=' ')
            //    {
            //        firstName.Append(name[i]);
            //    }
            //}
            formModel.Description = bench.Description;
            formModel.NumSeats    = bench.NumSeats;
            formModel.Latitude    = bench.Latitude;
            formModel.Longitude   = bench.Longitude;
            IList <Review> allReviews = reviewRepo.GetAllReviewsById(benchId);

            if (allReviews.Count == 0)
            {
                formModel.AverageRating = null;
            }
            else
            {
                float sum = 0f;
                foreach (Review rev in allReviews)
                {
                    sum += rev.Rating;
                }
                sum /= allReviews.Count;
                formModel.AverageRating = sum;
            }
            string        desc = formModel.Description;
            StringBuilder sb   = new StringBuilder();

            int wordCount = 0;

            for (int i = 0; i < desc.Length; i++)
            {
                sb.Append(desc[i]);
                if (desc[i] == ' ')
                {
                    wordCount++;
                }
                if (wordCount == 10)
                {
                    sb.Append("...");
                    break;
                }
            }
            formModel.Description = sb.ToString();

            return(View(formModel));
        }
 public BenchAPIController()
 {
     context = new Context();
     bRepo   = new BenchRepo(context);
 }