public DomainResponse<BooleanResult> EditArticle(Article article) { var response = new DomainResponse<BooleanResult>(); try { var articleData = _uOW.ArticleRepo.Get(filter: ai => ai.ArticleId == article.ArticleId, includeProperties: "Places").FirstOrDefault(); articleData.ArticleName = article.ArticleName ?? articleData.ArticleName; articleData.Title = article.Title ?? articleData.Title; articleData.CreatedDate = article.PublishDate != DateTime.MinValue ? article.PublishDate : articleData.CreatedDate; articleData.LastUpdatedDate = article.LastUpdatedDate != DateTime.MinValue ? article.LastUpdatedDate : articleData.LastUpdatedDate; articleData.PublishDate = article.PublishDate != DateTime.MinValue ? article.PublishDate : articleData.PublishDate; articleData.Content = article.Content; UpdateArticlePlaces(article, articleData); _uOW.ArticleRepo.Update(articleData); _uOW.Save(); } catch (Exception ex) { return response.ReturnFailResponse(new[] { ex.Message } , "There is an error trying to update data" , new BooleanResult { Success = false }); } return response.ReturnSuccessResponse(new BooleanResult { Success = true } , new[] { "Admin data has been successfully updated." } , "Admin data has been successfully updated."); }
public Article TransformToArticle() { Article articleObj = new Article(); articleObj.ArticleId = ArticleId; articleObj.ArticleName = ArticleName; articleObj.Title = Title; articleObj.Content = Content; articleObj.CreatedDate = CreatedDate; articleObj.LastUpdatedDate = LastUpdatedDate; articleObj.PublishDate = PublishDate; articleObj.Places = Places; return articleObj; }
public DomainResponse<Article> CreateNewArticle(Article newArticle) { var response = new DomainResponse<Article>(); var tempRelatedPlaces = newArticle.Places; newArticle.Places = new List<Place>(); newArticle.LastUpdatedDate = DateTime.Now; try { newArticle = _uOW.ArticleRepo.Insert(newArticle); _uOW.Save(); foreach (Place place in tempRelatedPlaces) { var tempPlace = _uOW.PlaceRepo.GetByID(place.PlaceId); newArticle.Places.Add(tempPlace); } _uOW.Save(); } catch (Exception ex) { return response.ReturnFailResponse(new[] { ex.Message } , "There is an error trying to create a new article." , null); } if (newArticle.ArticleId > 0) { return response.ReturnSuccessResponse(newArticle , new[] { "New article has been successfully created." } , "New article has been successfully created."); } else { return response.ReturnFailResponse(new[] { "Error occur while trying to create new article" } , "There is an error trying to save data" , null); } }
private void UpdateArticlePlaces(Article newArticle, Article articaleToUpdate) { var places = _uOW.PlaceRepo.Get(); var selectedIdsAry = newArticle.Places.Select(x => x.PlaceId).ToArray(); var existingIdsAry = articaleToUpdate.Places.Select(x => x.PlaceId).ToArray(); foreach (Place place in places) { if (selectedIdsAry.Contains(place.PlaceId)) { if(!existingIdsAry.Contains(place.PlaceId)) articaleToUpdate.Places.Add(place); } else { if (existingIdsAry.Contains(place.PlaceId)) articaleToUpdate.Places.Remove(place); } } }
private Place PreparePlaceViewModelToUpdate(PlaceViewModel viewModel) { var articleIds = !string.IsNullOrEmpty(viewModel.RelatedArticleIds) ? viewModel.RelatedArticleIds.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) : new string[]{}; var placeIds = !string.IsNullOrEmpty(viewModel.RelatedPlaceIds) ? viewModel.RelatedPlaceIds.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) : new string[] { }; var slideShowPicIds = !string.IsNullOrEmpty(viewModel.RelatedSlideShowPictureIds) ? viewModel.RelatedSlideShowPictureIds.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) : new string[] { }; var placeObj = viewModel.TransformToPlaceObject(); foreach (var id in articleIds) { var article = new Article { ArticleId = int.Parse(id) }; if (placeObj.Articles == null) placeObj.Articles = new List<Article>(); placeObj.Articles.Add(article); } /*foreach (var id in placeIds) { var place = new Place { PlaceId = int.Parse(id) }; if (placeObj.RelatedPlaces == null) placeObj.RelatedPlaces = new List<Place>(); placeObj.RelatedPlaces.Add(place); }*/ foreach (var id in slideShowPicIds) { var picture = new Picture { PictureId = int.Parse(id) }; if (placeObj.SlideshowPictures == null) placeObj.SlideshowPictures = new List<Picture>(); placeObj.SlideshowPictures.Add(picture); } return placeObj; }