<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using KompetansetorgetServer.Models;
using Microsoft.Ajax.Utilities;

namespace KompetansetorgetServer.Controllers.Api
{
    public class JobsController : ApiController
    {
        private KompetansetorgetServerContext db = new KompetansetorgetServerContext();

        // [HttpGet, Route("api/jobs")]
        // Activates the correct method based on query string parameters.
        // At the moment you can not use a combination of different strings (other then orderBy and sortBy)
        public IQueryable Get(string types = "", [FromUri] string[] studyGroups = null, string locations = "",
            string title = "", string orderBy = "", string sortBy = "")
        {
            if (!types.IsNullOrWhiteSpace())
            {
                IQueryable<Job> jobs = GetJobsByType(types);
                if (orderBy.Equals("desc") || orderBy.Equals("asc"))
                {
                   return GetJobsSorted(jobs, orderBy, sortBy);
                }
                return GetJobsSerialized(jobs);

            }

            if (studyGroups.Length != 0)
            {
                int i = studyGroups.Length;
                IQueryable<Job> jobs = GetJobsByStudy(studyGroups);
                if (orderBy.Equals("desc") || orderBy.Equals("asc"))
                    {
                    return GetJobsSorted(jobs, orderBy, sortBy);
                }
                return GetJobsSerialized(jobs);
            }

            if (!locations.IsNullOrWhiteSpace())
            {
                IQueryable<Job> jobs = GetJobsByLocation(locations);
                if (orderBy.Equals("desc") || orderBy.Equals("asc"))
                {
                    return GetJobsSorted(jobs, orderBy, sortBy);
                }
                return GetJobsSerialized(jobs);
            }

            if (!title.IsNullOrWhiteSpace())
            {
                IQueryable<Job> jobs = GetJobsByTitle(title);
                if (orderBy.Equals("desc") || orderBy.Equals("asc"))
                {
                    return GetJobsSorted(jobs, orderBy, sortBy);
                }
                return GetJobsSerialized(jobs); 
            }

            if (orderBy.Equals("desc") || orderBy.Equals("asc"))
                {
                return GetJobsSorted(orderBy, sortBy);
            }

            return GetJobs();
        }

        // GET: api/Jobs
        //public IQueryable<Job> GetJobs()
        /// <summary>
        /// This method is called if no query strings are presented
        /// </summary>
        /// <returns></returns>
        private IQueryable GetJobs()
        {
            var jobs = from job in db.jobs select job;
            return GetJobsSerialized(jobs);
        }

        // GET: api/Jobs/5
        // Example: /api/jobs/2c70edff-edbe-4d6d-8e79-10a47f330feb
        [HttpGet, Route("api/jobs/{id}")]
        [ResponseType(typeof(Job))]
        public async Task<IHttpActionResult> GetJob(string id)
        {
            Job job = await db.jobs.FindAsync(id);
            if (job == null)
            {
                return NotFound();
            }

            //return Ok(job);
            return Ok(new
            {
                job.uuid,
                job.title,
                job.description,
                job.webpage,
                job.linkedInProfile,
                job.expiryDate,
                job.stepsToApply,
                job.created,
                job.published,
                job.modified,
                companies = job.companies.Select(c => new { c.id }),
                locations = job.locations.Select(l => new { l.id }),
                jobTypes = job.jobTypes.Select(jt => new { jt.id }),
                studyGroups = job.studyGroups.Select(st => new { st.id })                          
        });
        }

        /// <summary>
        /// Lists all jobs that contains that spesific Location. 
        /// GET: api/jobs?locations=vestagder
        /// GET: api/jobs?locations=austagder<commit_msg>Fix contacts is now an underobject of api/jobs<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using KompetansetorgetServer.Models;
using Microsoft.Ajax.Utilities;

namespace KompetansetorgetServer.Controllers.Api
{
    public class JobsController : ApiController
    {
        private KompetansetorgetServerContext db = new KompetansetorgetServerContext();

        // [HttpGet, Route("api/jobs")]
        // Activates the correct method based on query string parameters.
        // At the moment you can not use a combination of different strings (other then orderBy and sortBy)
        public IQueryable Get(string types = "", [FromUri] string[] studyGroups = null, string locations = "",
            string title = "", string orderBy = "", string sortBy = "")
        {
            if (!types.IsNullOrWhiteSpace())
            {
                IQueryable<Job> jobs = GetJobsByType(types);
                if (orderBy.Equals("desc") || orderBy.Equals("asc"))
                {
                   return GetJobsSorted(jobs, orderBy, sortBy);
                }
                return GetJobsSerialized(jobs);

            }

            if (studyGroups.Length != 0)
            {
                int i = studyGroups.Length;
                IQueryable<Job> jobs = GetJobsByStudy(studyGroups);
                if (orderBy.Equals("desc") || orderBy.Equals("asc"))
                    {
                    return GetJobsSorted(jobs, orderBy, sortBy);
                }
                return GetJobsSerialized(jobs);
            }

            if (!locations.IsNullOrWhiteSpace())
            {
                IQueryable<Job> jobs = GetJobsByLocation(locations);
                if (orderBy.Equals("desc") || orderBy.Equals("asc"))
                {
                    return GetJobsSorted(jobs, orderBy, sortBy);
                }
                return GetJobsSerialized(jobs);
            }

            if (!title.IsNullOrWhiteSpace())
            {
                IQueryable<Job> jobs = GetJobsByTitle(title);
                if (orderBy.Equals("desc") || orderBy.Equals("asc"))
                {
                    return GetJobsSorted(jobs, orderBy, sortBy);
                }
                return GetJobsSerialized(jobs); 
            }

            if (orderBy.Equals("desc") || orderBy.Equals("asc"))
                {
                return GetJobsSorted(orderBy, sortBy);
            }

            return GetJobs();
        }

        // GET: api/Jobs
        //public IQueryable<Job> GetJobs()
        /// <summary>
        /// This method is called if no query strings are presented
        /// </summary>
        /// <returns></returns>
        private IQueryable GetJobs()
        {
            var jobs = from job in db.jobs select job;
            return GetJobsSerialized(jobs);
        }

        // GET: api/Jobs/5
        // Example: /api/jobs/2c70edff-edbe-4d6d-8e79-10a47f330feb
        [HttpGet, Route("api/jobs/{id}")]
        [ResponseType(typeof(Job))]
        public async Task<IHttpActionResult> GetJob(string id)
        {
            Job job = await db.jobs.FindAsync(id);
            if (job == null)
            {
                return NotFound();
            }

            //return Ok(job);
            return Ok(new
            {
                job.uuid,
                job.title,
                job.description,
                job.webpage,
                job.linkedInProfile,
                job.expiryDate,
                job.stepsToApply,
                job.created,
                job.published,
                job.modified,
                companies = job.companies.Select(c => new { c.id }),
                contacts = job.contacts.Select(c => new { c.id }),
                locations = job.locations.Select(l => new { l.id }),
                jobTypes = job.jobTypes.Select(jt => new { jt.id }),
                studyGroups = job.studyGroups.Select(st => new { st.id })                          
        });
        }

        /// <summary>
        /// Lists all jobs that contains that spesific Location. 
        /// GET: api/jobs?locations=vestagder
        /// GET: api/jobs?locations=austagder
{'fraction_non_alphanumeric': 0.10743801652892562, 'fraction_numerical': 0.0039026629935720843, 'mean_word_length': 1.51239907727797, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 34, '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': '4736798', 'n_tokens_mistral': 2450, 'n_tokens_neox': 2281, 'n_words': 498}