Commit ce3006b6 authored by Nguyen Loc's avatar Nguyen Loc

Loc's version

parent d7f1e514
......@@ -23,10 +23,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
......@@ -47,11 +47,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.security</groupId>-->
<!-- <artifactId>spring-security-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
</dependencies>
<build>
......
......@@ -2,8 +2,10 @@ package com.itsol.quantrivanphong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication
@EnableJpaAuditing
public class QuantrivanphongApplication {
public static void main(String[] args) {
......
package com.itsol.quantrivanphong.access.homepage.business;
import com.itsol.quantrivanphong.access.homepage.repository.CatalogiRepository;
import com.itsol.quantrivanphong.model.Catalogi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class CatalogiBusiness {
@Autowired
private CatalogiRepository catalogiRepository;
public Optional<Catalogi> findById(int id){
return catalogiRepository.findById(id);
}
public List<Catalogi> findAllCatalogi(){
return catalogiRepository.findAllCatalogi();
}
}
package com.itsol.quantrivanphong.access.homepage.business;
import com.itsol.quantrivanphong.access.homepage.repository.NewsRepository;
import com.itsol.quantrivanphong.exception.ResourceNotFoundException;
import com.itsol.quantrivanphong.model.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class NewsBusiness {
@Autowired
private NewsRepository newsRepository;
public List<News> findAllNews(){
return newsRepository.findAllNews();
}
// Find News By News Id
public Optional<News> findById(int newsId){
return newsRepository.findById(newsId);
}
//Find news By Employee Id
public List<News> findByEmployeeId(int employeeId){
return newsRepository.findByEmployeeId(employeeId);
}
// Find news By Catalogi
public List<News> findByCatalogiId(int catalogiId){
return newsRepository.findByCatalogiId(catalogiId);
}
public Optional<News> findByIdAndEmployeeId(int id, int employeeId){
return newsRepository.findByIdAndEmployeeId(id,employeeId);
}
public Optional<News> findByIdAndAndCatalogiId(int id, int catalogiId){
return newsRepository.findByIdAndAndCatalogiId(id,catalogiId);
}
public News save(News news){
return newsRepository.save(news);
}
public News updateNews(int newsId, News newsDetails){
News news = newsRepository.findNewsById(newsId);
news.setTitle(newsDetails.getTitle());
news.setContent(newsDetails.getContent());
news.setSummary(newsDetails.getSummary());
news.setThumbnail(newsDetails.getThumbnail());
News updateNews = newsRepository.save(news);
return updateNews;
}
public String deleteNews(int employeeId,int newsId){
newsRepository.findByIdAndEmployeeId(employeeId,newsId).map(news ->{
newsRepository.delete(news);
return "ok";
}).orElseThrow(() -> new ResourceNotFoundException("News" ,"newsId",newsId));
return "Ok";
}
}
package com.itsol.quantrivanphong.access.homepage.controller;
import com.itsol.quantrivanphong.access.homepage.business.CatalogiBusiness;
import com.itsol.quantrivanphong.exception.ResourceNotFoundException;
import com.itsol.quantrivanphong.model.Catalogi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
import java.util.Optional;
public class CatalogiController {
@Autowired
private CatalogiBusiness catalogiBusiness;
@GetMapping("/catalogies")
public ResponseEntity<List<Catalogi>> getAllNews(){
return ResponseEntity.ok( catalogiBusiness.findAllCatalogi());
}
@GetMapping("/catalogies/{catalogiId}")
public Catalogi getCatalogiById(@PathVariable(value = "catalogiId") int catalogiId){
return catalogiBusiness.findById(catalogiId).orElseThrow(()-> new ResourceNotFoundException("Catalogi","catalogiId",catalogiId));
}
}
package com.itsol.quantrivanphong.access.homepage.controller;
import com.itsol.quantrivanphong.access.homepage.business.CatalogiBusiness;
import com.itsol.quantrivanphong.access.homepage.business.NewsBusiness;
import com.itsol.quantrivanphong.employee.bussiness.EmployeeBussiness;
import com.itsol.quantrivanphong.exception.ResourceNotFoundException;
import com.itsol.quantrivanphong.model.Employee;
import com.itsol.quantrivanphong.model.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/api")
public class NewsController {
@Autowired
private NewsBusiness newsBusiness;
@Autowired
private CatalogiBusiness catalogiBusiness;
@Autowired
private EmployeeBussiness employeeBusiness;
// get All news
@GetMapping("/news")
public ResponseEntity<List<News>> getAllNews(){
return ResponseEntity.ok(newsBusiness.findAllNews());
}
// get news by employeeId
@GetMapping("/news/employees/{employeeId}")
public List<News> getAllNewsByEmployeeId(@PathVariable(value = "employeeId") int employeeId) {
return newsBusiness.findByEmployeeId(employeeId);
}
// get News by catalogiId
@GetMapping("/news/catalogi/{catalogiId}")
public List<News> getAllNewsByCatalogiId(@PathVariable(value="catalogiId") int catalogiId){
return newsBusiness.findByCatalogiId(catalogiId);
}
// create news by employeesId
@PostMapping("/news/employees/{employeeId}")
public News createNews(@PathVariable (value = "employeeId") int employeeId,
@Valid @RequestBody News news) {
return employeeBusiness.findById(employeeId).map(employee -> {
news.setEmployee(employee);
return newsBusiness.save(news);
}).orElseThrow(() -> new ResourceNotFoundException("Employee" ,"employeeId",employeeId));
}
// Edit news by EmployeeId
@PutMapping("/employees/{employeeId}/news/{newsId}")
public News updateNews(@PathVariable (value = "employeeId") int employeeId,
@PathVariable (value = "newsId") int newsId,
@Valid @RequestBody News newsRequest) {
if(!employeeBusiness.findById(employeeId).isPresent()) {
throw new ResourceNotFoundException("Employee" ,"employeeId",employeeId);
}
if (!newsBusiness.findById(newsId).isPresent()) {
throw new ResourceNotFoundException("News", "id", newsId);
}
return newsBusiness.updateNews(newsId,newsRequest);
}
//delete news by employeeId and newsId
@DeleteMapping("/employees/{employeeId}/news/{newsId}")
public ResponseEntity<?> deleteNews(@PathVariable (value = "employeeId") int employeeId,
@PathVariable (value = "newsId") int newsId) {
if(!newsBusiness.findByIdAndEmployeeId(employeeId,newsId).isPresent()){
throw new ResourceNotFoundException("News","newsId",newsId);
}
return ResponseEntity.ok(newsBusiness.deleteNews(employeeId,newsId));
}
}
//package com.itsol.quantrivanphong.access.homepage.dto;
//
//import lombok.AllArgsConstructor;
//import lombok.Data;
//import lombok.NoArgsConstructor;
//
//import javax.persistence.Entity;
//
//@Entity
//@Data
//@NoArgsConstructor
//@AllArgsConstructor
//public class NewsDTO {
// private int employeeId;
// private int catalogiId;
// private int id;
// private String title;
// private String thumbnail;
// private String summary;
// private String content;
// private boolean status;
//}
package com.itsol.quantrivanphong.access.homepage.repository;
import com.itsol.quantrivanphong.model.Catalogi;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CatalogiRepository extends JpaRepository<Catalogi, Integer> {
@Query("SELECT u from Catalogi u")
List<Catalogi> findAllCatalogi();
// @Query("SELECT u FROM Catalogi u where u.id=:id")
// Catalogi findCatalogiById(int id);
}
package com.itsol.quantrivanphong.access.homepage.repository;
import com.itsol.quantrivanphong.model.News;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface NewsRepository extends JpaRepository<News, Integer> {
@Query("SELECT u from News u")
List<News> findAllNews();
@Query("SELECT u FROM News u where u.id=:id")
News findNewsById(int id);
List<News> findByEmployeeId(int employeeId);
List<News> findByCatalogiId(int catalogiId);
Optional<News> findByIdAndEmployeeId(int id,int employeeId);
Optional<News> findByIdAndAndCatalogiId(int id,int catalogiId);
}
package com.itsol.quantrivanphong.audit;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.time.Instant;
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(
value = {"createdAt", "updatedAt"},
allowGetters = true
)
public abstract class DateAudit implements Serializable {
@CreatedDate
@Column(nullable = false, updatable = false)
private Instant createdAt;
@LastModifiedDate
@Column(nullable = false)
private Instant updatedAt;
public Instant getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Instant createdAt) {
this.createdAt = createdAt;
}
public Instant getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Instant updatedAt) {
this.updatedAt = updatedAt;
}
}
package com.itsol.quantrivanphong.employee.bussiness;
import com.itsol.quantrivanphong.employee.repository.EmployeeRepository;
import com.itsol.quantrivanphong.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class EmployeeBussiness {
@Autowired
EmployeeRepository employeeRepository;
// public List<Employee> findAll(){
// return employeeRepository.findAll();
// }
//
public Optional<Employee> findById(int newsId){
return employeeRepository.findById(newsId);
}
// public User save(User user){
// return userRepository.save(user);
// }
// public User updateUser(Long newsId, User signUpDTO){
// User user = userRepository.findUserById(newsId);
// user.setUsername(signUpDTO.getUsername());
// user.setPassword(signUpDTO.getPassword());
// user.setEmail(signUpDTO.getEmail());
// user.setFullname(signUpDTO.getFullname());
// user.setImage(signUpDTO.getImage());
// user.setKnowledge(signUpDTO.getKnowledge());
// user.setUniversity(signUpDTO.getUniversity());
// user.setPhonenumber(signUpDTO.getPhonenumber());
// user.setPosition(signUpDTO.getPosition());
// user.setVillage(signUpDTO.getVillage());
// user.setYearOfGraduation(signUpDTO.getYearOfGraduation());
// user.setRole(signUpDTO.getRole());
// return userRepository.save(user);
// }
//
// public void deleteUser(Long newsId){
// userRepository.deleteById(newsId);
// }
}
//package com.itsol.quantrivanphong.employee.controller;
//
//import com.example.easynotes.exception.ResourceNotFoundException;
//import com.example.easynotes.login.model.User;
//import com.example.easynotes.users.bussiness.UserBussiness;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.*;
//
//import javax.validation.Valid;
//import java.util.List;
//
//@RestController
//@RequestMapping("/api")
//public class UserController {
//
// @Autowired
// private UserBussiness userBussiness;
//
// @GetMapping("/users")
// public List<User> getAllUser(){ return userBussiness.findAll();}
//
// @GetMapping("/users/{id}")
// public User getUserById(@PathVariable(value = "id") Long userId){
// return userBussiness.findById(userId).orElseThrow(()-> new ResourceNotFoundException("News", "id", userId));
// }
//
// @PutMapping("/users/{id}")
// public ResponseEntity<User> updateUser(@PathVariable(value = "id") Long userId,
// @Valid @RequestBody User userDetails) {
// if (!userBussiness.findById(userId).isPresent()) {
// throw new ResourceNotFoundException("User", "id", userId);
// }
// return ResponseEntity.ok(userBussiness.updateUser(userId,userDetails));
// }
//}
package com.itsol.quantrivanphong.employee.repository;
import com.itsol.quantrivanphong.model.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface EmployeeRepository extends JpaRepository<Employee,Integer> {
@Query("SELECT u FROM Employee u where u.id=:id")
Employee findEmployeeById(int id);
}
package com.itsol.quantrivanphong.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
private String resourceName;
private String fieldName;
private Object fieldValue;
public ResourceNotFoundException(String resourceName, String fieldName, Object fieldValue) {
super(String.format("%s not found with %s : '%s'", resourceName, fieldName, fieldValue));
this.resourceName = resourceName;
this.fieldName = fieldName;
this.fieldValue = fieldValue;
}
public String getResourceName() {
return resourceName;
}
public String getFieldName() {
return fieldName;
}
public Object getFieldValue() {
return fieldValue;
}
}
package com.itsol.quantrivanphong.manage.leaveform;
package com.itsol.quantrivanphong.manager.employee;
public class test {
}
package com.itsol.quantrivanphong.manage.issue;
package com.itsol.quantrivanphong.manager.news;
public class test {
}
package com.itsol.quantrivanphong.report.project;
package com.itsol.quantrivanphong.manager.project;
public class test {
}
package com.itsol.quantrivanphong.manager.reportdetail;
public class test {
}
package com.itsol.quantrivanphong.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.itsol.quantrivanphong.audit.DateAudit;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
......@@ -10,8 +13,11 @@ import javax.persistence.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"},
allowGetters = true)
@Table(name = "news")
public class News {
public class News extends DateAudit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
......
package com.itsol.quantrivanphong.report.news;
package com.itsol.quantrivanphong.report.issue;
public class test {
}
package com.itsol.quantrivanphong.manage.timesheet;
package com.itsol.quantrivanphong.report.leaveform;
public class test {
}
package com.itsol.quantrivanphong.report.reportdetail;
public class test {
}
package com.itsol.quantrivanphong.report.employee;
package com.itsol.quantrivanphong.report.timesheet;
public class test {
}
server.port=8081
#server.port=8081
# ===============================
# DATABASE CONNECTION
# ===============================
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/quantrivanphong
spring.datasource.username=root
spring.datasource.password=
spring.datasource.password=vanloc
# ===============================
# JPA / HIBERNATE
......
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