<commit_before>﻿using CharacterManager.ViewModels;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CharacterManager.Models
{
    /// <summary>
    /// Interaction with the database.
    /// </summary>
    public class Repository
    {
        private readonly ApplicationContext _context = new ApplicationContext();

        /// <summary>
        /// 
        /// </summary>
        public enum CharacterCreationStatusCode
        {
            /// <summary>
            /// Congratulations!
            /// </summary>
            Created,

            /// <summary>
            /// Level was out of range
            /// </summary>
            LevelOutOfBound,

            FactionDoesNotExist,
            RaceDoesNotExist,
            ClassDoesNotExist,

            /// <summary>
            /// The user name is already chosen
            /// </summary>
            AlreadyExists,

            /// <summary>
            /// The selected race and faction were not compatible
            /// </summary>
            InvalidRaceFactionMapping,

            /// <summary>
            /// The selected race and class were not compatible
            /// </summary>
            InvalidRaceClassMapping,

            /// <summary>
            /// User doesn't have a high enough character to start a new one
            /// </summary>
            UserLevelRequirementsNotMet,

            /// <summary>
            /// Not yet defined model error
            /// </summary>
            UncaughtModelError
        }

        /// <summary>
        /// Gets a character from the database and converts it to a view-model for the client
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public CharacterViewModel GetCharacter(string name)
        {
            return AutoMapper.Mapper.Map<CharacterViewModel>(_context.Characters.Single(character => character.Name == name));
        }

        /// <summary>
        /// Gets a;; characters from the database and converts them to a view-model for the client
        /// </summary>
        /// <returns></returns>
        public IEnumerable<CharacterViewModel> GetCharacters()
        {
            return AutoMapper.Mapper.Map<IEnumerable<CharacterViewModel>>(_context.Characters);
        }
        
        /// <summary>
        /// Delete a character from the database - this is actually only a logical delete (record remains in db)
        /// </summary>
        /// <param name="name"></param>
        public int DeleteCharacter(string name)
        {
            var _character = _context.Characters.SingleOrDefault(character => character.Name == name);

            if (_character == null) return 0;

            _character.IsDeleted = true;

            return _context.SaveChanges();
        }

        /// <summary>
        /// Restore a character (change delete flag) from the database.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int RestoreCharacter(string name)
        {
            var _character = _context.Characters.SingleOrDefault(character => character.Name == name);

            if (_character == null) return 0;

            _character.IsDeleted = false;

            return _context.SaveChanges();
        }

        public CharacterCreationStatusCode CreateCharacter(CharacterViewModel characterViewModel)
        {
            var character = AutoMapper.Mapper.Map<Character>(characterViewModel);

            var statusCode = CanCreateCharacter(character);

            if (statusCode == CharacterCreationStatusCode.Created)
            {
                _context.Characters.Add(character);
                _context.SaveChanges();
            }

            return statusCode;
        }

        /// <summary>
        /// Checks to see if the new character has a valid race/faction mapping
        /// </summary>
        /// <param name="race">Race of the potential character</param><commit_msg>refactor(cleanup)<commit_after>﻿using CharacterManager.ViewModels;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CharacterManager.Models
{
    /// <summary>
    /// Interaction with the database.
    /// </summary>
    public class Repository
    {
        private readonly ApplicationContext _context = new ApplicationContext();

        /// <summary>
        /// List of codes returned when trying to create a new character
        /// </summary>
        public enum CharacterCreationStatusCode
        {
            /// <summary>
            /// Congratulations!
            /// </summary>
            Created,

            /// <summary>
            /// Level was out of range
            /// </summary>
            LevelOutOfBound,

            /// <summary>
            /// Faction is not valid
            /// </summary>
            FactionDoesNotExist,

            /// <summary>
            /// Race is not valid
            /// </summary>
            RaceDoesNotExist,

            /// <summary>
            /// Class is not valid
            /// </summary>
            ClassDoesNotExist,

            /// <summary>
            /// The user name is already chosen
            /// </summary>
            AlreadyExists,

            /// <summary>
            /// The selected race and faction were not compatible
            /// </summary>
            InvalidRaceFactionMapping,

            /// <summary>
            /// The selected race and class were not compatible
            /// </summary>
            InvalidRaceClassMapping,

            /// <summary>
            /// User doesn't have a high enough character to start a new one
            /// </summary>
            UserLevelRequirementsNotMet,

            /// <summary>
            /// Not yet defined model error
            /// </summary>
            UncaughtModelError
        }

        /// <summary>
        /// Gets a character from the database and converts it to a view-model for the client
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public CharacterViewModel GetCharacter(string name)
        {
            return AutoMapper.Mapper.Map<CharacterViewModel>(_context.Characters.Single(character => character.Name == name));
        }

        /// <summary>
        /// Gets a;; characters from the database and converts them to a view-model for the client
        /// </summary>
        /// <returns></returns>
        public IEnumerable<CharacterViewModel> GetCharacters()
        {
            return AutoMapper.Mapper.Map<IEnumerable<CharacterViewModel>>(_context.Characters);
        }
        
        /// <summary>
        /// Delete a character from the database - this is actually only a logical delete (record remains in db)
        /// </summary>
        /// <param name="name"></param>
        public int DeleteCharacter(string name)
        {
            var _character = _context.Characters.SingleOrDefault(character => character.Name == name);

            if (_character == null) return 0;

            _character.IsDeleted = true;

            return _context.SaveChanges();
        }

        /// <summary>
        /// Restore a character (change delete flag) from the database.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int RestoreCharacter(string name)
        {
            var _character = _context.Characters.SingleOrDefault(character => character.Name == name);

            if (_character == null) return 0;

            _character.IsDeleted = false;

            return _context.SaveChanges();
        }

        /// <summary>
        /// Attempts to create a new character from the client input
        /// </summary>
        /// <param name="characterViewModel"></param>
        /// <returns></returns>
        public CharacterCreationStatusCode CreateCharacter(CharacterViewModel characterViewModel)
        {
            var character = AutoMapper.Mapper.Map<Character>(characterViewModel);

            var statusCode = CanCreateCharacter(character);

            if (statusCode == CharacterCreationStatusCode.Created)
            {
                _context.Characters.Add(character);
                _context.SaveChanges();
            }

            return statusCode;
        }

        /// <summary>
        /// Checks to see if the new character has a valid race/faction mapping
        /// </summary>
        /// <param name="race">Race of the potential character</param>
{'fraction_non_alphanumeric': 0.10986573263280794, 'fraction_numerical': 0.0004670169293636894, 'mean_word_length': 1.561602870813397, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 115, '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': '4024865', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1931, 'n_words': 559}