Compare commits
16 Commits
product_re
...
master
Author | SHA1 | Date | |
---|---|---|---|
0bc0217bac | |||
eb9ac2cd79 | |||
178ae865df | |||
|
889fdb2459 | ||
|
d761ce30f1 | ||
7e29dca769 | |||
d79ad3eab8 | |||
f14bede40b | |||
0121fd4bcb | |||
50d6479c8d | |||
9ee0199b50 | |||
017e4c1643 | |||
232662adea | |||
73e0c0df14 | |||
b1dfb6322e | |||
f9caf17a18 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -5,6 +5,9 @@ build/
|
|||||||
!**/src/main/**
|
!**/src/main/**
|
||||||
!**/src/test/**
|
!**/src/test/**
|
||||||
|
|
||||||
|
application-mysql.yml
|
||||||
|
application-postgres.yml
|
||||||
|
|
||||||
### STS ###
|
### STS ###
|
||||||
.apt_generated
|
.apt_generated
|
||||||
.classpath
|
.classpath
|
||||||
|
8
README.md
Normal file
8
README.md
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
### Backend Classrom Demo
|
||||||
|
CUBETIQ Backend Training
|
||||||
|
|
||||||
|
### TODO
|
||||||
|
- [x] Entity
|
||||||
|
- [x] Repository
|
||||||
|
- [ ] Service
|
||||||
|
- [x] Controller
|
@ -13,11 +13,14 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
|
||||||
|
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
implementation 'io.springfox:springfox-swagger2:2.9.2'
|
implementation 'io.springfox:springfox-swagger2:2.9.2'
|
||||||
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
|
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
|
||||||
|
|
||||||
|
runtimeOnly 'mysql:mysql-connector-java'
|
||||||
runtimeOnly 'org.postgresql:postgresql'
|
runtimeOnly 'org.postgresql:postgresql'
|
||||||
compileOnly 'org.projectlombok:lombok'
|
compileOnly 'org.projectlombok:lombok'
|
||||||
annotationProcessor 'org.projectlombok:lombok'
|
annotationProcessor 'org.projectlombok:lombok'
|
||||||
|
5
homework/Home.md
Normal file
5
homework/Home.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
### Homework
|
||||||
|
|
||||||
|
- [ ] Create project all functions from start
|
||||||
|
- [ ] Add functions into BaseService (CRUD)
|
||||||
|
- [ ] Create BaseController
|
5
src/main/java/com/cubetiqs/demo/Constants.java
Normal file
5
src/main/java/com/cubetiqs/demo/Constants.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package com.cubetiqs.demo;
|
||||||
|
|
||||||
|
public final class Constants {
|
||||||
|
public static final String STATUS = "status";
|
||||||
|
}
|
@ -1,13 +1,24 @@
|
|||||||
package com.cubetiqs.demo;
|
package com.cubetiqs.demo;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
|
import com.cubetiqs.demo.rest.UserController;
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class DemoApplication {
|
public class DemoApplication implements CommandLineRunner {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(DemoApplication.class, args);
|
SpringApplication.run(DemoApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(String... args) throws Exception {
|
||||||
|
UserEntity userEntity = new UserEntity();
|
||||||
|
userEntity.setEmail("a@gm.com");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.cubetiqs.demo.config;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.repository.BaseRepositoryImpl;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableJpaRepositories(repositoryBaseClass = BaseRepositoryImpl.class, basePackages = "com.cubetiqs.demo.repository")
|
||||||
|
public class BaseRepositoryConfig {
|
||||||
|
}
|
60
src/main/java/com/cubetiqs/demo/domain/BaseEntity.java
Normal file
60
src/main/java/com/cubetiqs/demo/domain/BaseEntity.java
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.Constants;
|
||||||
|
import org.springframework.data.domain.Persistable;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@MappedSuperclass
|
||||||
|
public class BaseEntity<ID extends Serializable> implements Serializable, Persistable<ID> {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private ID id;
|
||||||
|
|
||||||
|
@Column(name = "created_date", updatable = false)
|
||||||
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
|
private Date createdDate;
|
||||||
|
|
||||||
|
@Column(name = Constants.STATUS)
|
||||||
|
@Enumerated(EnumType.ORDINAL)
|
||||||
|
private Status status;
|
||||||
|
|
||||||
|
public void setId(ID id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ID getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isNew() {
|
||||||
|
return id == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getCreatedDate() {
|
||||||
|
return createdDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatedDate(Date createdDate) {
|
||||||
|
this.createdDate = createdDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStatus(Status status) {
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Status getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PrePersist
|
||||||
|
public void beforeSave() {
|
||||||
|
if (createdDate == null) {
|
||||||
|
createdDate = new Date();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
src/main/java/com/cubetiqs/demo/domain/CommentEntity.java
Normal file
27
src/main/java/com/cubetiqs/demo/domain/CommentEntity.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "comments")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class CommentEntity extends BaseEntity<Long> {
|
||||||
|
@Column(columnDefinition = "TEXT")
|
||||||
|
private String contents;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.DETACH})
|
||||||
|
@JoinColumn(name = "post_id")
|
||||||
|
private PostEntity post;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.DETACH})
|
||||||
|
@JoinColumn(name = "user_id")
|
||||||
|
private UserEntity user;
|
||||||
|
}
|
31
src/main/java/com/cubetiqs/demo/domain/PostEntity.java
Normal file
31
src/main/java/com/cubetiqs/demo/domain/PostEntity.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "posts")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class PostEntity extends BaseEntity<Long> {
|
||||||
|
@Column
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@Column(columnDefinition = "TEXT")
|
||||||
|
private String contents;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.DETACH})
|
||||||
|
@JoinColumn(name = "user_id")
|
||||||
|
private UserEntity user;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "post", fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, orphanRemoval = true)
|
||||||
|
private Collection<CommentEntity> comments;
|
||||||
|
}
|
||||||
|
|
@ -1,20 +0,0 @@
|
|||||||
package com.cubetiqs.demo.domain;
|
|
||||||
|
|
||||||
import lombok.*;
|
|
||||||
|
|
||||||
import javax.persistence.*;
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Table(name = "products")
|
|
||||||
@Data
|
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class Product {
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
||||||
private Long id;
|
|
||||||
private String name;
|
|
||||||
private BigDecimal price;
|
|
||||||
private Boolean deleted;
|
|
||||||
}
|
|
6
src/main/java/com/cubetiqs/demo/domain/Status.java
Normal file
6
src/main/java/com/cubetiqs/demo/domain/Status.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
public enum Status {
|
||||||
|
ACTIVE,
|
||||||
|
DEACTIVE
|
||||||
|
}
|
21
src/main/java/com/cubetiqs/demo/domain/UserEntity.java
Normal file
21
src/main/java/com/cubetiqs/demo/domain/UserEntity.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "users")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class UserEntity extends BaseEntity<Long> {
|
||||||
|
@Column(name = "email", length = 100, unique = true, nullable = false)
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@Column(length = 100)
|
||||||
|
private String password;
|
||||||
|
}
|
20
src/main/java/com/cubetiqs/demo/domain/view/UserView.java
Normal file
20
src/main/java/com/cubetiqs/demo/domain/view/UserView.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package com.cubetiqs.demo.domain.view;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonPropertyOrder(value = {
|
||||||
|
"email",
|
||||||
|
"id"
|
||||||
|
})
|
||||||
|
public class UserView implements Serializable {
|
||||||
|
private Long id;
|
||||||
|
private String email;
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.cubetiqs.demo.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.repository.NoRepositoryBean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@NoRepositoryBean
|
||||||
|
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
|
||||||
|
List<T> findAllActives();
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.cubetiqs.demo.repository;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.Constants;
|
||||||
|
import com.cubetiqs.demo.domain.Status;
|
||||||
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
|
||||||
|
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
||||||
|
import org.springframework.data.repository.NoRepositoryBean;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@NoRepositoryBean
|
||||||
|
public class BaseRepositoryImpl<T, ID extends Serializable>
|
||||||
|
extends SimpleJpaRepository<T, ID>
|
||||||
|
implements BaseRepository<T, ID> {
|
||||||
|
private final EntityManager entityManager;
|
||||||
|
public BaseRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
|
||||||
|
super(entityInformation, entityManager);
|
||||||
|
this.entityManager = entityManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<T> findAllActives() {
|
||||||
|
Specification<T> specification = (Specification<T>) (root, query, criteriaBuilder) ->
|
||||||
|
criteriaBuilder.equal(root.get(Constants.STATUS), Status.ACTIVE);
|
||||||
|
return findAll(specification);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.cubetiqs.demo.repository;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.CommentEntity;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface CommentRepository extends JpaRepository<CommentEntity, Long> {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.cubetiqs.demo.repository;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.PostEntity;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface PostRepository extends BaseRepository<PostEntity, Long> {
|
||||||
|
|
||||||
|
}
|
@ -1,8 +0,0 @@
|
|||||||
package com.cubetiqs.demo.repository;
|
|
||||||
|
|
||||||
import com.cubetiqs.demo.domain.Product;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
public interface ProductRepository extends JpaRepository<Product, Long> {
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.cubetiqs.demo.repository;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface UserRepository extends BaseRepository<UserEntity, Long> {
|
||||||
|
Optional<UserEntity> findFirstByEmail(String email);
|
||||||
|
|
||||||
|
@Query(value = "select * from users u where u.email = ?1", nativeQuery = true)
|
||||||
|
Optional<UserEntity> fetchFirstByEmail(String email);
|
||||||
|
|
||||||
|
@Query(value = "select * from users u", nativeQuery = true)
|
||||||
|
Page<UserEntity> fetchAllUsers(Pageable pageable);
|
||||||
|
|
||||||
|
@Query(value = "select * from users u where lower(u.email) like ?1", nativeQuery = true)
|
||||||
|
Page<UserEntity> searchByEmail(String likeEmail, Pageable pageable);
|
||||||
|
}
|
33
src/main/java/com/cubetiqs/demo/rest/PostController.java
Normal file
33
src/main/java/com/cubetiqs/demo/rest/PostController.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package com.cubetiqs.demo.rest;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.service.PostService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(path = {"/posts"})
|
||||||
|
public class PostController {
|
||||||
|
private final PostService postService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public PostController(PostService postService) {
|
||||||
|
this.postService = postService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<Object> getAllPosts(
|
||||||
|
Pageable pageable,
|
||||||
|
@RequestParam(value = "view", defaultValue = "list") String viewType,
|
||||||
|
@RequestParam(value = "q", defaultValue = "") String q
|
||||||
|
) {
|
||||||
|
if ("list".equalsIgnoreCase(viewType)) {
|
||||||
|
return ResponseEntity.ok(postService.findAllActives());
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(postService.findAll(pageable));
|
||||||
|
}
|
||||||
|
}
|
@ -1,34 +0,0 @@
|
|||||||
package com.cubetiqs.demo.rest;
|
|
||||||
|
|
||||||
import com.cubetiqs.demo.domain.Product;
|
|
||||||
import com.cubetiqs.demo.repository.ProductRepository;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping(path = "/products")
|
|
||||||
public class ProductController {
|
|
||||||
private final ProductRepository productRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public ProductController(ProductRepository productRepository) {
|
|
||||||
this.productRepository = productRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(method = {RequestMethod.GET})
|
|
||||||
public List<Product> getAllProducts() {
|
|
||||||
return productRepository.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
|
||||||
public Product getOneProduct(@PathVariable Long id) {
|
|
||||||
return productRepository.findById(id).orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping
|
|
||||||
public Product createProduct(@RequestBody Product item) {
|
|
||||||
return productRepository.save(item);
|
|
||||||
}
|
|
||||||
}
|
|
102
src/main/java/com/cubetiqs/demo/rest/UserController.java
Normal file
102
src/main/java/com/cubetiqs/demo/rest/UserController.java
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
package com.cubetiqs.demo.rest;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
|
import com.cubetiqs.demo.domain.view.UserView;
|
||||||
|
import com.cubetiqs.demo.repository.UserRepository;
|
||||||
|
import com.cubetiqs.demo.service.UserNotFoundException;
|
||||||
|
import com.cubetiqs.demo.service.UserService;
|
||||||
|
import com.cubetiqs.demo.util.StringUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(path = {"/users"})
|
||||||
|
public class UserController {
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public UserController(UserRepository userRepository, UserService userService) {
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
this.userService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<UserEntity> getAllUsers(Pageable pageable) {
|
||||||
|
return userService.findAllActives();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/searchByEmail")
|
||||||
|
public Page<UserView> searchByEmail(
|
||||||
|
Pageable pageable,
|
||||||
|
@RequestParam(value = "q", defaultValue = "") String q
|
||||||
|
) {
|
||||||
|
return userService.searchByEmailAsView(StringUtils.stringQuery(q), pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/allUsers")
|
||||||
|
public Page<UserEntity> fetchAllUsers(Pageable pageable) {
|
||||||
|
return userRepository.fetchAllUsers(Pageable.unpaged());
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/findByEmail/{email}")
|
||||||
|
public ResponseEntity<Object> getUserById(
|
||||||
|
@PathVariable String email
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
return ResponseEntity.ok(userService.findByEmail(email));
|
||||||
|
} catch (UserNotFoundException ex) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public UserEntity getUserById(
|
||||||
|
@PathVariable Long id
|
||||||
|
) {
|
||||||
|
return userRepository.findById(id).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public UserEntity createUser(
|
||||||
|
@RequestBody UserEntity user
|
||||||
|
) {
|
||||||
|
return userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public UserEntity updateUser(
|
||||||
|
@PathVariable Long id,
|
||||||
|
@RequestBody UserEntity user
|
||||||
|
) {
|
||||||
|
Optional<UserEntity> userEntityOptional = userRepository.findById(id);
|
||||||
|
if(userEntityOptional.isPresent()) {
|
||||||
|
// found
|
||||||
|
user.setId(id);
|
||||||
|
return userRepository.save(user);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ResponseEntity<Object> deleteUser(
|
||||||
|
@PathVariable Long id
|
||||||
|
) {
|
||||||
|
Optional<UserEntity> userEntityOptional = userRepository.findById(id);
|
||||||
|
if (userEntityOptional.isPresent()) {
|
||||||
|
userRepository.delete(userEntityOptional.get());
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).body("user deleted");
|
||||||
|
} else {
|
||||||
|
return ResponseEntity.badRequest().body("not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
18
src/main/java/com/cubetiqs/demo/service/BaseService.java
Normal file
18
src/main/java/com/cubetiqs/demo/service/BaseService.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.repository.BaseRepository;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.repository.NoRepositoryBean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@NoRepositoryBean
|
||||||
|
public interface BaseService<T, ID extends Serializable> {
|
||||||
|
BaseRepository<T, ID> getRepository();
|
||||||
|
Page<T> findAll(Pageable pageable, String q);
|
||||||
|
Page<T> findAll(Pageable pageable);
|
||||||
|
List<T> findAll();
|
||||||
|
List<T> findAllActives();
|
||||||
|
}
|
26
src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java
Normal file
26
src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.repository.NoRepositoryBean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@NoRepositoryBean
|
||||||
|
public abstract class BaseServiceImpl<T, ID extends Serializable> implements BaseService<T, ID> {
|
||||||
|
@Override
|
||||||
|
public List<T> findAll() {
|
||||||
|
return getRepository().findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<T> findAll(Pageable pageable) {
|
||||||
|
return getRepository().findAll(pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<T> findAllActives() {
|
||||||
|
return getRepository().findAllActives();
|
||||||
|
}
|
||||||
|
}
|
8
src/main/java/com/cubetiqs/demo/service/PostService.java
Normal file
8
src/main/java/com/cubetiqs/demo/service/PostService.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.PostEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public interface PostService extends BaseService<PostEntity, Long> {
|
||||||
|
}
|
36
src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java
Normal file
36
src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.PostEntity;
|
||||||
|
import com.cubetiqs.demo.repository.BaseRepository;
|
||||||
|
import com.cubetiqs.demo.repository.PostRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PostServiceImpl extends BaseServiceImpl<PostEntity, Long> implements PostService {
|
||||||
|
private final PostRepository postRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public PostServiceImpl(PostRepository postRepository) {
|
||||||
|
this.postRepository = postRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BaseRepository<PostEntity, Long> getRepository() {
|
||||||
|
return postRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<PostEntity> findAll(Pageable pageable, String q) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PostEntity> findAllActives() {
|
||||||
|
return postRepository.findAllActives();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
public class UserNotFoundException extends Exception {
|
||||||
|
public UserNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
14
src/main/java/com/cubetiqs/demo/service/UserService.java
Normal file
14
src/main/java/com/cubetiqs/demo/service/UserService.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
|
import com.cubetiqs.demo.domain.view.UserView;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public interface UserService extends BaseService<UserEntity, Long> {
|
||||||
|
UserEntity findByEmail(String email) throws UserNotFoundException;
|
||||||
|
Page<UserEntity> searchByEmail(String email, Pageable pageable);
|
||||||
|
Page<UserView> searchByEmailAsView(String email, Pageable pageable);
|
||||||
|
}
|
59
src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java
Normal file
59
src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
|
import com.cubetiqs.demo.domain.view.UserView;
|
||||||
|
import com.cubetiqs.demo.repository.BaseRepository;
|
||||||
|
import com.cubetiqs.demo.repository.UserRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserServiceImpl extends BaseServiceImpl<UserEntity, Long> implements UserService {
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public UserServiceImpl(UserRepository userRepository) {
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BaseRepository<UserEntity, Long> getRepository() {
|
||||||
|
return userRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<UserEntity> findAll(Pageable pageable, String q) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<UserEntity> findAllActives() {
|
||||||
|
return userRepository.findAllActives();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserEntity findByEmail(String email) throws UserNotFoundException {
|
||||||
|
Optional<UserEntity> opt = userRepository.fetchFirstByEmail(email);
|
||||||
|
if (opt.isPresent()) {
|
||||||
|
return opt.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new UserNotFoundException("user does not exists");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<UserEntity> searchByEmail(String email, Pageable pageable) {
|
||||||
|
return userRepository.searchByEmail(email, pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<UserView> searchByEmailAsView(String email, Pageable pageable) {
|
||||||
|
Page<UserEntity> users = userRepository.searchByEmail(email, pageable);
|
||||||
|
return users.map(s -> new UserView(s.getId(), s.getEmail()));
|
||||||
|
}
|
||||||
|
}
|
20
src/main/java/com/cubetiqs/demo/ui/HomeController.java
Normal file
20
src/main/java/com/cubetiqs/demo/ui/HomeController.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package com.cubetiqs.demo.ui;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class HomeController {
|
||||||
|
@GetMapping(value = {"/index", "", "/", "/index.php"})
|
||||||
|
public String index(Model model) {
|
||||||
|
model.addAttribute("myname", "Sambo");
|
||||||
|
return "index";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(value = {"/admin/users"})
|
||||||
|
public String user(Model model) {
|
||||||
|
model.addAttribute("myname", "Sambo");
|
||||||
|
return "admin/users/index";
|
||||||
|
}
|
||||||
|
}
|
8
src/main/java/com/cubetiqs/demo/util/StringUtils.java
Normal file
8
src/main/java/com/cubetiqs/demo/util/StringUtils.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package com.cubetiqs.demo.util;
|
||||||
|
|
||||||
|
public final class StringUtils {
|
||||||
|
public static String stringQuery(String q) {
|
||||||
|
return "%" + q.toLowerCase() + "%";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
12
src/main/resources/application-mysql.yml.example
Normal file
12
src/main/resources/application-mysql.yml.example
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
server:
|
||||||
|
port: 8081
|
||||||
|
|
||||||
|
spring:
|
||||||
|
jpa:
|
||||||
|
hibernate:
|
||||||
|
ddl-auto: update
|
||||||
|
datasource:
|
||||||
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
username: root
|
||||||
|
password: root
|
||||||
|
url: jdbc:mysql://192.168.0.150:3306/demo
|
12
src/main/resources/application-postgres.yml.example
Normal file
12
src/main/resources/application-postgres.yml.example
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
server:
|
||||||
|
port: 8081
|
||||||
|
|
||||||
|
spring:
|
||||||
|
jpa:
|
||||||
|
hibernate:
|
||||||
|
ddl-auto: update
|
||||||
|
datasource:
|
||||||
|
driver-class-name: org.postgresql.Driver
|
||||||
|
username: root
|
||||||
|
password: root
|
||||||
|
url: jdbc:postgresql://${POSTGRES_HOST:192.168.0.150}:5432/demo
|
@ -1,12 +1,3 @@
|
|||||||
server:
|
|
||||||
port: 8080
|
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
jpa:
|
profiles:
|
||||||
hibernate:
|
active: postgres
|
||||||
ddl-auto: update
|
|
||||||
datasource:
|
|
||||||
driver-class-name: org.postgresql.Driver
|
|
||||||
username: cubetiq
|
|
||||||
password: Root$
|
|
||||||
url: jdbc:postgresql://${POSTGRES_HOST:192.168.0.150}:5432/demo
|
|
1
src/main/resources/static/js/main.js
Normal file
1
src/main/resources/static/js/main.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
console.log("hello")
|
13
src/main/resources/templates/admin/users/index.html
Normal file
13
src/main/resources/templates/admin/users/index.html
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport"
|
||||||
|
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Users</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
13
src/main/resources/templates/index.html
Normal file
13
src/main/resources/templates/index.html
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport"
|
||||||
|
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
|
<title>Index</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1 th:text="${myname}"></h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user