Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

부산대 BE_이풍헌 2주차 과제(step2) #298

Open
wants to merge 12 commits into
base: canyos
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
schema.sql data.sql 자동실행
canyos committed Jul 3, 2024
commit 66292a01945615b9cae6d00a2a723c4eb7204246
14 changes: 2 additions & 12 deletions src/main/java/gift/Application.java
Original file line number Diff line number Diff line change
@@ -6,20 +6,10 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;

@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
JdbcTemplate jdbcTemplate;

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... strings) throws Exception {
jdbcTemplate.execute("DROP TABLE product IF EXISTS");
jdbcTemplate.execute("CREATE TABLE product(id SERIAL, name VARCHAR(255), price int, imageUrl varchar(255))");
jdbcTemplate.execute("Drop table option IF exists");
jdbcTemplate.execute("CREAte table option (id int, option varchar(255))");
}
}
39 changes: 39 additions & 0 deletions src/main/java/gift/controller/ProductAdminController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gift.controller;

import gift.entity.Product;
import gift.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class ProductAdminController {
@Autowired
private ProductService productService;

@GetMapping("/admin/products")
public ModelAndView adminProducts(Model model){
List<Product> products = productService.getAllProducts();
model.addAttribute("products", products);
return new ModelAndView("admin/products");
}

@GetMapping("/admin/add")
public ModelAndView adminProductsAdd(Model model){
return new ModelAndView("admin/add");
}

@GetMapping("/admin/modify")
public ModelAndView adminProductsModify(Model model){
return new ModelAndView("admin/modify");
}

@GetMapping("/admin/delete")
public ModelAndView adminProductsDelete(Model model){
return new ModelAndView("admin/delete");
}
}
37 changes: 2 additions & 35 deletions src/main/java/gift/controller/ProductController.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,23 @@
package gift.controller;

import gift.entity.Product;
import gift.exception.Exception404;
import gift.service.ProductService;
import org.hibernate.validator.internal.constraintvalidators.bv.NotBlankValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import Exception.*;

import java.util.List;


@RestController
public class ProductController {
//private Map<Long, Product> products = new HashMap<>();
//private static Long count = 1L;

@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private ProductService productService;

@GetMapping("/admin/products")
public ModelAndView adminProducts(Model model){
List<Product> products = productService.getAllProducts();
model.addAttribute("products", products);
return new ModelAndView("admin/products");
}

@GetMapping("/admin/add")
public ModelAndView adminProductsAdd(Model model){
return new ModelAndView("admin/add");
}

@GetMapping("/admin/modify")
public ModelAndView adminProductsModify(Model model){
return new ModelAndView("admin/modify");
}

@GetMapping("/admin/delete")
public ModelAndView adminProductsDelete(Model model){
return new ModelAndView("admin/delete");
}

@GetMapping("/api/products")
public String getProducts() {
String jsonProducts = productService.getJsonAllProducts();
@@ -75,13 +48,7 @@ public void modifyProduct(@RequestParam("id") int id, @RequestParam("name") Stri
@GetMapping("/api/product/{id}")
public String getProduct(@PathVariable int id){
String product = productService.getProductByID(id);

return product;
}

@GetMapping("/error/404")
public void Error404(){
throw new Exception404("Not Found");
}
}

20 changes: 20 additions & 0 deletions src/main/java/gift/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gift.controller;

import gift.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class UserController {
@Autowired
UserService userService;

@GetMapping("/api/signin")
public void SignIn(String user_id, String password) {
userService.signin(user_id,password);
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gift.DTO;
package gift.dto;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import jakarta.validation.constraints.NotBlank;
25 changes: 25 additions & 0 deletions src/main/java/gift/entity/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gift.entity;

public class User {
private static int id = 1;
private String userId;
private String userPw;

public int getId() {
return id;
}

public String getUserId() {
return userId;
}

public String getUserPw() {
return userPw;
}

public User(String userId, String userPw) {
this.id = id++;
this.userId = userId;
this.userPw = userPw;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Exception;
package gift.exception;

public class Exception400 extends RuntimeException{
public Exception400(String message){
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Exception;
package gift.exception;

public class Exception401 extends RuntimeException {
public Exception401(String msg) {
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Exception;
package gift.exception;

public class Exception404 extends RuntimeException{
public Exception404(String msg){
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Exception;
package gift.exception;

public class Exception500 extends RuntimeException{
public Exception500(String message) {
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package Exception;
package gift.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


2 changes: 1 addition & 1 deletion src/main/java/gift/repository/ProductRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gift.repository;

import gift.DTO.SaveProductDTO;
import gift.dto.SaveProductDTO;
import gift.entity.Option;
import gift.entity.Product;
import jakarta.validation.Valid;
23 changes: 23 additions & 0 deletions src/main/java/gift/repository/UserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gift.repository;

import gift.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepository {
@Autowired
JdbcTemplate jdbcTemplate;

public boolean isExistAccount(String userId) {
String sql = "select count(*) from account where user_id=?";
int count = jdbcTemplate.queryForObject(sql, Integer.class, userId);
return count > 0;
}

public void saveUser(User user) {
String sql = "insert into User(id, user_id, user_pw) values(?,?,?)";
jdbcTemplate.update(sql,user.getId(),user.getUserId(),user.getUserPw());
}
}
6 changes: 4 additions & 2 deletions src/main/java/gift/service/ProductService.java
Original file line number Diff line number Diff line change
@@ -2,15 +2,17 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import gift.DTO.SaveProductDTO;
import gift.dto.SaveProductDTO;
import gift.entity.Option;
import gift.entity.Product;
import gift.exception.Exception400;
import gift.exception.Exception401;
import gift.exception.Exception404;
import gift.repository.ProductRepository;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import Exception.*;

import java.util.Arrays;
import java.util.List;
19 changes: 19 additions & 0 deletions src/main/java/gift/service/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package gift.service;

import gift.entity.User;
import gift.exception.*;
import gift.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
@Autowired
UserRepository userRepository;

public void signin(String userId, String password) {
if(userRepository.isExistAccount(userId))
throw new Exception404("이미 존재하는 계정");
userRepository.saveUser(new User(userId, password));
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
spring.application.name=spring-gift
# h2-console ??? ??
spring.h2.console.enabled=true
# db url
spring.datasource.url=jdbc:h2:mem:test
10 changes: 10 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
spring:
h2:
console:
enabled: true
# 7 서버 실행시에 data.sql 실행
sql:
init:
schema-locations: classpath:db/schema.sql
data-locations:
- classpath:db/data.sql
8 changes: 8 additions & 0 deletions src/main/resources/db/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
insert into product(id, name , price , imageUrl) values(1,'1',1,'1');
insert into option(id,option) values(1,'1');

insert into product(id, name , price , imageUrl) values(2,'2',2,'2');
insert into option(id,option) values(2,'2');

insert into user_tb(id,user_id,user_pw) values(1,'asdf','asdf');
insert into user_tb(id,user_id,user_pw) values(2,'qwer','qwer');
6 changes: 6 additions & 0 deletions src/main/resources/db/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DROP TABLE product IF EXISTS;
CREATE TABLE product(id SERIAL, name VARCHAR(255), price int, imageUrl varchar(255));
DROP TABLE option IF exists;
CREATE TABLE option (id int, option varchar(255));
DROP TABLE user_tb IF exists;
CREATE TABLE user_tb(id SERIAL, user_id varchar(20), user_pw varchar(20));