Commit 6ef5898d authored by Shaini Thenuwara's avatar Shaini Thenuwara

create getAllPost function (backend)

parent 993bbc41
package com.cdap_2020_027.elearning.controller;
import com.cdap_2020_027.elearning.model.Post;
import com.cdap_2020_027.elearning.service.PostServiceImpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @author IT17120012 - Shaini
*/
@RestController
@CrossOrigin
public class PostController {
@Autowired
PostServiceImpl postServiceImpl;
/* get Post Details */
@RequestMapping(value = "/post", method = RequestMethod.GET)
public ResponseEntity<List<Post>> getAllPost() {
try {
List<Post> postList = postServiceImpl.getAllPost();
return new ResponseEntity<>(postList, HttpStatus.OK);
} catch (Exception ex) {
System.out.println(ex);
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
package com.cdap_2020_027.elearning.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author IT17120012 - Shaini
*/
@Entity
@Table(name = "post")
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Integer id;
@Column(name = "TITLE")
private String title;
@Column(name = "CONTENT")
private String content;
@Column(name = "IMAGE")
private String image;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
package com.cdap_2020_027.elearning.repository;
import com.cdap_2020_027.elearning.model.Post;
import org.springframework.data.jpa.repository.JpaRepository;
/**
*
* @author IT17120012 - Shaini
*/
public interface PostRepository extends JpaRepository<Post,Integer>{
}
package com.cdap_2020_027.elearning.service;
import com.cdap_2020_027.elearning.model.Post;
import com.cdap_2020_027.elearning.repository.PostRepository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* @author IT17120012 - Shaini
*/
@Service
public class PostServiceImpl {
@Autowired
PostRepository postRepository;
public List<Post> getAllPost() {
return postRepository.findAll();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment