public async Task <Post> Post(PostInput input) { var post = new Post(input); _repo.AddAsync(post); await _repo.SaveAsync(); return(post); }
public EntityControllerTest(ITestOutputHelper output, KWebAppFactory <StartupDbLocalization> factory) : base(output, factory) { PostRepository = Factory.Server.Host.Services.GetService <PostRepository>(); PostRepository.AddAsync(new Post { Title = "سلام", LangId = "fa-IR" }).Wait(); PostRepository.AddAsync(new Post { Title = "Hello", LangId = "en-US" }).Wait(); PostRepository.SaveAsync().Preserve(); }
public async Task <IActionResult> Create([Bind("Id,Title,Description,Image,IsEdited,Created,UserId")] Post post) { if (ModelState.IsValid) { post.Created = DateTime.Now; post.UserId = User.FindFirstValue(ClaimTypes.NameIdentifier); await repo.AddAsync(post); return(RedirectToAction(nameof(Index))); } ViewData["UserId"] = new SelectList(post.UserId); return(View(post)); }
public async Task <PostResponseModel> CreatePostAsync(CreatePostModel model) { var user = await userManager.FindByNameAsync(model.Name); await ctx.Entry(user) .Reference(r => r.Profile) .LoadAsync(); var post = new Post { Description = model.Description, Profile = ctx.Profiles.FirstOrDefault(p => p.User.UserName == model.Name), ProfileId = ctx.Profiles.FirstOrDefault(p => p.User.UserName == model.Name).Id, PostImages = new List <PostImage>(), PostSongs = new List <PostSong>(), PostVideos = new List <PostVideo>() }; if (model.PostImages != null) { foreach (var file in model.PostImages) { var image = await this.AddPostImage(file, post.Id); ctx.PostImages.Add(image); post.PostImages.Add(image); } } if (model.PostSongs != null) { foreach (var postSong in model.PostSongs) { var song = await this.AddPostSong(postSong, post.Id); ctx.PostSongs.Add(song); post.PostSongs.Add(song); } } if (model.PostVideos != null) { foreach (var video in model.PostVideos) { var postVideo = await this.AddPostVideos(video, post.Id); ctx.PostVideos.Add(postVideo); post.PostVideos.Add(postVideo); } } post.ProfileId = user.Profile.Id; post.Profile = user.Profile; post.DateOfCreation = DateTime.UtcNow; await postRepository.AddAsync(post, user); return(Ok(post)); }
public async Task <ActionResult> Create(UserPost userPost) { if (ModelState.IsValid) { userPost.PostDate = DateTime.Now.ToUniversalTime(); await _repository.AddAsync(userPost); return(RedirectToAction("Index", "Home")); } ViewBag.UserId = new SelectList(Db.Users, "UserId", "Email", userPost.UserId); return(View(userPost)); }
public async Task AddAsync(string title, List <string> tags, string content, bool isDraft) { var post = new Post { Title = title, Tags = tags, Context = content, Date = DateTime.Now, EditDate = DateTime.Now, IsDraft = isDraft, PageViews = 0, UserName = "" }; await _repository.AddAsync(post); }
public async Task <IActionResult> Add(PostCreateViewModel model) { if (!ModelState.IsValid) { return(RedirectToAction("ImageValidationError", "Home")); } var user = await userManager.GetUserAsync(User); var post = mapper.Map <Post>(model); post.User = user; if (model.Image != null) { post.PhotoPath = await imageStorage.SaveAsync(model.Image); } await postRepository.AddAsync(post); return(RedirectToAction("Index")); }