Ejemplo n.º 1
0
        public async void add_keys_should_create_new_word_and_return_bool()
        {
            //arrange           
            var validModel = new WordModel
            {
                Key = "key",
                Tag = "tag",
                Description = "desc"
            };
            var wordService = new Mock<IWordService>();
            wordService.Setup(x => x.Create(validModel)).Returns(Task.FromResult(validModel.Key));

            //act
            var sut = new ApiControllerBuilder().WithWordService(wordService.Object)
                                                .Build();

            var result = await sut.AddKeys(validModel.Key, validModel.Tag);

            //assert
            Assert.NotNull(result);
            Assert.NotNull(result.Data);
            Assert.IsAssignableFrom(typeof(bool), result.Data);

            sut.AssertPostAttribute(ActionNameAddKeys, new[] { typeof(string), typeof(string) }); 

            wordService.Verify(x => x.Create(It.IsAny<WordModel>()), Times.AtLeastOnce); 
        }
Ejemplo n.º 2
0
        public void should_create_check_wordmodel_IsValidForNew()
        {
            //arrange
            var model = new WordModel();

            //act
            var sut = new WordServiceBuilder().WithWordRepository(Mock.Of<IRepository<Word>>()).Build();

            var result = sut.Create(model);

            //assert
            Assert.IsNull(result);
        }
Ejemplo n.º 3
0
        public Task<string> Create(WordModel model)
        {
            if (!model.IsValidForNew())
            {
                return null;
            }

            var slug = model.Key.ToUrlSlug();
            if (_wordRepository.Set<Word>().Any(x => x.Key == slug))
            {
                return null;
            }

            var tags = new List<Tag>();
            if (!string.IsNullOrEmpty(model.Tag))
            {
                var items = model.Tag.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in items)
                {
                    tags.Add(new Tag
                    {
                        CreatedBy = model.CreatedBy,
                        Name = item,
                        UrlName = item.ToUrlSlug()
                    });
                }
            }

            var word = new Word
            {
                Key = slug,
                Description = model.Description ?? string.Empty,
                IsTranslated = false,
                TranslationCount = 0,
                CreatedBy = model.CreatedBy,
                UpdatedBy = model.CreatedBy,
                Tags = tags
            };

            _wordRepository.Create(word);
            _wordRepository.SaveChanges();

            if (word.Id < 1)
            {
                return null;
            }

            return Task.FromResult(word.Key);
        }
Ejemplo n.º 4
0
        public async Task<ActionResult> New(WordModel model)
        {
            if (!model.IsValidForNew())
            {
                model.Msg = "bir sorun oluştu";
                return View(model);
            }

            model.CreatedBy = User.Identity.GetUserId();
            
            var key = await _wordService.Create(model);
            if (key == null)
            {
                model.Msg = "bir sorun oluştu, daha önce eklenmiş olabilir";
                return View(model);
            }

            return Redirect("/word/detail/" + key);
        }
Ejemplo n.º 5
0
        public void create_check_if_word_allready_exists()
        {
            //arrange
            var model = new WordModel
            {
                Key = "key",
                Tag = "tag"
            };

            var wordRepository = new Mock<IRepository<Word>>();
            wordRepository.Setup(x => x.Any(It.IsAny<Expression<Func<Word, bool>>>()))
                          .Returns(true);

            //act
            var sut = new WordServiceBuilder().WithWordRepository(wordRepository.Object)
                                              .Build();

            var result = sut.Create(model);

            //assert
            Assert.IsNull(result);
            wordRepository.Verify(x => x.Any(It.IsAny<Expression<Func<Word, bool>>>()), Times.Once);
          
        }
Ejemplo n.º 6
0
        public void should_create_word()
        {
            //arrange
            var model = new WordModel
            {
                Key = "key",
                Tag = "tag1"
            };

            var wordRepository = new Mock<IRepository<Word>>();
            wordRepository.Setup(x => x.SaveChanges()).Returns(true);

            //act
            var sut = new WordServiceBuilder().WithWordRepository(wordRepository.Object).Build();

            var result = sut.Create(model);

            //assert
            Assert.IsNotNull(result);
            Assert.AreEqual(model.Key, result.Result);
            
            wordRepository.Verify(x => x.Create(It.IsAny<Word>()), Times.Once);
            wordRepository.Verify(x => x.SaveChanges(), Times.Once);
        }
Ejemplo n.º 7
0
        public void should_create_check_for_savechanges_returns_true()
        {
            //arrange
            var model = new WordModel
            {
                Key = "key",
                Tag = "tag"
            };

            var wordRepository = new Mock<IRepository<Word>>();
            wordRepository.Setup(x => x.SaveChanges()).Returns(false);

            //act
            var sut = new WordServiceBuilder().WithWordRepository(wordRepository.Object).Build();

            var result = sut.Create(model);

            //assert
            Assert.IsNull(result);
        }
