Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.simplecoding.social.auth.SecurityService;
import com.simplecoding.social.auth.models.UserDto;
import com.simplecoding.social.dtos.PostDto;
import com.simplecoding.social.model.Post;
import com.simplecoding.social.exceptions.UnauthorizedException;
import com.simplecoding.social.model.User;
import com.simplecoding.social.repo.UserRepository;
import com.simplecoding.social.service.FriendService;
import com.simplecoding.social.service.PostService;
import com.simplecoding.social.service.RoomService;
import com.simplecoding.social.service.UserService;
import org.modelmapper.ModelMapper;
Expand All @@ -15,6 +18,8 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Null;
import java.net.URI;
import java.util.List;

@RestController
Expand All @@ -28,6 +33,9 @@ public class PrivateEndpoint {
@Autowired
FriendService friendService;

@Autowired
PostService postService;

@Autowired
RoomService roomService;

Expand Down Expand Up @@ -73,6 +81,19 @@ public ResponseEntity<List<User>> getFriends() {
return new ResponseEntity<List<User>>(myFriends, HttpStatus.OK);
}

@PostMapping("addpost")
public ResponseEntity<?> addPost(@RequestBody Post post) throws NullPointerException {
UserDto user = securityService.getUser();
Post savedPost = postService.savePost(user,post.getContent());
return ResponseEntity.created(URI.create("/private/mypost")).body(savedPost);
}

@GetMapping("mypost")
public ResponseEntity<?> myPosts() throws NullPointerException {
User user=userService.getUser(securityService.getUser().getEmail());
List<PostDto> postList = postService.getPostsOfUser(user.getId());
return ResponseEntity.ok(postList);
}
@GetMapping("getRoomName")
public ResponseEntity<String> getRoom(@RequestParam("friendId")int friendId) {
UserDto currentUser = securityService.getUser();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.simplecoding.social.controller;

import com.simplecoding.social.dtos.PostDto;
import com.simplecoding.social.model.Post;
import com.simplecoding.social.model.User;
import com.simplecoding.social.repo.UserRepository;
import com.simplecoding.social.service.PostService;
import com.simplecoding.social.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

Expand All @@ -19,6 +20,12 @@ public class PublicEndpoints {
@Autowired
UserService userService;

@Autowired
PostService postService;

@Autowired
UserRepository userRepository;

@GetMapping("test")
ResponseEntity<String> getPublic() {
return ResponseEntity.ok("OK");
Expand All @@ -29,4 +36,21 @@ public ResponseEntity<?> getAllUsers() {
List<User> allUsers = userService.getAllUsers();
return ResponseEntity.ok(allUsers);
}
@GetMapping("/user-details/{id}")
public ResponseEntity<?> getdetailsbyid(@PathVariable Integer id){
User user = userRepository.findUserById(id);
return ResponseEntity.ok(user);
}

@GetMapping("posts")
public ResponseEntity<List<Post>> getAllPosts(){
List<Post> postList = postService.getAllPost();
return ResponseEntity.ok(postList);
}
@GetMapping("/{userId}/posts")
public ResponseEntity<?> getPostofUser(@PathVariable Integer userId){
List<PostDto> postList = postService.getPostsOfUser(userId);
return ResponseEntity.ok(postList);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.simplecoding.social.dtos;

public class PostDto {
Integer id;
String content;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.simplecoding.social.model;

import javax.persistence.*;

@Entity
@Table(name = "posts")
public class Post {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id",referencedColumnName = "id")
private User user;

private String content;

public Post() {
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.simplecoding.social.repo;

import com.simplecoding.social.model.Post;
import com.simplecoding.social.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PostRepository extends JpaRepository<Post,Integer> {

List<Post> findPostByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.simplecoding.social.service;

import com.simplecoding.social.auth.SecurityService;
import com.simplecoding.social.auth.models.UserDto;
import com.simplecoding.social.dtos.PostDto;
import com.simplecoding.social.model.Post;
import com.simplecoding.social.model.User;
import com.simplecoding.social.repo.PostRepository;
import com.simplecoding.social.repo.UserRepository;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PostService {

@Autowired
PostRepository postRepository;

@Autowired
UserRepository userRepository;

@Autowired
ModelMapper modelMapper;

@Autowired
SecurityService securityService;

public Post savePost(UserDto userDto, String content){
Post post = new Post();
User user = userRepository.findUserByEmail(userDto.getEmail());
post.setUser(user);
post.setContent(content);
return postRepository.save(post);
}

public List<PostDto> getPostsOfUser(Integer userId){
List<Post> postList= postRepository.findPostByUser(userRepository.findUserById(userId));
List<PostDto> postDtoList= new ArrayList<>();
for (Post post :postList) {
postDtoList.add(modelMapper.map(post,PostDto.class));
}
return postDtoList;
}

public List<Post> getAllPost(){
return postRepository.findAll();
}

}
1 change: 0 additions & 1 deletion vue-social/.env

This file was deleted.

Loading