public Task <string> CreatePostAsync(BlogPostAddItem post) { return(Task.Run(async() => { var slug = UtilityService.GenerateSlug(post.BlogPost.Title); if (await PostExistsAsync(slug)) { return null; } var newPostTags = await AddPostTags(slug, post.BlogPost.TagList); var newPost = new Entities.Post { Slug = slug, Title = post.BlogPost.Title, Description = post.BlogPost.Description, Body = post.BlogPost.Body, PostTags = newPostTags }; dbContext.Posts.Add(newPost); dbContext.SaveChanges(); return slug; })); }
private void CBoxPost_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (CBoxPost.SelectedIndex == 0) { if (CBoxPost.Text.Equals("")) { MessageBox.Show("Не может быть пустым", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error); CBoxPost.SelectedItem = null; return; } if (AppData.Context.Posts.Where(p => p.NameOfPost == CBoxPost.Text).ToList().Count != 0) { MessageBox.Show("Уже существует!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error); CBoxPost.SelectedItem = AppData.Context.Posts.Where(p => p.NameOfPost == CBoxPost.Text).ToList().FirstOrDefault(); return; } Entities.Post post = new Entities.Post() { NameOfPost = CBoxPost.Text }; AppData.Context.Posts.Add(post); AppData.Context.SaveChanges(); Update(); CBoxPost.SelectedItem = post; } }
public Task UpdatePostAsync(string slug, BlogPostUpdateItem post) { return(Task.Run(() => { var oldPost = dbContext.Posts .Include(x => x.PostTags) .FirstOrDefault(x => x.Slug == slug); if (oldPost == null) { return; } if (string.IsNullOrEmpty(post.BlogPost.Title) || post.BlogPost.Title.Equals(oldPost.Title)) { oldPost.Description = post.BlogPost.Description ?? oldPost.Description; oldPost.Body = post.BlogPost.Body ?? oldPost.Body; oldPost.UpdatedAt = DateTime.Now; dbContext.Update(oldPost); } else { List <Entities.PostTag> updatedPostTags = new(); var newSlug = UtilityService.GenerateSlug(post.BlogPost.Title); foreach (var postTag in oldPost.PostTags) { updatedPostTags.Add(new Entities.PostTag { PostSlug = newSlug, TagSlug = postTag.TagSlug }); } var newPost = new Entities.Post { Slug = newSlug, Title = post.BlogPost.Title ?? oldPost.Title, Description = post.BlogPost.Description ?? oldPost.Description, Body = post.BlogPost.Body ?? oldPost.Body, CreatedAt = oldPost.CreatedAt, //UpdatedAt = DateTime.Now, PostTags = updatedPostTags }; dbContext.Remove(oldPost); dbContext.Posts.Add(newPost); } dbContext.SaveChanges(); })); }
public void MapCommonConditionSuccessTest() { var post = new Post(2, 1, "My test post", "My test Body"); var postMapper = new PostMapper(); Entities.Post result = postMapper.Map(post); Assert.AreEqual(2, result.Id); Assert.AreEqual("My test post", result.Title); Assert.AreEqual("My test Body", result.Body); Assert.AreEqual(1, result.UserId); }
private void Update() { var rooms = AppData.Context.Rooms.ToList(); var posts = AppData.Context.Posts.ToList(); var room = new Entities.Room() { NumberOfRoom = "Добавить новый кабинет" }; var post = new Entities.Post() { NameOfPost = "Добавить новую должность" }; rooms.Insert(0, room); posts.Insert(0, post); CBoxPost.ItemsSource = posts; CBoxRoom.ItemsSource = rooms; }
public void PublishPost(Contracts.BlogPost post) { // Create Linq2Sql DataContext using (var dbx = CreateContext()) { // Build entity for blog post Entities.Post postEntity = new Entities.Post() { DateCreated = DateTime.Now, Title = post.Title, Content = post.Content, Username = post.Username }; if (post.Tags != null) { // Iterate through tags foreach (var tag in post.Tags) { // Create new Tag in databse if needed. Otherwise just get the Id. int?tagId = null; dbx.EnsureTag(tag, ref tagId); // Add PostTagMappings to the BlogPost entity. postEntity.PostTagMappings.Add(new Entities.PostTagMapping() { TagId = tagId.Value }); } } dbx.Posts.InsertOnSubmit(postEntity); // Save changes. dbx.SubmitChanges(); } }
public static ViewModel.Post ToViewModel(this Entities.Post post) => _mapper.Map <ViewModel.Post>(post);
public static ViewModel.PostTeaser ToTeaserModel(this Entities.Post post) => _mapper.Map <ViewModel.PostTeaser>(post);