Пример #1
0
        public virtual async Task <string> GetNextChildCodeAsync(Guid?parentId)
        {
            var lastChild = await GetLastChildOrNullAsync(parentId);

            if (lastChild != null)
            {
                return(CodeNumberGenerator.CalculateNextCode(lastChild.Code));
            }

            var parentCode = parentId != null
                ? await GetCodeOrDefaultAsync(parentId.Value)
                : null;

            return(CodeNumberGenerator.AppendCode(
                       parentCode,
                       CodeNumberGenerator.CreateCode(1)
                       ));
        }
        public virtual async Task <DataDto> CreateAsync(DataCreateDto input)
        {
            var data = await DataRepository.FindByNameAsync(input.Name);

            if (data != null)
            {
                throw new UserFriendlyException(L["DuplicateData", input.Name]);
            }

            string code     = string.Empty;
            var    children = await DataRepository.GetChildrenAsync(input.ParentId);

            if (children.Any())
            {
                var lastChildren = children.OrderBy(x => x.Code).FirstOrDefault();
                code = CodeNumberGenerator.CalculateNextCode(lastChildren.Code);
            }
            else
            {
                var parentData = input.ParentId != null
                ? await DataRepository.GetAsync(input.ParentId.Value)
                : null;

                code = CodeNumberGenerator.AppendCode(parentData?.Code, CodeNumberGenerator.CreateCode(1));
            }

            data = new Data(
                GuidGenerator.Create(),
                input.Name,
                code,
                input.DisplayName,
                input.Description,
                input.ParentId,
                CurrentTenant.Id
                );

            data = await DataRepository.InsertAsync(data);

            await CurrentUnitOfWork.SaveChangesAsync();

            return(ObjectMapper.Map <Data, DataDto>(data));
        }