<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ProjectFood.Models;
using System.Diagnostics;

namespace ProjectFood.Controllers
{
    public class RecipesController : Controller
    {
        private readonly DataBaseContext _db = new DataBaseContext();

        // GET: Recipes
        public ActionResult Index()
        {
            if (User.Identity.IsAuthenticated)
            {
                Session["ScreenName"] = _db.Users.First(u => u.Username == User.Identity.Name).Name;
            }
           
            return View(_db.Recipes.Include(r => r.Ingredients).ToList());
        }

        // GET: Recipes/Details/5
        public ActionResult Details(int? id, int? numPersons)
        {
            if(User.Identity.IsAuthenticated) {
                ViewBag.ShoppingLists =
                    _db.Users.Include(s => s.ShoppingLists)
                        .First(u => u.Username == User.Identity.Name)
                        .ShoppingLists.ToList();
            } else {
                ViewBag.ShoppingLists = null;
            }

            if(id == null) {
                return RedirectToAction("index");
            }

            var recipe = _db.Recipes.Include(r => r.Ingredients).Single(x => x.ID == id);
            ViewBag.Author = _db.Users.First(u => u.Username == recipe.AuthorName);
            ViewBag.OriginalAuthor = _db.Users.SingleOrDefault(u => u.Username == recipe.OriginalAuthorName);
            if(recipe.Ingredients.Count > 0) {
                ViewBag.Recipe_Ingredient = _db.Recipe_Ingredient.Where(x => x.RecipeID == id).ToList();
            }

            // ?? means if null assign the right side, else the left side.
            ViewBag.numPersons = numPersons ?? 4;

            //calculate average score
            ViewBag.AverageScore = getScore(id);
            ViewBag.RatingCount = getRatingCount(id);

            return View(recipe);
        }

        private dynamic getRatingCount(int? id)
        {   
            int? count = _db.Ratings.Include(s => s.Score).Where(r => r.Recipe.ID == id).Select(s => s.Score).Count();
            if (count == null)
                return 0;
            else
                return count;
        }

        private decimal? getScore(int id)
        {
            decimal? score = _db.Ratings.Include(s => s.Score).Where(r => r.Recipe.ID == id).Select(s => s.Score).Average();
            if (score == null)
                return 0;
            else
                return score;
        }

        // GET: Recipes/Create
        public ActionResult Create()
        {
            if(User.Identity.IsAuthenticated) {
                return View();
            }
            return RedirectToAction("Register", "Account");
        }

        // POST: Recipes/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Title,OriginalAuthorName,AuthorName,Minutes,Instructions,Tags")] Recipe recipe)
        {
            _db.Recipes.Add(recipe);<commit_msg>nullable<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ProjectFood.Models;
using System.Diagnostics;

namespace ProjectFood.Controllers
{
    public class RecipesController : Controller
    {
        private readonly DataBaseContext _db = new DataBaseContext();

        // GET: Recipes
        public ActionResult Index()
        {
            if (User.Identity.IsAuthenticated)
            {
                Session["ScreenName"] = _db.Users.First(u => u.Username == User.Identity.Name).Name;
            }
           
            return View(_db.Recipes.Include(r => r.Ingredients).ToList());
        }

        // GET: Recipes/Details/5
        public ActionResult Details(int? id, int? numPersons)
        {
            if(User.Identity.IsAuthenticated) {
                ViewBag.ShoppingLists =
                    _db.Users.Include(s => s.ShoppingLists)
                        .First(u => u.Username == User.Identity.Name)
                        .ShoppingLists.ToList();
            } else {
                ViewBag.ShoppingLists = null;
            }

            if(id == null) {
                return RedirectToAction("index");
            }

            var recipe = _db.Recipes.Include(r => r.Ingredients).Single(x => x.ID == id);
            ViewBag.Author = _db.Users.First(u => u.Username == recipe.AuthorName);
            ViewBag.OriginalAuthor = _db.Users.SingleOrDefault(u => u.Username == recipe.OriginalAuthorName);
            if(recipe.Ingredients.Count > 0) {
                ViewBag.Recipe_Ingredient = _db.Recipe_Ingredient.Where(x => x.RecipeID == id).ToList();
            }

            // ?? means if null assign the right side, else the left side.
            ViewBag.numPersons = numPersons ?? 4;

            //calculate average score
            ViewBag.AverageScore = getScore(id);
            ViewBag.RatingCount = getRatingCount(id);

            return View(recipe);
        }

        private dynamic getRatingCount(int? id)
        {   
            int? count = _db.Ratings.Include(s => s.Score).Where(r => r.Recipe.ID == id).Select(s => s.Score).Count();
            if (count == null)
                return 0;
            else
                return count;
        }

        private decimal? getScore(int? id)
        {
            decimal? score = _db.Ratings.Include(s => s.Score).Where(r => r.Recipe.ID == id).Select(s => s.Score).Average();
            if (score == null)
                return 0;
            else
                return score;
        }

        // GET: Recipes/Create
        public ActionResult Create()
        {
            if(User.Identity.IsAuthenticated) {
                return View();
            }
            return RedirectToAction("Register", "Account");
        }

        // POST: Recipes/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Title,OriginalAuthorName,AuthorName,Minutes,Instructions,Tags")] Recipe recipe)
        {
            _db.Recipes.Add(recipe);
{'fraction_non_alphanumeric': 0.11212302179755151, 'fraction_numerical': 0.0032845625559868616, 'mean_word_length': 1.7624742268041238, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6199040', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1822, 'n_words': 383}