<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.OData;
using System.Web.OData.Query;
using Core.ApplicationServices;
using Core.ApplicationServices.Interfaces;
using Core.DomainModel;
using Core.DomainServices;
using Core.DomainServices.RoutingClasses;
using Infrastructure.DataAccess;
using Newtonsoft.Json.Schema;
using Core.ApplicationServices.Logger;

namespace OS2Indberetning.Controllers
{
    public class PersonController : BaseController<Person>
    {
        private IPersonService _person;
        private readonly IGenericRepository<Employment> _employmentRepo = new GenericRepository<Employment>(new DataContext());
        private readonly IGenericRepository<LicensePlate> _licensePlateRepo = new GenericRepository<LicensePlate>(new DataContext());
        private readonly IGenericRepository<Substitute> _substituteRepo;
        private readonly IGenericRepository<AppLogin> _appLoginRepo;
        private readonly ILogger _logger;

        public PersonController(IGenericRepository<Person> repo, IPersonService personService, IGenericRepository<Employment> employmentRepo, IGenericRepository<LicensePlate> licensePlateRepo, IGenericRepository<Substitute> substituteRepo, IGenericRepository<AppLogin> appLoginRepo, ILogger log)
            : base(repo, repo)
        {
            _person = personService;
            _employmentRepo = employmentRepo;
            _licensePlateRepo = licensePlateRepo;
            _substituteRepo = substituteRepo;
            _appLoginRepo = appLoginRepo;
            _logger = log;
        }

        // GET: odata/Person
        /// <summary>
        /// GET API endpoint for Person
        /// </summary>
        /// <param name="queryOptions"></param>
        /// <returns>People</returns>
        [EnableQuery]
        public IHttpActionResult GetPerson(ODataQueryOptions<Person> queryOptions)
        {
            var res = GetQueryable(queryOptions);
            _person.ScrubCprFromPersons(res);
            var currentTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            //We used to loop through all persons and remove employments that had expired
            //This proved to be to slow, and we did not have a use for current employments 
            //for a list of users, each time we needed someones employment we made queries
            //to that person anyway, so the return of this function has all employments
            //including the expired ones.
            return Ok(res);
        }

        
        /// <summary>
        /// GET API endpoint for CurrentUser.
        /// Sets HomeWorkDistance on each of the users employments.
        /// Strips CPR-number off.
        /// </summary>
        /// <returns>The user currently logged in.</returns>
        [EnableQuery(MaxExpansionDepth = 4)]
        // Disable caching.
        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public Person GetCurrentUser()
        {
            _logger.Log("CurrentUser() initial", "web", 3);
            try
            {
                var currentDateTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                var employments = _employmentRepo.AsQueryable().Where(x => x.PersonId == CurrentUser.Id && (x.EndDateTimestamp == 0 || x.EndDateTimestamp > currentDateTimestamp));
                var employmentList = employments.ToList();

                CurrentUser.Employments.Clear();
                foreach (var employment in employmentList)
                {
                    CurrentUser.Employments.Add(employment);
                }

                _person.AddHomeWorkDistanceToEmployments(CurrentUser);
                CurrentUser.CprNumber = "";
                CurrentUser.HasAppPassword = _appLoginRepo.AsQueryable().Any(x => x.PersonId == CurrentUser.Id);
                CurrentUser.IsSubstitute = _substituteRepo.AsQueryable().Any(x => x.SubId.Equals(CurrentUser.Id) && x.StartDateTimestamp < currentDateTimestamp && x.EndDateTimestamp > currentDateTimestamp);
           
            }catch(Exception ex)
            {
                _logger.Log("Exception GetCurrentUser(). Exception: " + ex.Message, "web", ex, 3);
            }
            _logger.Log("CurrentUser() end" + CurrentUser.FullName, "web", 3);
            return CurrentUser;

        }

