/// <summary> /// Find the news by id. /// </summary> /// <param name="newsId">the news id to find.</param> /// <returns></returns> private async Task <NewsViewData> FindNews(int newsId) { NewsViewData newsViewData = new NewsViewData() { TranslationsList = new List <NewsTranslationItemData>(), News = new NewsItemData() }; NewsRequestData findNewsRequest = new NewsRequestData { NewsDto = new NewsItemData { NewsId = newsId }, FindNewsDto = FindNewsItemData.NewsId }; NewsResultData resultNews = await WebApiClient.PostFormJsonAsync <NewsRequestData, NewsResultData>(Constant.WebApiControllerNews, Constant.WebApiFindNews, findNewsRequest); if (resultNews != null && resultNews.OperationSuccess && resultNews.NewsDto != null) { newsViewData.News = resultNews.NewsDto; NewsTranslationRequestData findNewsTranslationRequest = new NewsTranslationRequestData() { NewsTranslationDto = new NewsTranslationItemData { NewsId = newsId }, FindNewsTranslationDto = FindNewsTranslationItemData.NewsId }; NewsTranslationResultData resultNewsTranslation = await WebApiClient.PostFormJsonAsync <NewsTranslationRequestData, NewsTranslationResultData>(Constant.WebApiControllerNews, Constant.WebApiFindNewsTranslations, findNewsTranslationRequest); if (resultNewsTranslation != null && resultNewsTranslation.OperationSuccess && resultNewsTranslation.NewsTranslationDtoList != null) { newsViewData.TranslationsList.Add(resultNewsTranslation.NewsTranslationDtoList.First(t => t.LanguageId == _lang)); } } return(newsViewData); }
/// <summary> /// Find the news translations /// </summary> /// <param name="newsId">the news id to search.</param> /// <returns>the transalations</returns> private async Task <List <NewsTranslationItemData> > GetNewsTranslations(int newsId) { List <NewsTranslationItemData> translationsList = new List <NewsTranslationItemData>(); NewsTranslationRequestData findNewsTranslationRequest = new NewsTranslationRequestData() { NewsTranslationDto = new NewsTranslationItemData { NewsId = newsId }, FindNewsTranslationDto = FindNewsTranslationItemData.NewsId }; NewsTranslationResultData resultNewsTranslation = await WebApiClient.PostFormJsonAsync <NewsTranslationRequestData, NewsTranslationResultData>(Constant.WebApiControllerNews, Constant.WebApiFindNewsTranslations, findNewsTranslationRequest); if (resultNewsTranslation != null && resultNewsTranslation.OperationSuccess && resultNewsTranslation.NewsTranslationDtoList != null) { translationsList.AddRange(resultNewsTranslation.NewsTranslationDtoList); } return(translationsList); }
/// <summary> /// Get News Model for Update /// </summary> /// <param name="newsId"></param> /// <returns></returns> public async Task <ActionResult> GetUpdateNews(int?newsId) { NewsFormData newsFormData = new NewsFormData { TranslationsList = new List <NewsTranslationFormData>() }; if (newsId.HasValue) { NewsRequestData findNewsRequest = new NewsRequestData { NewsDto = new NewsItemData { NewsId = newsId.Value }, FindNewsDto = FindNewsItemData.NewsId }; NewsResultData resultNews = await WebApiClient.PostFormJsonAsync <NewsRequestData, NewsResultData>(Constant.WebApiControllerNews, Constant.WebApiFindNews, findNewsRequest); if (resultNews != null && resultNews.OperationSuccess && resultNews.NewsDto != null) { newsFormData.NewsId = resultNews.NewsDto.NewsId; newsFormData.NewsDate = resultNews.NewsDto.NewsDate.ToString("dd/MM/yyyy"); NewsTranslationRequestData findNewsTranslationRequest = new NewsTranslationRequestData() { NewsTranslationDto = new NewsTranslationItemData { NewsId = newsId.Value }, FindNewsTranslationDto = FindNewsTranslationItemData.NewsId }; NewsTranslationResultData resultNewsTranslation = await WebApiClient.PostFormJsonAsync <NewsTranslationRequestData, NewsTranslationResultData>(Constant.WebApiControllerNews, Constant.WebApiFindNewsTranslations, findNewsTranslationRequest); if (resultNewsTranslation != null && resultNewsTranslation.OperationSuccess && resultNewsTranslation.NewsTranslationDtoList != null) { newsFormData.TranslationsList = resultNewsTranslation.NewsTranslationDtoList.ToFormDataList(); } } } ViewBag.Action = "UpdateNews"; return(PartialView("Partials/_ManageNews", newsFormData)); }
public async Task <OmsJsonResult> UpdateNews(NewsFormData newsFormData) { JsonReturnData data = new JsonReturnData(); if (ModelState.IsValid) { NewsRequestData request = newsFormData.ToRequestData(); NewsResultData newsResult = await WebApiClient.PostFormJsonAsync <NewsRequestData, NewsResultData>(Constant.WebApiControllerNews, Constant.WebApiUpdateNews, request); if (newsResult != null && newsResult.OperationSuccess) { NewsTranslationRequestData newsTranslationRequestData = new NewsTranslationRequestData { NewsTranslationDtoList = newsFormData.TranslationsList.ToItemDataList() }; NewsTranslationResultData newsTranslationResultData = await WebApiClient.PostFormJsonAsync <NewsTranslationRequestData, NewsTranslationResultData>(Constant.WebApiControllerNews, Constant.WebApiUpdateNewsTranslationRange, newsTranslationRequestData); if (newsTranslationResultData == null) { data.ErrorMessage = SharedResources.ServerError; data.OperationSuccess = false; } else if (!newsTranslationResultData.OperationSuccess && newsTranslationResultData.Errors != null && newsTranslationResultData.Errors.Count > 0) { data.ErrorMessage = newsTranslationResultData.Errors.First(); data.OperationSuccess = false; } else if (newsTranslationResultData.OperationSuccess) { if (!Directory.Exists(Server.MapPath($"~/Images/News/" + newsFormData.NewsId))) { Directory.CreateDirectory( Server.MapPath($"~/Images/News/" + newsFormData.NewsId)); } newsFormData.NewsImage?.SaveAs( Server.MapPath($"~/Images/News/" + newsFormData.NewsId + $"/") + newsFormData.NewsImage.FileName); data.SuccessMessage = UserResources.Ok; data.OperationSuccess = true; } } else if (newsResult == null) { data.ErrorMessage = SharedResources.ServerError; data.OperationSuccess = false; } else if (!newsResult.OperationSuccess && newsResult.Errors != null && newsResult.Errors.Count > 0) { data.ErrorMessage = newsResult.Errors.First(); data.OperationSuccess = false; } else if (newsResult.OperationSuccess) { data.SuccessMessage = UserResources.Ok; data.OperationSuccess = true; } } else { data = new JsonReturnData { ErrorMessage = NewsResources.RequiredFields, OperationSuccess = false }; } return(new OmsJsonResult(data)); }
public async Task <OmsJsonResult> CreateNews(NewsFormData newsFormData) { JsonReturnData data = new JsonReturnData(); if (ModelState.IsValid) { NewsRequestData request = newsFormData.ToRequestData(); if (request.NewsDto.NewsImage == null) { request.NewsDto.NewsImage = "default-image-news.jpg"; } NewsResultData result = await WebApiClient.PostFormJsonAsync <NewsRequestData, NewsResultData>(Constant.WebApiControllerNews, Constant.WebApiCreateNews, request); if (result != null && result.OperationSuccess && result.NewsDto != null) { foreach (var translation in newsFormData.TranslationsList.ToList()) { translation.NewsId = result.NewsDto.NewsId; } NewsTranslationRequestData translationRequest = new NewsTranslationRequestData { NewsTranslationDtoList = newsFormData.TranslationsList.ToItemDataList() }; NewsTranslationResultData newsTranslationResultData = await WebApiClient.PostFormJsonAsync <NewsTranslationRequestData, NewsTranslationResultData>( Constant.WebApiControllerNews, Constant.WebApiCreateNewsTranslationRange, translationRequest); if (newsTranslationResultData == null) { data.ErrorMessage = SharedResources.ServerError; data.OperationSuccess = false; } else if (!newsTranslationResultData.OperationSuccess && newsTranslationResultData.Errors != null && newsTranslationResultData.Errors.Count > 0) { data.ErrorMessage = newsTranslationResultData.Errors.First(); data.OperationSuccess = false; } else if (newsTranslationResultData.OperationSuccess) { Directory.CreateDirectory( Server.MapPath($"~/Images/News/" + result.NewsDto.NewsId)); newsFormData.NewsImage?.SaveAs( Server.MapPath($"~/Images/News/" + result.NewsDto.NewsId + $"/") + newsFormData.NewsImage.FileName); System.IO.File.Copy(Server.MapPath($"~/Images/Default/default-image-news.jpg"), Server.MapPath($"~/Images/News/" + result.NewsDto.NewsId + $"/default-image-news.jpg")); data.SuccessMessage = UserResources.Ok; data.OperationSuccess = true; } } else if (result == null) { data.ErrorMessage = SharedResources.ServerError; data.OperationSuccess = false; } else if (!result.OperationSuccess && result.Errors != null && result.Errors.Count > 0) { data.ErrorMessage = result.Errors.First(); data.OperationSuccess = false; } } else { data = new JsonReturnData { ErrorMessage = NewsResources.RequiredFields, OperationSuccess = false }; } return(new OmsJsonResult(data)); }