/// <summary> /// Get news form to create. /// </summary> /// <returns></returns> public async Task <ActionResult> GetCreateNews() { NewsFormData newsFormData = new NewsFormData { NewsDate = DateTime.Now.ToString("dd/MM/yyyy"), TranslationsList = new List <NewsTranslationFormData>() }; LanguageResultData result = await WebApiClient.GetAsync <LanguageResultData>(Constant.WebApiControllerLanguages, Constant.WebApiLanguageList); if (result != null && result.OperationSuccess && result.LanguageDtoList != null) { foreach (var language in result.LanguageDtoList) { var translation = new NewsTranslationFormData { LanguagePrefix = language.LanguagePrefix, LanguageId = language.LanguageId, }; newsFormData.TranslationsList.Add(translation); } } ViewBag.Action = "CreateNews"; return(PartialView("Partials/_ManageNews", newsFormData)); }
/// <summary> /// From FORMDATA To REQUESTDATA /// </summary> /// <param name="newsFormData"></param> /// <returns></returns> public static NewsRequestData ToRequestData(this NewsFormData newsFormData) { if (newsFormData == null) { return(new NewsRequestData()); } return(new NewsRequestData { NewsDto = newsFormData.ToItemData() }); }
/// <summary> /// FROM ITEM DATA TO FORM DATA. /// </summary> /// <param name="itemData">the itemData to convert</param> /// <returns>the formdata result</returns> public static NewsFormData ToFormData(this NewsItemData itemData) { if (itemData == null) { return(new NewsFormData()); } NewsFormData formData = new NewsFormData { NewsId = itemData.NewsId, NewsDate = itemData.NewsDate.ToString("dd/MM/yyyy") }; return(formData); }
/// <summary> /// FROM FROMDATA TO ITEMDATA. /// </summary> /// <param name="formData">the formdata to convert</param> /// <returns>the item data.</returns> public static NewsItemData ToItemData(this NewsFormData formData) { if (formData == null) { return(new NewsItemData()); } NewsItemData itemData = new NewsItemData { NewsImage = formData.NewsImage?.FileName, NewsId = formData.NewsId, NewsDate = new DateTime(int.Parse(formData.NewsDate.Split('/')[2]), int.Parse(formData.NewsDate.Split('/')[1]), int.Parse(formData.NewsDate.Split('/')[0])) }; return(itemData); }
/// <summary> /// validate create news. /// </summary> /// <param name="newsFormData">the newsFormData to validate.</param> /// <returns>true if the proportises are valide.</returns> public bool ValidateNewsFormData(NewsFormData newsFormData) { List <string> errors = new List <string>(); if (newsFormData == null) { errors.Add(SharedResources.NullFormData); } else { errors.AddRange( GenericValidationAttribute <NewsFormData> .ValidateAttributes("NewsImage", newsFormData.NewsImage?.ToString())); errors.AddRange( GenericValidationAttribute <NewsFormData> .ValidateAttributes("NewsDate", newsFormData.NewsDate)); } return(errors.Count == 0); }
/// <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)); }