<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Mtc.Domain.Common;
using Mtc.Domain.Models;
using Mtc.Domain.Services.Interfaces;
using Mtc.Infrastructure.DataAccess.Interfaces;
using MtcModel;

namespace Mtc.Domain.Services
{
    public class SectionContentService : ISectionContentService
    {
        private readonly IStructureContentRepository _structureContentRepository;

        public SectionContentService(IStructureContentRepository structureContentRepository)
        {
            _structureContentRepository = structureContentRepository;
        }

        public SectionContent GetById(int id)
        {
            return ModelHelper.Mapper(_structureContentRepository.GetById(id));
        }

        public SectionContent GetByName(string name)
        {
            throw new NotImplementedException();
        }

        public SectionContent Create(SectionContent entity)
        {
            throw new NotImplementedException();
        }

        public SectionContent Update(SectionContent entity)
        {
            throw new NotImplementedException();
        }

        public SectionContent Delete(SectionContent entity)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<SectionContent> GetAll()
        {
            throw new NotImplementedException();
        }


        public void UpdateSectionContent(int sectionContentId, string title, string mainText)
        {
            SectionContent sectionContent = ModelHelper.Mapper(
                _structureContentRepository.GetById(sectionContentId));
            sectionContent.Title = title;
            sectionContent.MainText = mainText;
            sectionContent.CurrentProgress = CalculateProgress(mainText, sectionContent.MinWordCount);
           _structureContentRepository.Update(ModelHelper.Mapper(sectionContent));
        }

        public MemoryStream GenerateSimpleDocument(int sectionContentId, ExportDocumentType exportDocumentType)
        {
            SectionContent sectionContent = GetById(sectionContentId);
            switch (exportDocumentType)
            {
                case ExportDocumentType.Docx:
                    return DocxDocumentGenerator.GenerateSimpleDocxDocument(sectionContent);
                case ExportDocumentType.Txt:
                    return DocxDocumentGenerator.GenerateSimpleTxtDocument(sectionContent);
                case ExportDocumentType.Pdf:
                    return PdfDocumentGenerator.GenerateSimplePdfDocument(sectionContent);
                default:
                    throw new InvalidOperationException("No such file format found");
            }
        }

        private int CalculateProgress(string mainText, int minWordCount)
        {
            int progress = 1;
            if (mainText.Length > 0)
            {
                String text = mainText.Trim();
                int wordCount = 0, index = 0;

                while (index < text.Length)
                {
                    // check if current char is part of a word
                    while (index < text.Length && Char.IsWhiteSpace(text[index]) == false)
                        index++;

                    wordCount++;

                    // skip whitespace until next word
                    while (index < text.Length && Char.IsWhiteSpace(text[index]) == true)
                        index++;
                }
                if (wordCount >= minWordCount)
                {
                    progress = 100;
                }
                else
                {
                    progress = (int)Math.Floor((double)wordCount * 100 / minWordCount);
                }
                if (progress > 100) progress = 100;
                else if (progress < 1) progress = 1;
            }

            return progress;
        }
    }
}
<commit_msg>fixing error on saving empty content<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Mtc.Domain.Common;
using Mtc.Domain.Models;
using Mtc.Domain.Services.Interfaces;
using Mtc.Infrastructure.DataAccess.Interfaces;
using MtcModel;

namespace Mtc.Domain.Services
{
    public class SectionContentService : ISectionContentService
    {
        private readonly IStructureContentRepository _structureContentRepository;

        public SectionContentService(IStructureContentRepository structureContentRepository)
        {
            _structureContentRepository = structureContentRepository;
        }

        public SectionContent GetById(int id)
        {
            return ModelHelper.Mapper(_structureContentRepository.GetById(id));
        }

        public SectionContent GetByName(string name)
        {
            throw new NotImplementedException();
        }

        public SectionContent Create(SectionContent entity)
        {
            throw new NotImplementedException();
        }

        public SectionContent Update(SectionContent entity)
        {
            throw new NotImplementedException();
        }

        public SectionContent Delete(SectionContent entity)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<SectionContent> GetAll()
        {
            throw new NotImplementedException();
        }


        public void UpdateSectionContent(int sectionContentId, string title, string mainText)
        {
            SectionContent sectionContent = ModelHelper.Mapper(
                _structureContentRepository.GetById(sectionContentId));
            sectionContent.Title = title;
            sectionContent.MainText = mainText;
            sectionContent.CurrentProgress = CalculateProgress(mainText, sectionContent.MinWordCount);
           _structureContentRepository.Update(ModelHelper.Mapper(sectionContent));
        }

        public MemoryStream GenerateSimpleDocument(int sectionContentId, ExportDocumentType exportDocumentType)
        {
            SectionContent sectionContent = GetById(sectionContentId);
            switch (exportDocumentType)
            {
                case ExportDocumentType.Docx:
                    return DocxDocumentGenerator.GenerateSimpleDocxDocument(sectionContent);
                case ExportDocumentType.Txt:
                    return DocxDocumentGenerator.GenerateSimpleTxtDocument(sectionContent);
                case ExportDocumentType.Pdf:
                    return PdfDocumentGenerator.GenerateSimplePdfDocument(sectionContent);
                default:
                    throw new InvalidOperationException("No such file format found");
            }
        }

        private int CalculateProgress(string mainText, int minWordCount)
        {
            int progress = 1;
            if (!String.IsNullOrEmpty(mainText))
            {
                String text = mainText.Trim();
                int wordCount = 0, index = 0;

                while (index < text.Length)
                {
                    // check if current char is part of a word
                    while (index < text.Length && Char.IsWhiteSpace(text[index]) == false)
                        index++;

                    wordCount++;

                    // skip whitespace until next word
                    while (index < text.Length && Char.IsWhiteSpace(text[index]) == true)
                        index++;
                }
                if (wordCount >= minWordCount)
                {
                    progress = 100;
                }
                else
                {
                    progress = (int)Math.Floor((double)wordCount * 100 / minWordCount);
                }
                if (progress > 100) progress = 100;
                else if (progress < 1) progress = 1;
            }

            return progress;
        }
    }
}

{'fraction_non_alphanumeric': 0.06690800149794034, 'fraction_numerical': 0.004368992635126701, 'mean_word_length': 1.8573466476462197, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 10, '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': '4224598', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1737, 'n_words': 410}