public bool PutJoke(JokeViewModel joke)
 {
     var entity = RepositoryFactory.Context.JokeDetails.FirstOrDefault(x => x.Id == joke.JokeId);
     if (entity == null)
         throw new Exception(ThrowMessage.JokeNotExists.ToString());
     if (entity.JokeCategoryDetail.JokeCategory.Id != joke.JokeCategoryId) // in edit i change category
     {
         JokeCategoryDetailDao categoryDetail = GetJokeCategory(joke.JokeCategoryId, joke.LanguageId);
         entity.JokeCategoryDetail = categoryDetail;
         foreach (AvailableLanguage al in joke.JokeTranslations)
         {
             if (al.HasTranslation && al.Language.Id != joke.LanguageId)
             {
                 JokeCategoryDetailDao categoryDetailForOtherTranslation = GetJokeCategory(joke.JokeCategoryId, al.Language.Id);
                 var otherTranslationJoke =
                     RepositoryFactory.Context.JokeDetails.FirstOrDefault(x => x.Joke.Id == joke.Id);
                 if (otherTranslationJoke != null)
                     otherTranslationJoke.JokeCategoryDetail = categoryDetailForOtherTranslation;
             }
         }
     }
     else if (!entity.JokeContent.Equals(joke.JokeContent))
         if (RepositoryFactory.Context.JokeDetails.FirstOrDefault(x => x.JokeContent.Equals(joke.JokeContent)) !=
             null)
             throw new Exception(ThrowMessage.JokeExists.ToString());
     entity.JokeContent = joke.JokeContent;
     RepositoryFactory.Context.SaveChanges();
     return true;
 }
 public JokeViewModel GetJokeTranslation(int jokeId, int languageId)
 {
     var entity =
         RepositoryFactory.Context.JokeDetails.FirstOrDefault(
             x => x.Joke.Id == jokeId && x.Language.Id == languageId);
     if (entity == null)
         throw new Exception(ThrowMessage.JokeTranslationNotExists.ToString());
     JokeViewModel joke = new JokeViewModel { Id = jokeId, JokeContent = entity.JokeContent, LanguageId = entity.Language.Id, JokeId = entity.Id };
     return joke;
 }
 public JsonResult<ResponseViewModel<JokeViewModel>> PostJoke(JokeViewModel joke)
 {
     var response = ServiceExecutor.Execute(() => _jokeManagementService.InsertJoke(joke));
     return Json(response);
 }
 public JsonResult<ResponseViewModel<bool>> PostDeleteJoke(JokeViewModel joke)
 {
     var response = ServiceExecutor.Execute(() => _jokeManagementService.RemoveJoke(joke));
     return Json(response);
 }
 public JokeViewModel InsertJoke(JokeViewModel joke)
 {
     if(!ValidateInsertJoke(joke))
         throw new Exception(ThrowMessage.JokeNotAdded.ToString());
     return ValidateGetInsertedJoke(joke);
 }
 private JokeViewModel ValidateGetInsertedJoke(JokeViewModel joke)
 {
     var insertedJoke = RepositoryFactory.Context.JokeDetails.FirstOrDefault(x => x.JokeContent.Equals(joke.JokeContent));
     if (insertedJoke == null)
         throw new Exception(ThrowMessage.JokeNotAdded.ToString());
     joke.Id = insertedJoke.Joke.Id;
     joke.JokeId = insertedJoke.Id;
     joke.JokeTranslations = FillAvailableTranslation(joke.Id);
     return joke;
 }
 private bool ValidateInsertJoke(JokeViewModel joke)
 {
     if (IsJokeContentExists(joke))
         throw new Exception(ThrowMessage.JokeExists.ToString());
     return IsMainJokeTranslation(joke)
         ? InsertMainJokeTranslation(joke)
         : InsertAnotherJokeTranslation(joke);
 }
 private bool RemoveAllJokeTranslation(JokeViewModel joke)
 {
     var jokeDetails = RepositoryFactory.Context.JokeDetails.Where(x => x.Joke.Id == joke.Id);
     if (!jokeDetails.Any())
         throw new Exception(ThrowMessage.JokeDetailNotExists.ToString());
     RepositoryFactory.Context.JokeDetails.RemoveRange(jokeDetails);
     RepositoryFactory.Context.SaveChanges();
     var mainJoke = RepositoryFactory.Context.Jokes.FirstOrDefault(x => x.Id == joke.Id);
     if (mainJoke == null)
         throw new Exception(ThrowMessage.JokeNotExists.ToString());
     RepositoryFactory.Context.Jokes.Remove(mainJoke);
     RepositoryFactory.Context.SaveChanges();
     return true;
 }
 private bool RemoveSelectedJokeTranslation(JokeViewModel joke)
 {
     var jokeDetail = RepositoryFactory.Context.JokeDetails.FirstOrDefault(x => x.Id == joke.JokeId);
     if (jokeDetail != null)
         RepositoryFactory.Context.JokeDetails.Remove(jokeDetail);
     int jokeTranslationCount = 0;
     if (joke.JokeTranslations == null)
     {
         joke.JokeTranslations?.ForEach(x =>
         {
             if (x.HasTranslation)
                 jokeTranslationCount++;
         });
     }
     if (jokeTranslationCount == 1)//have only one translation , can delete main id for joke . Safe is safe ...
     {
         var mainJoke = RepositoryFactory.Context.Jokes.FirstOrDefault(x => x.Id == joke.Id);
         if (mainJoke != null)
             RepositoryFactory.Context.Jokes.Remove(mainJoke);
     }
     RepositoryFactory.Context.SaveChanges();
     return true;
 }
 private bool IsMainJokeTranslation(JokeViewModel joke)
 {
     return joke.Id == 0;
 }
 private bool PutJokeContent(JokeViewModel joke)
 {
     var jokeToEdit = RepositoryFactory.Context.JokeDetails.FirstOrDefault(x => x.Id == joke.JokeId);
     if (jokeToEdit == null)
         throw new Exception(ThrowMessage.JokeTranslationNotExists.ToString());
     jokeToEdit.JokeContent = joke.JokeContent;
     RepositoryFactory.Context.SaveChanges();
     return true;
 }
 private bool IsJokeContentExists(JokeViewModel joke)
 {
     return RepositoryFactory.Context.JokeDetails.Any(x => x.JokeContent == joke.JokeContent);
 }
 private bool IsAnotherJokeTranslationToEdit(JokeViewModel joke)
 {
     return joke.JokeId > 0;
 }
 private bool InsertMainJokeTranslation(JokeViewModel joke)
 {
     JokeCategoryDetailDao jokeCategory = GetJokeCategory(joke.JokeCategoryId, joke.LanguageId);
     JokeDetailDao entityDetail = new JokeDetailDao
     {
         JokeContent = joke.JokeContent,
         Language = GetLanguage(joke.LanguageId),
         JokeCategoryDetail = jokeCategory
     };
     JokeDao entity = new JokeDao
     {
         CreationDate = DateTime.UtcNow,
         JokeDetails = new List<JokeDetailDao>()
     };
     entity.JokeDetails.Add(entityDetail);
     RepositoryFactory.Context.Jokes.Add(entity);
     RepositoryFactory.Context.SaveChanges();
     return true;
 }
 private bool InsertAnotherJokeTranslation(JokeViewModel joke)
 {
     if (IsAnotherJokeTranslationToEdit(joke))
         return PutJokeContent(joke);
     LanguageDao lang = GetLanguage(joke.LanguageId);
     JokeCategoryDetailDao categoryDetail = GetJokeCategory(joke.JokeCategoryId, joke.LanguageId);
     var entity = RepositoryFactory.Context.Jokes.FirstOrDefault(x => x.Id == joke.Id);
     entity?.JokeDetails.Add(new JokeDetailDao
     {
         JokeContent = joke.JokeContent,
         Language = lang,
         JokeCategoryDetail = categoryDetail
     });
     RepositoryFactory.Context.SaveChanges();
     return true;
 }
 public bool RemoveJoke(JokeViewModel joke)
 {
     return joke.TemporarySeveralTranslationDelete
         ? RemoveAllJokeTranslation(joke)
         : RemoveSelectedJokeTranslation(joke);
 }