Ejemplo n.º 8
0
 public ViewResult New()
 {
     var model = new WordModel();
     return View(model);
 }
Ejemplo n.º 9
0
        public async void locales_should_return_name_value_model()
        {
            //arrange           
            var validModel = new WordModel
            {
                Key = "key",
                Tag = "tag",
                Languages = new List<LanguageModel>
                {
                    new LanguageModel
                    {
                        Key="tr"
                    }
                },
                Description = "desc"
            };

            var list = new List<Word> { new Word { Id = 1 }, new Word { Id = 2 } }; 

            var tagService = new Mock<ITagService>();
            tagService.Setup(x => x.GetWords(validModel.Tag, 1))
                      .Returns(Task.FromResult(new PagedList<Word>(1, 2, 3, list)));
             
            var wordService = new Mock<IWordService>();
            wordService.Setup(x => x.GetWords(1))
                       .Returns(Task.FromResult(new PagedList<Word>(1, 2, 3, list)));
              
            //act
            var sut = new ApiControllerBuilder().WithWordService(wordService.Object)
                                                .WithTagService(tagService.Object)
                                                .Build();

            var result = await sut.Locales(validModel.Tag, "tr",1);

            //assert
            Assert.NotNull(result);
            Assert.NotNull(result.Data);
            Assert.IsAssignableFrom(typeof(List<NameValueModel>), result.Data);

            sut.AssertGetAttribute(ActionNameLocales, new[] { typeof(string), typeof(string) ,typeof(int) });

            wordService.Verify(x => x.GetWords(1), Times.AtMostOnce);
            tagService.Verify(x => x.GetWords(validModel.Tag,1), Times.AtMostOnce); 
        } 
Ejemplo n.º 10
0
        public static WordModel MapEntityToModel(Word entity)
        {
            var model = new WordModel
            {
                Key = entity.Key,
                Description = entity.Description,
                IsTranslated = entity.IsTranslated,
                CreatedBy = entity.CreatedBy
            };

            if (entity.Tags != null
                && entity.Tags.Any())
            {
                foreach (var tag in entity.Tags)
                {
                    model.Tags.Add(new TagModel
                    {
                        Name = tag.Name,
                        UrlName = tag.UrlName
                    });

                    model.Tag += string.Format("{0},", tag);
                }
            }

            if (entity.IsTranslated)
            {
                if (!string.IsNullOrEmpty(entity.Translation_TR))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key = entity.Key,
                        Value = entity.Translation_TR,
                        Language = LanguageModel.TR()
                    });

                    model.Languages.Add(LanguageModel.TR());
                }

                if (!string.IsNullOrEmpty(entity.Translation_EN))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key = entity.Key,
                        Value = entity.Translation_EN,
                        Language = LanguageModel.EN()
                    });

                    model.Languages.Add(LanguageModel.EN());
                }

                if (!string.IsNullOrEmpty(entity.Translation_AZ))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key = entity.Key,
                        Value = entity.Translation_AZ,
                        Language = LanguageModel.AZ()
                    });

                    model.Languages.Add(LanguageModel.AZ());
                }

                if (!string.IsNullOrEmpty(entity.Translation_SP))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key = entity.Key,
                        Value = entity.Translation_SP,
                        Language = LanguageModel.SP()
                    });

                    model.Languages.Add(LanguageModel.SP());
                }
                if (!string.IsNullOrEmpty(entity.Translation_CN))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key = entity.Key,
                        Value = entity.Translation_CN,
                        Language = LanguageModel.CN()
                    });

                    model.Languages.Add(LanguageModel.CN());
                }
                if (!string.IsNullOrEmpty(entity.Translation_FR))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key = entity.Key,
                        Value = entity.Translation_FR,
                        Language = LanguageModel.FR()
                    });

                    model.Languages.Add(LanguageModel.FR());
                }
                if (!string.IsNullOrEmpty(entity.Translation_GR))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key = entity.Key,
                        Value = entity.Translation_GR,
                        Language = LanguageModel.GR()
                    });

                    model.Languages.Add(LanguageModel.GR());
                }
                if (!string.IsNullOrEmpty(entity.Translation_IT))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key = entity.Key,
                        Value = entity.Translation_IT,
                        Language = LanguageModel.IT()
                    });

                    model.Languages.Add(LanguageModel.IT());
                }
                if (!string.IsNullOrEmpty(entity.Translation_KZ))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key = entity.Key,
                        Value = entity.Translation_KZ,
                        Language = LanguageModel.KZ()
                    });

                    model.Languages.Add(LanguageModel.KZ());
                }
                if (!string.IsNullOrEmpty(entity.Translation_RU))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key = entity.Key,
                        Value = entity.Translation_RU,
                        Language = LanguageModel.RU()
                    });

                    model.Languages.Add(LanguageModel.RU());
                }
                if (!string.IsNullOrEmpty(entity.Translation_TK))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key = entity.Key,
                        Value = entity.Translation_TK,
                        Language = LanguageModel.TK()
                    });

                    model.Languages.Add(LanguageModel.TK());
                }
            }

            return model;
        }
