ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Instagram 클론 -2) status, post, comment모델 만들기
    Spring 2021. 6. 12. 00:20

     

    user에 이어 이제 status모델을 만들자. 

     

     

    먼저 status entity를 만든다. 

    entity에 Status자바 클래스를 만들어준다. 

     

    자동생성되는 id를 만들어주고 나서, 필요한 요소들을 만들고 

    기본 생성자와 요소 생성자, getter, setter도 만들어준다. 

    package com.yunhalee.Instagram_App.Entity;
    
    import java.sql.Timestamp;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity(name = "Status")
    public class Status {
    	
    	@Id
    	@GeneratedValue
    	private int id;
    	
    	private String statusId;
    	private String userId;
    	private String path;
    	private Timestamp timestamp;
    	
    	public Status() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    
    	public Status(int id, String statusId, String userId, String path, Timestamp timestamp) {
    		super();
    		this.id = id;
    		this.statusId = statusId;
    		this.userId = userId;
    		this.path = path;
    		this.timestamp = timestamp;
    	}
    
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	public String getStatusId() {
    		return statusId;
    	}
    
    	public void setStatusId(String statusId) {
    		this.statusId = statusId;
    	}
    
    	public String getUserId() {
    		return userId;
    	}
    
    	public void setUserId(String userId) {
    		this.userId = userId;
    	}
    
    	public String getPath() {
    		return path;
    	}
    
    	public void setPath(String path) {
    		this.path = path;
    	}
    
    	public Timestamp getTimestamp() {
    		return timestamp;
    	}
    
    	public void setTimestamp(Timestamp timestamp) {
    		this.timestamp = timestamp;
    	}
    	
    	
    	
    	
    	
    }

     

     

    이제 controller도 만들어주자. 

    StatusController자바 클래스를 만든다. 

    package com.yunhalee.Instagram_App.Controller;
    
    import java.util.ArrayList;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.yunhalee.Instagram_App.Entity.Status;
    
    @RestController
    @RequestMapping("/status")
    public class StatusController {
    	
    	@PostMapping("")
    	private Status submitStatus(@RequestBody Status status) {
    		return new Status();
    	}
    	
    	@GetMapping("")
    	private ArrayList<Status> getAllStatus(){
    		return new ArrayList<Status>();
    	}
    
    }
    

     

     

     

    이제 서비스도 만들어주자. 

    StatusService.java

     

    package com.yunhalee.Instagram_App.Service;
    
    import java.util.ArrayList;
    
    import org.springframework.stereotype.Service;
    
    import com.yunhalee.Instagram_App.Entity.Status;
    
    @Service
    public class StatusService {
    	
    	public Status submitDataIntoDB(Status status) {
    		return new Status();
    	}
    	
    	public ArrayList<Status> retrieveStatus(){
    		return new ArrayList<Status>();
    	}
    }
    

     

     

    controller에 서비스를 추가해주자. 

    import java.util.ArrayList;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.yunhalee.Instagram_App.Entity.Status;
    import com.yunhalee.Instagram_App.Service.StatusService;
    
    @RestController
    @RequestMapping("/status")
    public class StatusController {
    	
    	@Autowired
    	StatusService statusService;
    	
    	@PostMapping("")
    	private Status submitStatus(@RequestBody Status status) {
    		return statusService.submitDataIntoDB(status);
    	}
    	
    	@GetMapping("")
    	private ArrayList<Status> getAllStatus(){
    		return statusService.retrieveStatus();
    	}
    
    }
    

     

    repository만들어주자. 

    package com.yunhalee.Instagram_App.Repository;
    
    import java.util.ArrayList;
    
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.stereotype.Repository;
    
    import com.yunhalee.Instagram_App.Entity.Status;
    
    @Repository
    public interface StatusRepository extends CrudRepository<Status, Integer>{
    	
    	Status save(Status status);
    	
    	ArrayList<Status> findAll(); 
    
    }
    

     

     

    service에 repository를 추가해주자. 

    package com.yunhalee.Instagram_App.Service;
    
    import java.util.ArrayList;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.yunhalee.Instagram_App.Entity.Status;
    import com.yunhalee.Instagram_App.Repository.StatusRepository;
    
    @Service
    public class StatusService {
    	
    	@Autowired
    	StatusRepository statusRepository;
    	
    	public Status submitDataIntoDB(Status status) {
    		return statusRepository.save(status);
    	}
    	
    	public ArrayList<Status> retrieveStatus(){
    		return statusRepository.findAll();
    	}
    }
    

     

     

    postman을 가지고 제대로 작동하는지 확인하자. 

     

    정상작동한다. 

     

     

    여기서 status는 유저네임이 없는데,

    userId를 이용해서 status리스트를 가져올때, user엔티티에서 이름 정보를 가져오도록 하자. 

     

    status 엔티티에 userName항목을 추가해주자. 

    getter와 setter만 설정해주고 생성자는만들지 않는다. 

    package com.yunhalee.Instagram_App.Entity;
    
    import java.sql.Timestamp;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity(name = "Status")
    public class Status {
    	
    	@Id
    	@GeneratedValue
    	private int id;
    	
    	private String statusId;
    	private String userId;
    	private String path;
    	private String userName;
    	private Timestamp timestamp;
    	
    	public Status() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    
    	public Status(int id, String statusId, String userId, String path, Timestamp timestamp) {
    		super();
    		this.id = id;
    		this.statusId = statusId;
    		this.userId = userId;
    		this.path = path;
    		this.userName = userName;
    		this.timestamp = timestamp;
    	}
    
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	public String getStatusId() {
    		return statusId;
    	}
    
    	public void setStatusId(String statusId) {
    		this.statusId = statusId;
    	}
    
    	public String getUserId() {
    		return userId;
    	}
    
    	public void setUserId(String userId) {
    		this.userId = userId;
    	}
    
    	public String getPath() {
    		return path;
    	}
    
    	public void setPath(String path) {
    		this.path = path;
    	}
    
    	public Timestamp getTimestamp() {
    		return timestamp;
    	}
    
    	public void setTimestamp(Timestamp timestamp) {
    		this.timestamp = timestamp;
    	}
    	
    	public String getUserName() {
    		return userName;
    	}
    
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    	
    	
    	
    	
    }

     

     

    그다음 status service에서 userservice를 이용해서 유저네임 정보를 가져와서 

    리스트를 반환할때 같이반환해주도록 설정하자. 

    package com.yunhalee.Instagram_App.Service;
    
    import java.util.ArrayList;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.yunhalee.Instagram_App.Entity.Status;
    import com.yunhalee.Instagram_App.Repository.StatusRepository;
    
    @Service
    public class StatusService {
    	
    	@Autowired
    	UserService userService;
    	
    	@Autowired
    	StatusRepository statusRepository;
    	
    	public Status submitDataIntoDB(Status status) {
    		return statusRepository.save(status);
    	}
    	
    	public ArrayList<Status> retrieveStatus(){
    		
    	ArrayList<Status> statusList = statusRepository.findAll();
    	
    	for(int i=0; i<statusList.size(); i++) {
    		Status statusItem = statusList.get(i);
    		statusItem.setUserName(userService.displayUserMetaData(statusItem.getUserId()).getUserName());
    		
    	}
    	return statusList;
    	}
    }

     

    이제 post 모델을 만들자. 

    package com.yunhalee.Instagram_App.Entity;
    
    import java.sql.Timestamp;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity(name="Post")
    public class Post {
    	
    	@Id
    	@GeneratedValue
    	private int id;
    	
    	private String postId;
    	private String userId;
    	private String postPath;
    	private Timestamp tiemStamp;
    	private int likeCount;
    	public Post() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    	public Post(int id, String postId, String userId, String postPath, Timestamp tiemStamp, int likeCount) {
    		super();
    		this.id = id;
    		this.postId = postId;
    		this.userId = userId;
    		this.postPath = postPath;
    		this.tiemStamp = tiemStamp;
    		this.likeCount = likeCount;
    	}
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getPostId() {
    		return postId;
    	}
    	public void setPostId(String postId) {
    		this.postId = postId;
    	}
    	public String getUserId() {
    		return userId;
    	}
    	public void setUserId(String userId) {
    		this.userId = userId;
    	}
    	public String getPostPath() {
    		return postPath;
    	}
    	public void setPostPath(String postPath) {
    		this.postPath = postPath;
    	}
    	public Timestamp getTiemStamp() {
    		return tiemStamp;
    	}
    	public void setTiemStamp(Timestamp tiemStamp) {
    		this.tiemStamp = tiemStamp;
    	}
    	public int getLikeCount() {
    		return likeCount;
    	}
    	public void setLikeCount(int likeCount) {
    		this.likeCount = likeCount;
    	}
    	
    	
    	
    }

     

    controller

    PostController

    package com.yunhalee.Instagram_App.Controller;
    import java.util.ArrayList;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.yunhalee.Instagram_App.Entity.Post;
    
    @RestController
    @RequestMapping("/post")
    public class PostController {
    	
    	@PostMapping("")
    	private Post submitUserPost(@RequestBody Post post){
    		return new Post(post);
    	}
    	
    	@GetMapping("")
    	private ArrayList<Post> getAllPost(){
    		return new ArrayList<Post>();
    	}
    }
    

     

     

    service 도 만들어주자. 

     

    PostService.java

    package com.yunhalee.Instagram_App.Service;
    
    import java.util.ArrayList;
    
    import org.springframework.stereotype.Service;
    
    import com.yunhalee.Instagram_App.Entity.Post;
    
    @Service
    public class PostService {
    	
    	public Post submitPostToDatabase(Post post) {
    		return new Post();
    	}
    	
    	public ArrayList<Post> retrivePostFromDB(){
    		return new ArrayList<Post>();
    	}
    
    }
    

     

    controller에 추가해주자. 

    package com.yunhalee.Instagram_App.Controller;
    import java.util.ArrayList;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.yunhalee.Instagram_App.Entity.Post;
    import com.yunhalee.Instagram_App.Service.PostService;
    
    @RestController
    @RequestMapping("/post")
    public class PostController {
    	
    	@Autowired
    	PostService postService;
    	
    	@PostMapping("")
    	private Post submitUserPost(@RequestBody Post post){
    		return postService.submitPostToDatabase(post);
    	}
    	
    	@GetMapping("")
    	private ArrayList<Post> getAllPost(){
    		return postService.retrivePostFromDB();
    	}
    }
    

     

     

    PostRepository.java

    package com.yunhalee.Instagram_App.Repository;
    
    import java.util.ArrayList;
    
    import org.springframework.data.repository.CrudRepository;
    
    import com.yunhalee.Instagram_App.Entity.Post;
    
    public interface PostRepository extends CrudRepository<Post, Integer> {
    	
    	Post save(Post post);
    	ArrayList<Post> findAll();
    	
    }
    

     

    Service에 추가해주자. 

    package com.yunhalee.Instagram_App.Service;
    
    import java.util.ArrayList;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.yunhalee.Instagram_App.Entity.Post;
    import com.yunhalee.Instagram_App.Repository.PostRepository;
    
    @Service
    public class PostService {
    	
    	@Autowired
    	PostRepository postRepository;
    	
    	public Post submitPostToDatabase(Post post) {
    		return postRepository.save(post);
    	}
    	
    	public ArrayList<Post> retrivePostFromDB(){
    		return postRepository.findAll();
    	}
    
    }
    

     

    이제 userId 를 이용해서 

    user정보를 가져와보자. 

     

     

    post엔티티에 userName을 추가해주자. 

    (생성자는 말고, getter, setter까지만)

    package com.yunhalee.Instagram_App.Entity;
    
    import java.sql.Timestamp;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity(name="Post")
    public class Post {
    	
    	@Id
    	@GeneratedValue
    	private int id;
    	
    	private String postId;
    	private String userId;
    	private String userName;
    	private String postPath;
    	private Timestamp timeStamp;
    	private int likeCount;
    	public Post() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    	public Post(int id, String postId, String userId, String postPath, Timestamp timeStamp, int likeCount) {
    		super();
    		this.id = id;
    		this.postId = postId;
    		this.userId = userId;
    		this.postPath = postPath;
    		this.timeStamp = timeStamp;
    		this.likeCount = likeCount;
    	}
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getPostId() {
    		return postId;
    	}
    	public void setPostId(String postId) {
    		this.postId = postId;
    	}
    	public String getUserId() {
    		return userId;
    	}
    	public void setUserId(String userId) {
    		this.userId = userId;
    	}
    	public String getPostPath() {
    		return postPath;
    	}
    	public void setPostPath(String postPath) {
    		this.postPath = postPath;
    	}
    	public Timestamp getTimeStamp() {
    		return timeStamp;
    	}
    	public void setTimeStamp(Timestamp timeStamp) {
    		this.timeStamp = timeStamp;
    	}
    	public int getLikeCount() {
    		return likeCount;
    	}
    	public void setLikeCount(int likeCount) {
    		this.likeCount = likeCount;
    	}
    	public String getUserName() {
    		return userName;
    	}
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    	
    	
    	
    }

    servec에서 사용자 이름도 사용하도록 바꿔주자. 

     

    package com.yunhalee.Instagram_App.Service;
    
    import java.util.ArrayList;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.yunhalee.Instagram_App.Entity.Post;
    import com.yunhalee.Instagram_App.Repository.PostRepository;
    
    @Service
    public class PostService {
    	
    	@Autowired
    	UserService userService;
    	
    	@Autowired
    	PostRepository postRepository;
    	
    	public Post submitPostToDatabase(Post post) {
    		return postRepository.save(post);
    	}
    	
    	public ArrayList<Post> retrivePostFromDB(){
    		
    		ArrayList<Post> postList = postRepository.findAll();
    		
    		for(int i=0;i<postList.size();i++) {
    			Post postItem=postList.get(i);
    			postItem.setUserName(userService.displayUserMetaData(postItem.getUserId()).getUserName());
    		}
    		return postRepository.findAll();
    	}
    
    }

     

     

    comments도 만들어주자. 

    엔티티

    package com.yunhalee.Instagram_App.Entity;
    
    import java.sql.Timestamp;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity(name="Comments")
    public class Comments {
    	
    	@Id
    	@GeneratedValue
    	private int id;
    	
    	private String commentId;
    	private String userId;
    	private String postId;
    	private Timestamp timeStamp;
    	private String comment;
    	public Comments() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    	public Comments(int id, String commentId, String userId, String postId, Timestamp timeStamp, String comment) {
    		super();
    		this.id = id;
    		this.commentId = commentId;
    		this.userId = userId;
    		this.postId = postId;
    		this.timeStamp = timeStamp;
    		this.comment = comment;
    	}
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getCommentId() {
    		return commentId;
    	}
    	public void setCommentId(String commentId) {
    		this.commentId = commentId;
    	}
    	public String getUserId() {
    		return userId;
    	}
    	public void setUserId(String userId) {
    		this.userId = userId;
    	}
    	public String getPostId() {
    		return postId;
    	}
    	public void setPostId(String postId) {
    		this.postId = postId;
    	}
    	public Timestamp getTimeStamp() {
    		return timeStamp;
    	}
    	public void setTimeStamp(Timestamp timeStamp) {
    		this.timeStamp = timeStamp;
    	}
    	public String getComment() {
    		return comment;
    	}
    	public void setComment(String comment) {
    		this.comment = comment;
    	}
    	
    	
    }

     

     

    controller

    package com.yunhalee.Instagram_App.Controller;
    
    import java.util.ArrayList;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.yunhalee.Instagram_App.Entity.Comments;
    
    @RestController
    @RequestMapping("/comments")
    public class CommentsController {
    	
    	@PostMapping("")
    	private Comments submitComment(@RequestBody Comments comment) {
    		return new Comments();
    	}
    	
    	@GetMapping("{postId}")
    	private ArrayList<Comments> getCommentsForPost(@PathVariable("postId") String postId){
    		return new ArrayList<Comments>();
    	}
    }
    

     

    service

     

    package com.yunhalee.Instagram_App.Service;
    
    import java.util.ArrayList;
    
    import org.springframework.stereotype.Service;
    
    import com.yunhalee.Instagram_App.Entity.Comments;
    
    @Service
    public class CommentsService {
    
    	public Comments submitCommentToDB(Comments comment) {
    		
    	}
    	
    	public ArrayList<Comments> getAllCommentsForDB(String postId){
    		
    	}
    }
    

     

     

    controller에 추가

    import java.util.ArrayList;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.yunhalee.Instagram_App.Entity.Comments;
    import com.yunhalee.Instagram_App.Service.CommentsService;
    
    @RestController
    @RequestMapping("/comments")
    public class CommentsController {
    	
    	@Autowired
    	CommentsService commentsService;
    	
    	@PostMapping("")
    	private Comments submitComment(@RequestBody Comments comment) {
    		return commentsService.submitCommentToDB(comment);
    	}
    	
    	@GetMapping("{postId}")
    	private ArrayList<Comments> getCommentsForPost(@PathVariable("postId") String postId){
    		return commentsService.getAllCommentsForDB(postId);
    	}
    }

     

    repository

    package com.yunhalee.Instagram_App.Repository;
    
    import java.util.ArrayList;
    
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.stereotype.Repository;
    
    import com.yunhalee.Instagram_App.Entity.Comments;
    
    @Repository
    public interface CommentsRepository extends CrudRepository<Comments, Integer>{
    	
    	Comments save(Comments comment);
    	ArrayList<Comments> findAllByPostId(String postId);
    }
    

     

     

    service에 추가

    package com.yunhalee.Instagram_App.Service;
    
    import java.util.ArrayList;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.yunhalee.Instagram_App.Entity.Comments;
    import com.yunhalee.Instagram_App.Entity.Post;
    import com.yunhalee.Instagram_App.Repository.CommentsRepository;
    
    @Service
    public class CommentsService {
    	
    	@Autowired
    	UserService userService;
    	
    	@Autowired
    	CommentsRepository commentsRepository;
    
    	public Comments submitCommentToDB(Comments comment) {
    		return commentsRepository.save(comment);
    	}
    	
    	public ArrayList<Comments> getAllCommentsForDB(String postId){
    		ArrayList<Comments> commentsList = commentsRepository.findAllByPostId(postId);
    		
    		for(int i=0;i<commentsList.size();i++) {
    			Comments commentItem=commentsList.get(i);
    			commentItem.setUserName(userService.displayUserMetaData(commentItem.getUserId()).getUserName());
    		}
    		return commentsList;
    	}
    }
    

     

     

    comment엔티티에 username 추가해준다. 

    (getter setter도 )

    package com.yunhalee.Instagram_App.Entity;
    
    import java.sql.Timestamp;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity(name="Comments")
    public class Comments {
    	
    	@Id
    	@GeneratedValue
    	private int id;
    	
    	private String commentId;
    	private String userId;
    	private String userName;
    	private String postId;
    	private Timestamp timeStamp;
    	private String comment;
    	public Comments() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    	public Comments(int id, String commentId, String userId, String postId, Timestamp timeStamp, String comment) {
    		super();
    		this.id = id;
    		this.commentId = commentId;
    		this.userId = userId;
    		this.postId = postId;
    		this.timeStamp = timeStamp;
    		this.comment = comment;
    	}
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getCommentId() {
    		return commentId;
    	}
    	public void setCommentId(String commentId) {
    		this.commentId = commentId;
    	}
    	public String getUserId() {
    		return userId;
    	}
    	public void setUserId(String userId) {
    		this.userId = userId;
    	}
    	public String getPostId() {
    		return postId;
    	}
    	public void setPostId(String postId) {
    		this.postId = postId;
    	}
    	public Timestamp getTimeStamp() {
    		return timeStamp;
    	}
    	public void setTimeStamp(Timestamp timeStamp) {
    		this.timeStamp = timeStamp;
    	}
    	public String getComment() {
    		return comment;
    	}
    	public void setComment(String comment) {
    		this.comment = comment;
    	}
    	public String getUserName() {
    		return userName;
    	}
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    	
    	
    }

     

     

     

     

     

     

Designed by Tistory.