public async Task <ActionResult> AddPost(AddPostDto model) { User currentUser = await GetCurrentUser(); if (model.UserId != currentUser.UserId) { return(BadRequest(new { message = "Authentication issue, please re-log." })); } Post post = new Post(_userService.GetById(model.UserId), model.Title, model.Text); try { _postService.Add(post); } catch (Exception e) { return(BadRequest(new { message = "Error in adding post: " + e.Message })); } PostDto postDto = new PostDto { UserDto = new UserDto(currentUser.UserId, currentUser.Email, currentUser.Username, currentUser.Firstname, currentUser.Lastname), DatePosted = post.DatePosted, PostId = post.PostId, Text = post.Text, Title = post.Title }; return(Ok(postDto)); }
public IHttpActionResult PostPost(AddPostDto postDto) { if (!ModelState.IsValid) { return BadRequest(ModelState); } // Find related user var user = db.Users.FirstOrDefault(u => u.Identifier == postDto.UserIdentifier); if (user == null) { return BadRequest("Invalid user"); } // Find related image var image = db.Images.Find(postDto.ImageId); if (image == null) { return BadRequest("Image was not found"); } // Map dto to post var post = new Post { Title = postDto.Title, Image = image, User = user }; // Add to db db.Posts.Add(post); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = post.Id }, postDto); }
public async Task <ServiceResponse <List <GetPostDto> > > AddPost(AddPostDto newPost) { ServiceResponse <List <GetPostDto> > serviceResponse = new ServiceResponse <List <GetPostDto> >(); try { Post post = _mapper.Map <Post>(newPost); post.Date = DateTime.Now; if (newPost.Image.Length > 0) { string filePath = Path.Combine("wwwroot", "images", DateTime.Now.Ticks + Path.GetRandomFileName() + newPost.Image.FileName); using (var stream = System.IO.File.Create(filePath)) { await newPost.Image.CopyToAsync(stream); post.Image = filePath.Substring(8, filePath.Length - 8); } } await _context.Posts.AddAsync(post); await _context.SaveChangesAsync(); serviceResponse.Data = (_context.Posts.Select(p => _mapper.Map <GetPostDto>(p))).ToList(); } catch (System.Exception ex) { serviceResponse.Success = false; serviceResponse.Message = ex.Message; } return(serviceResponse); }
public Post FromAddPostDtoToPost(AddPostDto addPostDto) { return(new Post { Title = addPostDto.Title, Text = addPostDto.Text }); }
public void Execute(AddPostDto request) { Context.Posts.Add(new Post { UserId = request.UserId, ModelId = request.ModelId, FuelId = request.FuelId }); }
public int AddPost(string authorId, AddPostDto addPostDto) { var post = _postConverter.FromAddPostDtoToPost(addPostDto); post.AuthorId = authorId; post.Created = DateTime.UtcNow; return(_postRepository.Add(post)); }
public async Task <IActionResult> AddPost([FromForm] AddPostDto dto) { await _mediator.Publish(new AddPost { Photo = HttpContext.Request.Form.Files.Any() ? HttpContext.Request.Form.Files[0] : null, Description = dto.Description }); return(Ok()); }
public GetPostDto AddPost(AddPostDto addPostDto) { AddPostValidate(addPostDto); var post = _mapper.Map <Post>(addPostDto); post.Date = DateTime.Now; _dbContext.Posts.Add(post); _dbContext.SaveChanges(); return(_mapper.Map <GetPostDto>(post)); }
public async Task <ActionResult <GetPostDto> > AddPost([FromBody] AddPostDto post) { if (!ModelState.IsValid) { return(BadRequest("Invalid data.")); } await _postService.CreatePost(post); return(Ok()); }
private static void AddPostValidate(AddPostDto addPostDto) { if (addPostDto == null) { throw new ArgumentNullException(nameof(addPostDto)); } if (!addPostDto.Validate()) { throw new InvalidOperationException(); } }
public IActionResult Post([FromBody] AddPostDto dto) { try { _addPostsCommand.Execute(dto); return(StatusCode(201, "Successfully created.")); } catch { return(StatusCode(422, "An error has occurred.")); } }
public async Task <GetPostDto> AddPostAsync(AddPostDto addPostDto) { AddPostValidate(addPostDto); var post = _mapper.Map <Post>(addPostDto); post.Date = DateTime.Now; await _dbContext.AddAsync(post); await _dbContext.SaveChangesAsync(); return(_mapper.Map <GetPostDto>(post)); }
public IActionResult Put(int id, [FromBody] AddPostDto dto) { try { dto.Id = id; _editPostsCommand.Execute(dto); return(StatusCode(204, "Successfully edited.")); } catch { return(StatusCode(422, "An error has occurred.")); } }
public async Task <ServiceResponse <Post> > UpdatePost(int postId, AddPostDto postToBeUpdated) { ServiceResponse <Post> response = new ServiceResponse <Post>(); try { var post = await FindPostByIdAsync(postId); if (post == null) { response.Message = "Post not found"; } post.Title = postToBeUpdated.Title; post.Summary = postToBeUpdated.Summary; post.Body = postToBeUpdated.Body; post.LastModified = postToBeUpdated.LastModified; if (!String.IsNullOrEmpty(postToBeUpdated.Category)) { var query = await _categoryService.FindCategoryByName(postToBeUpdated.Category); if (!query.Success) { response.Message = "There has been a problem retrieving the category"; } var category = query.Data.FirstOrDefault(); var currentPostCategory = post.PostCategories .FirstOrDefault(); post.PostCategories.Remove(currentPostCategory); var postCategory = new PostCategories { Post = post, Category = category }; _context.PostCategories.Update(postCategory); } await _context.SaveChangesAsync(); response.Data = _mapper.Map <Post>(post); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public Post ConvertToPost(AddPostDto dto) { if (dto == null) { throw new ArgumentNullException(nameof(dto)); } return(new Post { AuthorId = dto.AuthorId, Title = dto.Title, Text = dto.Text }); }
public void Execute(AddPostDto request) { var post = Context.Posts.Find(request.Id); if (post == null) { throw new EntityNotFoundException(); } post.UserId = request.UserId; post.ModelId = request.ModelId; post.FuelId = request.FuelId; Context.SaveChanges(); }
public async Task <ActionResult <GetPostDto> > CreatePost(AddPostDto addPostDto) { GetPostDto getPostDto = null; try { getPostDto = await _postService.AddPostAsync(addPostDto); } catch { return(BadRequest()); } return(CreatedAtRoute(nameof(GetPostById), new { id = getPostDto.Id }, getPostDto)); }
public bool AddPost(AddPostDto model) { _context.Posts.Add(new Entities.Post { Title = model.Title, Body = model.Body, Created = DateTime.Now, LikesCount = 0, Id = Guid.NewGuid() }); _context.SaveChanges(); return(true); }
public async Task <ActionResult <PostDto> > PostPost(AddPostDto post) { var userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); var topic = await _context.Topics.FirstOrDefaultAsync(topic => topic.Name == post.Topic); var newPost = new Post() { PostId = Guid.NewGuid().ToString(), Title = post.Title, Body = post.Body, TopicId = topic.TopicId, UserId = userId, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }; _context.Posts.Add(newPost); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (PostExists(newPost.PostId)) { return(Conflict()); } else { throw; } } var postDto = new PostDto() { PostId = newPost.PostId, Title = newPost.Title, Body = newPost.Body, Topic = newPost.Topic.Name, UserId = newPost.UserId, Author = newPost.User.FullName, CreatedAt = newPost.CreatedAt.ToString("yyyy-MM-dd"), }; return(CreatedAtAction("GetPost", postDto)); }
public async Task <IActionResult> AddNewPost(AddPostDto addPostDto) { Post post = new Post(); post.Title = addPostDto.Title; post.Description = addPostDto.Description; post.UserId = 1; post.PostedDate = DateTime.Now; post.PostCategories = new List <PostCategory>(); foreach (var item in addPostDto.CategoryIds) { PostCategory postCategory = new PostCategory(); postCategory.CategoryId = item; post.PostCategories.Add(postCategory); } await genericDal.AddAsync(post); return(Ok()); }
public async Task <IActionResult> AddPost(AddPostDto addPostDto) { List <PostTag> postTags = await _tagRepo.GetTagsAndUpdateAmmountAdding(addPostDto.Description); Post post = new Post { UserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value), Description = addPostDto.Description, CreateAt = DateTime.Now, Photos = addPostDto.Photos, PostTags = postTags }; _repo.Add(post); await _repo.SaveAll(); return(StatusCode(201)); }
public ActionResult Post([FromBody] AddPostDto model) { if (!ModelState.IsValid) { return(Ok(new { status = 0, Result = "", Error = "Validation Problem" })); } var methodResult = _postService.AddPost(model); return(Ok(new { Status = 1, Result = methodResult, Error = "" })); }
public async Task <ActionResult <GetPostDto> > PutPost(int id, [FromBody] AddPostDto post) { await _postService.UpdatePost(id, post); return(Ok()); }
public async Task <ServiceResponse <Post> > CreatePost(AddPostDto newPost) { ServiceResponse <Post> response = new ServiceResponse <Post>(); try { var author = await _authorService.FindAuthorById(newPost.AuthorId); var post = new Post() { Title = newPost.Title, Summary = newPost.Summary, Body = newPost.Body, LastModified = newPost.LastModified }; _context.Posts.Add(post); post.Author = author; await _context.SaveChangesAsync(); //now checks which category has been specified if (!string.IsNullOrEmpty(newPost.Category)) { ////finds the category object that corresponds to the category name received ////just one category is added from the frontend var query = await _categoryService.FindCategoryByName(newPost.Category); if (!query.Success) { response.Message = "There has been a problem retrieving the category"; } var category = query.Data.FirstOrDefault(); var postCategory = new PostCategories { Post = post, Category = category }; _context.PostCategories.Add(postCategory); } else { // //assign the default category as General that is the category with Id 1 var category = await _context.Categories.Where(c => c.CategoryId == 1) .ToListAsync(); var postCategory = new PostCategories { Post = post, Category = category.FirstOrDefault() }; _context.PostCategories.Add(postCategory); } await _context.SaveChangesAsync(); response.Data = _mapper.Map <Post>(newPost); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public async Task <IActionResult> AddPost([FromForm] AddPostDto newPost) { return(Ok(await _postService.AddPost(newPost))); }
public async Task <int> AddPost(AddPostDto postDto) { var post = _blogConverter.ConvertToPost(postDto); return(await _blogRepository.Add(post)); }