<commit_before>﻿namespace Interapp.Web.Areas.Student.Controllers
{
    using AutoMapper;
    using AutoMapper.QueryableExtensions;
    using Microsoft.AspNet.Identity;
    using Models.ApplicationsViewModels;
    using Services.Contracts;
    using System.Web.Mvc;

    [Authorize(Roles = "Student")]
    public class ApplicationsController : Controller
    {
        private IApplicationsService applications;
        private IStudentInfosService studentInfos;
        private IMajorsService majors;

        public ApplicationsController(IApplicationsService applications, IStudentInfosService studentInfos, IMajorsService majors)
        {
            this.applications = applications;
            this.studentInfos = studentInfos;
            this.majors = majors;
        }

        [HttpGet]
        public ActionResult All()
        {
            var studentId = this.User.Identity.GetUserId();
            var model = this.applications
                .AllByStudent(studentId)
                .ProjectTo<ApplicationViewModel>();

            return View(model);
        }

        [HttpGet]
        public ActionResult Details(int id)
        {
            var application = this.applications
                .GetById(id);
            var studentId = this.User.Identity.GetUserId();

            if (application.StudentId != studentId)
            {
                return this.View();
            }

            var model = Mapper.Map<ApplicationDetailsViewModel>(application);

            return this.View(model);
        }

        [HttpPost]
        public ActionResult Submit(ApplicationInputViewModel model)
        {
            var studentId = this.User.Identity.GetUserId();
            var eligiblity = this.studentInfos.IsEligibleToApply(studentId, model.UniversityId);

            if (!eligiblity.IsEligible)
            {
                this.ModelState.AddModelError("Eligiblity", eligiblity.Message);
            }
            else
            {
                var major = this.majors.GetById(model.MajorId);
                
                if(major == null)
                {
                    this.ModelState.AddModelError("Major", "There is no such major.");
                }
            }

            if (this.ModelState.IsValid)
            {<commit_msg>Added check to make sure student is not applying twice<commit_after>﻿namespace Interapp.Web.Areas.Student.Controllers
{
    using AutoMapper;
    using AutoMapper.QueryableExtensions;
    using Microsoft.AspNet.Identity;
    using Models.ApplicationsViewModels;
    using Services.Contracts;
    using System.Linq;
    using System.Web.Mvc;

    [Authorize(Roles = "Student")]
    public class ApplicationsController : Controller
    {
        private IApplicationsService applications;
        private IStudentInfosService studentInfos;
        private IMajorsService majors;

        public ApplicationsController(IApplicationsService applications, IStudentInfosService studentInfos, IMajorsService majors)
        {
            this.applications = applications;
            this.studentInfos = studentInfos;
            this.majors = majors;
        }

        [HttpGet]
        public ActionResult All()
        {
            var studentId = this.User.Identity.GetUserId();
            var model = this.applications
                .AllByStudent(studentId)
                .ProjectTo<ApplicationViewModel>();

            return View(model);
        }

        [HttpGet]
        public ActionResult Details(int id)
        {
            var application = this.applications
                .GetById(id);
            var studentId = this.User.Identity.GetUserId();

            if (application.StudentId != studentId)
            {
                return this.View();
            }

            var model = Mapper.Map<ApplicationDetailsViewModel>(application);

            return this.View(model);
        }

        [HttpPost]
        public ActionResult Submit(ApplicationInputViewModel model)
        {
            var studentId = this.User.Identity.GetUserId();

            var hasApplied = this.applications
                .All()
                .Where(a => a.UniversityId == model.UniversityId && a.StudentId == studentId)
                .Any();

            if (hasApplied)
            {
                this.ModelState.AddModelError("Applied", "You have already applied");
            }

            var eligiblity = this.studentInfos.IsEligibleToApply(studentId, model.UniversityId);

            if (!eligiblity.IsEligible)
            {
                this.ModelState.AddModelError("Eligiblity", eligiblity.Message);
            }
            else
            {
                var major = this.majors.GetById(model.MajorId);
                
                if(major == null)
                {
                    this.ModelState.AddModelError("Major", "There is no such major.");
                }
            }

            if (this.ModelState.IsValid)
            {
{'fraction_non_alphanumeric': 0.081374321880651, 'fraction_numerical': 0.0, 'mean_word_length': 1.7366684991753711, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '3837545', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1210, 'n_words': 244}