Ejemplo n.º 11
0
        public static WordModel MapEntityToModel(Word entity)
        {
            var model = new WordModel
            {
                Key          = entity.Key,
                Description  = entity.Description,
                IsTranslated = entity.IsTranslated,
                CreatedBy    = entity.CreatedBy
            };

            if (entity.Tags != null &&
                entity.Tags.Any())
            {
                foreach (var tag in entity.Tags)
                {
                    model.Tags.Add(new TagModel
                    {
                        Name    = tag.Name,
                        UrlName = tag.UrlName
                    });

                    model.Tag += string.Format("{0},", tag);
                }
            }

            if (entity.IsTranslated)
            {
                if (!string.IsNullOrEmpty(entity.Translation_TR))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key      = entity.Key,
                        Value    = entity.Translation_TR,
                        Language = LanguageModel.TR()
                    });

                    model.Languages.Add(LanguageModel.TR());
                }

                if (!string.IsNullOrEmpty(entity.Translation_EN))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key      = entity.Key,
                        Value    = entity.Translation_EN,
                        Language = LanguageModel.EN()
                    });

                    model.Languages.Add(LanguageModel.EN());
                }

                if (!string.IsNullOrEmpty(entity.Translation_AZ))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key      = entity.Key,
                        Value    = entity.Translation_AZ,
                        Language = LanguageModel.AZ()
                    });

                    model.Languages.Add(LanguageModel.AZ());
                }

                if (!string.IsNullOrEmpty(entity.Translation_SP))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key      = entity.Key,
                        Value    = entity.Translation_SP,
                        Language = LanguageModel.SP()
                    });

                    model.Languages.Add(LanguageModel.SP());
                }
                if (!string.IsNullOrEmpty(entity.Translation_CN))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key      = entity.Key,
                        Value    = entity.Translation_CN,
                        Language = LanguageModel.CN()
                    });

                    model.Languages.Add(LanguageModel.CN());
                }
                if (!string.IsNullOrEmpty(entity.Translation_FR))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key      = entity.Key,
                        Value    = entity.Translation_FR,
                        Language = LanguageModel.FR()
                    });

                    model.Languages.Add(LanguageModel.FR());
                }
                if (!string.IsNullOrEmpty(entity.Translation_GR))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key      = entity.Key,
                        Value    = entity.Translation_GR,
                        Language = LanguageModel.GR()
                    });

                    model.Languages.Add(LanguageModel.GR());
                }
                if (!string.IsNullOrEmpty(entity.Translation_IT))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key      = entity.Key,
                        Value    = entity.Translation_IT,
                        Language = LanguageModel.IT()
                    });

                    model.Languages.Add(LanguageModel.IT());
                }
                if (!string.IsNullOrEmpty(entity.Translation_KZ))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key      = entity.Key,
                        Value    = entity.Translation_KZ,
                        Language = LanguageModel.KZ()
                    });

                    model.Languages.Add(LanguageModel.KZ());
                }
                if (!string.IsNullOrEmpty(entity.Translation_RU))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key      = entity.Key,
                        Value    = entity.Translation_RU,
                        Language = LanguageModel.RU()
                    });

                    model.Languages.Add(LanguageModel.RU());
                }
                if (!string.IsNullOrEmpty(entity.Translation_TK))
                {
                    model.Translations.Add(new TranslationModel
                    {
                        Key      = entity.Key,
                        Value    = entity.Translation_TK,
                        Language = LanguageModel.TK()
                    });

                    model.Languages.Add(LanguageModel.TK());
                }
            }

            return(model);
        }
Ejemplo n.º 12
0
        public Task<string> Update(WordModel model)
        {
            if (!model.IsValidForNew())
            {
                return null;
            }

            var slug = model.Key.ToUrlSlug();

            var wordEntity = _wordRepository.FindOne(x => x.Key == slug);
            if (wordEntity == null) return Create(model);

            _wordRepository.SoftDelete(wordEntity.Id, model.CreatedBy);
            _wordRepository.SaveChanges();

            return Create(model);
        }
