<commit_before>﻿using System;

using Zk.Models;
using Zk.Repositories;

namespace Zk.Services
{
    public class AnonymousUserService : IUserService
    {
        readonly Repository repository;
        readonly CookieProvider cookieProvider;

        public AnonymousUserService(Repository repository, CookieProvider cookieProvider)
        {
            this.repository = repository;
            this.cookieProvider = cookieProvider;
        }
            
        public User CurrentAnonymousUser 
        { 
            get 
            { 
                string guid;
                var guidFromClient = GetGuidFromClient();

                if (string.IsNullOrEmpty(guidFromClient))
                {
                    guid = Guid.NewGuid().ToString();
                    AddUser(guid, null, null);
                    StoreGuidOnClient(guid);
                }
                else
                {
                    guid = guidFromClient;
                }

                return repository.GetUserByUserName(guid);
            }

        }

        public void AddUser(string userName, string fullName, string email)
        {
            var user = new User 
                {
                    Name = userName,
                    AuthenticationStatus = AuthenticatedStatus.Anonymous, // by definition
                    CreationDate = DateTime.Now
                };
            repository.AddUser(user);

            // Get the actual user from the database, so we get the created UserId.
            var newlyCreatedUser = repository.GetUserByUserName(userName);

            // Create calendar for the anonymous user
            repository.CreateCalendar(newlyCreatedUser);
        }

        public int GetCurrentUserId()
        {
            return CurrentAnonymousUser.UserId;
        }

        public User GetUser(int id)
        {
            return repository.GetUserById(id);
        }

        const string anonymousUserCookieKey = "anonymousUserKey"; 
        const double anonymousUserCookieExpiration = 730;

        private string GetGuidFromClient()
        {
            return cookieProvider.GetCookie(anonymousUserCookieKey);
        }

        private void StoreGuidOnClient(string guid)
        {
            cookieProvider.SetCookie(anonymousUserCookieKey, guid, anonymousUserCookieExpiration);
        }
            <commit_msg>Change Guid get and set method to property<commit_after>﻿using System;

using Zk.Models;
using Zk.Repositories;

namespace Zk.Services
{
    public class AnonymousUserService : IUserService
    {
        readonly Repository repository;
        readonly CookieProvider cookieProvider;

        const string anonymousUserCookieKey = "anonymousUserKey"; 
        const double anonymousUserCookieExpiration = 730;

        private string guidOnClient;

        public AnonymousUserService(Repository repository, CookieProvider cookieProvider)
        {
            this.repository = repository;
            this.cookieProvider = cookieProvider;
        }
            
        public User CurrentAnonymousUser 
        { 
            get 
            { 
                if (string.IsNullOrEmpty(GuidOnClient))
                {
                    var guid = Guid.NewGuid().ToString();
                    AddUser(guid, null, null);
                    GuidOnClient = guid;

                    return repository.GetUserByUserName(guid);
                }
                else
                {
                    return repository.GetUserByUserName(GuidOnClient);
                }

            }

        }
            
        protected string GuidOnClient
        {
            get 
            { 
                if (guidOnClient == null)
                {
                    guidOnClient = cookieProvider.GetCookie(anonymousUserCookieKey);
                }
                return guidOnClient;
            }
            set 
            { 
                cookieProvider.SetCookie(anonymousUserCookieKey, value, anonymousUserCookieExpiration); 
            }
        }
            
        public void AddUser(string userName, string fullName, string email)
        {
            var user = new User 
                {
                    Name = userName,
                    AuthenticationStatus = AuthenticatedStatus.Anonymous, // by definition
                    CreationDate = DateTime.Now
                };
            repository.AddUser(user);

            // Get the actual user from the database, so we get the created UserId.
            var newlyCreatedUser = repository.GetUserByUserName(userName);

            // Create calendar for the anonymous user
            repository.CreateCalendar(newlyCreatedUser);
        }

        public int GetCurrentUserId()
        {
            return CurrentAnonymousUser.UserId;
        }

        public User GetUser(int id)
        {
            return repository.GetUserById(id);
        }
            
{'fraction_non_alphanumeric': 0.05813246911079603, 'fraction_numerical': 0.0012153129430828439, 'mean_word_length': 1.437314906219151, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '346786', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1063, 'n_words': 258}