public ActionResult Create(BatchRating rating)
        {
            if (ModelState.IsValid)
            {
                rating.UserId = ControllerUtils.getCurrentUserId(User);

                db.BatchRatings.Add(rating);
                db.SaveChanges();
                return RedirectToAction("Details", "Batch", new { id = rating.BatchId });
            }

            return View(rating);
        }
Пример #2
0
        public static BatchRating createBatchRating(Batch batch, UserProfile user, int rating, string comment)
        {
            BrewersBuddyContext db = new BrewersBuddyContext();

            BatchRating batchRating = new BatchRating();
            batchRating.Batch = batch;
            batchRating.User = user;
            batchRating.Rating = rating;
            batchRating.Comment = comment;

            db.BatchRatings.Add(batchRating);

            db.SaveChanges();

            return batchRating;
        }
        //
        // GET: /BatchRating/Create
        public ActionResult Create(int batchId = 0)
        {
            int currentUserId = ControllerUtils.getCurrentUserId(User);
            Batch batch = db.Batches.Find(batchId);

            BatchRating previousRating = db.BatchRatings
                .Where(r => r.BatchId == batchId && r.UserId == currentUserId)
                .FirstOrDefault();

            if (previousRating != null)
            {
                // You can only rate a batch once
                // TODO - Do something nicer here... this is kind of jarring
                // and no explanation is given in the UI as to why a user can't rate
                return RedirectToAction("Details", "Batch", new { id = batchId });
            }

            if (batch == null)
            {
                // I'm not sure if this makes sense here, but keeping
                // it for the time being - S. Platz
                HttpNotFound();
            }

            BatchRating rating = new BatchRating()
            {
                BatchId = batchId,
                Batch = batch
            };

            // Create a list from 0 - 100 for all possible rating values
            ViewBag.Ratings = Enumerable.Range(0, 101)
                .Select(num => new SelectListItem()
                {
                    Text = num.ToString(),
                    Value = num.ToString()
                });

            return View(rating);
        }