Ejemplo n.º 13
0
        public async Task<ActionResult> Import(HttpPostedFileBase file, bool isOverWrite = false)
        {
            var model = new ExcelImportModel();

            if (file == null
                || file.ContentLength <= 0)
            {
                model.Msg = _htmlHelper.LocalizationString("please_select_file");
                return View(model);
            }

            var extension = Path.GetExtension(file.FileName);
            if (extension != ".xls"
                && extension != ".xlsx")
            {
                model.Msg = _htmlHelper.LocalizationString("please_select_excel_file");
                return View(model);
            }

            var excelName = string.Format("{0:yyyy-MM-dd}-{1}", DateTime.Now, Guid.NewGuid());
            var path = string.Format("{0}/{1}{2}", Server.MapPath("~/Public/files"), excelName, extension);

            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }
            file.SaveAs(path);

            var existingFile = new FileInfo(path);
            using (var package = new ExcelPackage(existingFile))
            {
                var workBook = package.Workbook;
                if (workBook == null
                    || workBook.Worksheets.Count <= 0)
                {
                    model.Msg = _htmlHelper.LocalizationString("excel_file_has_problems");
                    return View(model);
                }

                var currentWorksheet = workBook.Worksheets.First();
                var count = currentWorksheet.Dimension.End.Column;
                if (count <= 2)
                {
                    for (var i = 2; i < currentWorksheet.Dimension.End.Row; i++)
                    {
                        var key = currentWorksheet.Cells[i, 1].Value.ToString();
                        var tag = currentWorksheet.Cells[i, 2].Value.ToString();
                        var wordModels = new WordModel { Key = key, Tag = tag, CreatedBy = User.Identity.GetUserId() };
                        try
                        {
                            if (isOverWrite)
                            {
                                await _wordService.Update(wordModels);
                            }
                            else
                            {
                                await _wordService.Create(wordModels);
                            }
                        }
                        catch
                        {
                            model.Msg = _htmlHelper.LocalizationString("please_try_again");
                            return View(model);
                        }
                    }
                }
                else
                {
                    for (var i = 2; i < currentWorksheet.Dimension.End.Row; i++)
                    {
                        var key = currentWorksheet.Cells[i, 1].Value.ToString();
                        var desc = currentWorksheet.Cells[i, 2].Value.ToString();
                        var tag = currentWorksheet.Cells[i, 3].Value.ToString();

                        var translationTR = currentWorksheet.Cells[i, 4].Value.ToString();
                        var translationEN = currentWorksheet.Cells[i, 5].Value.ToString();
                        var translationAZ = currentWorksheet.Cells[i, 6].Value.ToString();
                        var translationCN = currentWorksheet.Cells[i, 7].Value.ToString();
                        var translationFR = currentWorksheet.Cells[i, 8].Value.ToString();
                        var translationGR = currentWorksheet.Cells[i, 9].Value.ToString();
                        var translationIT = currentWorksheet.Cells[i, 10].Value.ToString();
                        var translationKZ = currentWorksheet.Cells[i, 11].Value.ToString();
                        var translationRU = currentWorksheet.Cells[i, 12].Value.ToString();
                        var translationSP = currentWorksheet.Cells[i, 13].Value.ToString();
                        var translationTK = currentWorksheet.Cells[i, 14].Value.ToString();

                        var wordModel = new WordModel { Key = key, Description = desc, Tag = tag, CreatedBy = User.Identity.GetUserId() };

                        try
                        {
                            if (isOverWrite)
                            {
                                await _wordService.Update(wordModel);
                            }
                            else
                            {
                                await _wordService.Create(wordModel);
                            }

                            await _wordService.Translate(key, "TR", translationTR);
                            await _wordService.Translate(key, "EN", translationEN);
                            await _wordService.Translate(key, "AZ", translationAZ);
                            await _wordService.Translate(key, "CN", translationCN);
                            await _wordService.Translate(key, "FR", translationFR);
                            await _wordService.Translate(key, "GR", translationGR);
                            await _wordService.Translate(key, "IT", translationIT);
                            await _wordService.Translate(key, "KZ", translationKZ);
                            await _wordService.Translate(key, "RU", translationRU);
                            await _wordService.Translate(key, "SP", translationSP);
                            await _wordService.Translate(key, "TK", translationTK);
                        }
                        catch
                        {
                            model.Msg = _htmlHelper.LocalizationString("please_try_again");
                            return View(model);
                        }
                    }
                }

            }

            model.Msg = _htmlHelper.LocalizationString("import_successful");
            model.IsSuccess = true;

            return View(model);
        }