        /// <summary>
        /// GET API endpoint for user as CurrentUser.
        /// Sets HomeWorkDistance on each of the users employments.<commit_msg>New log to see current user<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.OData;
using System.Web.OData.Query;
using Core.ApplicationServices;
using Core.ApplicationServices.Interfaces;
using Core.DomainModel;
using Core.DomainServices;
using Core.DomainServices.RoutingClasses;
using Infrastructure.DataAccess;
using Newtonsoft.Json.Schema;
using Core.ApplicationServices.Logger;

namespace OS2Indberetning.Controllers
{
    public class PersonController : BaseController<Person>
    {
        private IPersonService _person;
        private readonly IGenericRepository<Employment> _employmentRepo = new GenericRepository<Employment>(new DataContext());
        private readonly IGenericRepository<LicensePlate> _licensePlateRepo = new GenericRepository<LicensePlate>(new DataContext());
        private readonly IGenericRepository<Substitute> _substituteRepo;
        private readonly IGenericRepository<AppLogin> _appLoginRepo;
        private readonly ILogger _logger;

        public PersonController(IGenericRepository<Person> repo, IPersonService personService, IGenericRepository<Employment> employmentRepo, IGenericRepository<LicensePlate> licensePlateRepo, IGenericRepository<Substitute> substituteRepo, IGenericRepository<AppLogin> appLoginRepo, ILogger log)
            : base(repo, repo)
        {
            _person = personService;
            _employmentRepo = employmentRepo;
            _licensePlateRepo = licensePlateRepo;
            _substituteRepo = substituteRepo;
            _appLoginRepo = appLoginRepo;
            _logger = log;
        }

        // GET: odata/Person
        /// <summary>
        /// GET API endpoint for Person
        /// </summary>
        /// <param name="queryOptions"></param>
        /// <returns>People</returns>
        [EnableQuery]
        public IHttpActionResult GetPerson(ODataQueryOptions<Person> queryOptions)
        {
            var res = GetQueryable(queryOptions);
            _person.ScrubCprFromPersons(res);
            var currentTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            //We used to loop through all persons and remove employments that had expired
            //This proved to be to slow, and we did not have a use for current employments 
            //for a list of users, each time we needed someones employment we made queries
            //to that person anyway, so the return of this function has all employments
            //including the expired ones.
            return Ok(res);
        }

        
        /// <summary>
        /// GET API endpoint for CurrentUser.
        /// Sets HomeWorkDistance on each of the users employments.
        /// Strips CPR-number off.
        /// </summary>
        /// <returns>The user currently logged in.</returns>
        [EnableQuery(MaxExpansionDepth = 4)]
        // Disable caching.
        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public Person GetCurrentUser()
        {
            _logger.Log("CurrentUser() initial. Current userId=" + CurrentUser.Id + "CurrentUserInitials="+CurrentUser.Initials, "web", 3);
            try
            {
                var currentDateTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                var employments = _employmentRepo.AsQueryable().Where(x => x.PersonId == CurrentUser.Id && (x.EndDateTimestamp == 0 || x.EndDateTimestamp > currentDateTimestamp));
                var employmentList = employments.ToList();

                CurrentUser.Employments.Clear();
                foreach (var employment in employmentList)
                {
                    CurrentUser.Employments.Add(employment);
                }

                _person.AddHomeWorkDistanceToEmployments(CurrentUser);
                CurrentUser.CprNumber = "";
                CurrentUser.HasAppPassword = _appLoginRepo.AsQueryable().Any(x => x.PersonId == CurrentUser.Id);
                CurrentUser.IsSubstitute = _substituteRepo.AsQueryable().Any(x => x.SubId.Equals(CurrentUser.Id) && x.StartDateTimestamp < currentDateTimestamp && x.EndDateTimestamp > currentDateTimestamp);
           
            }catch(Exception ex)
            {
                _logger.Log("Exception GetCurrentUser(). Exception: " + ex.Message, "web", ex, 3);
            }
            _logger.Log("CurrentUser() end" + CurrentUser.FullName, "web", 3);
            return CurrentUser;

        }

        /// <summary>
        /// GET API endpoint for user as CurrentUser.
        /// Sets HomeWorkDistance on each of the users employments.
{'fraction_non_alphanumeric': 0.09237033861980283, 'fraction_numerical': 0.004929275610801543, 'mean_word_length': 2.544625902012913, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 61, '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': '7142912', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2341, 'n_words': 600}