<commit_before>﻿using System.Linq;
using System.Threading.Tasks;
using Lucid.Infrastructure.Lib.Facade.Exceptions;

namespace Lucid.Modules.AppFactory.Design.Blueprints
{
    public class Blueprint : Registry.Entity
    {
        protected Blueprint() { }

        public virtual int      OwnedByUserId   { get; protected set; }
        public virtual string   Name            { get; protected set; }

        public static async Task<Blueprint> Start(StartCommand cmd)
        {
            var existingBlueprint = Repository.Query<Blueprint>()
                .Where(bp => bp.OwnedByUserId == cmd.OwnedByUserId)
                .Where(bp => bp.Name == cmd.Name)
                .SingleOrDefault();

            if (existingBlueprint != null)
                throw new FacadeException($"There is already a Blueprint with the name '{cmd.Name}'");

            return await Repository.SaveAsync(
                new Blueprint
                {
                    OwnedByUserId = cmd.OwnedByUserId,
                    Name = cmd.Name,
                });
        }
    }
}<commit_msg>Pushed uniqueness test into separate method.<commit_after>﻿using System.Linq;
using System.Threading.Tasks;
using Lucid.Infrastructure.Lib.Facade.Exceptions;

namespace Lucid.Modules.AppFactory.Design.Blueprints
{
    public class Blueprint : Registry.Entity
    {
        protected Blueprint() { }

        public virtual int      OwnedByUserId   { get; protected set; }
        public virtual string   Name            { get; protected set; }

        public static async Task<Blueprint> Start(StartCommand cmd)
        {
            VerifyNameIsUnique(cmd.OwnedByUserId, cmd.Name);

            return await Repository.SaveAsync(
                new Blueprint
                {
                    OwnedByUserId = cmd.OwnedByUserId,
                    Name = cmd.Name,
                });
        }

        private static void VerifyNameIsUnique(int ownedByUserId, string name)
        {
            var existingBlueprint = Repository.Query<Blueprint>()
                .Where(bp => bp.OwnedByUserId == ownedByUserId)
                .Where(bp => bp.Name == name)
                .SingleOrDefault();

            if (existingBlueprint != null)
                throw new FacadeException($"There is already a Blueprint with the name '{name}'");
        }
    }
}
{'fraction_non_alphanumeric': 0.08645655877342419, 'fraction_numerical': 0.0, 'mean_word_length': 1.8438256658595642, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '3629101', 'n_tokens_mistral': 618, 'n_tokens_neox': 602, 'n_words': 148}