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

Add files via upload #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions ProductManagement/DBRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.ProductManagement;


//@Repository
public class DBRepository implements ProductRepository{

// DBRepository가 왜 필요한가요?
// 이미 MapRepository가 DB역할을 해주는거 아닌가요?


}
46 changes: 46 additions & 0 deletions ProductManagement/MapRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.example.ProductManagement;

import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class MapRepository implements ProductRepository{

Map<Long, String> database = new HashMap<Long, String>();
Long id = 1L;

@Override
public String add(String val) {
database.put(id++, val);
return val;
}

@Override
public String search(Long productId) {
return database.get(productId);
}

@Override
public void delete(Long productId) {
database.remove(productId);
}

@Override
public void update(Long productId, String newName) {
database.replace(productId, newName);
}

// database의 HashMap 오브젝트를 한줄씩 문자열로 반환
@Override
public String getAllProducts() {
StringBuilder sb = new StringBuilder();

for (int i = 1; i < database.size(); i++) {
sb.append(String.format("%d : %s\n", i, database.get(i)));
}

return sb.toString();
}
}
42 changes: 42 additions & 0 deletions ProductManagement/ProductController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.ProductManagement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/boss")
public class ProductController {

@Autowired
ProductService productService;

@RequestMapping("/view-all-product")
public String getAllProducts() {
return productService.getAllProducts();
}

@RequestMapping("/add-product")
public String add(@RequestParam("productName") String name) {
return productService.add(name);
}

@RequestMapping("/search-product")
public String search(@RequestParam("productId") Long productId) {
return productService.search(productId);
}

@RequestMapping("/update-product")
public void update(
@RequestParam("productId") Long productId,
@RequestParam("newName") String newName
) {
productService.update(productId, newName);
}

@RequestMapping("/delete-product")
public void delete(@RequestParam("productId") Long productId) {
productService.delete(productId);
}
}
14 changes: 14 additions & 0 deletions ProductManagement/ProductRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.ProductManagement;

import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository {

abstract public String add(String productName);
abstract public String search(Long productId);
abstract public void delete(Long productId);
abstract public void update(Long productId, String newName);
abstract public String getAllProducts();

}
31 changes: 31 additions & 0 deletions ProductManagement/ProductService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.ProductManagement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductService {

@Autowired
private ProductRepository productRepository;

public String add(String productName) {
return productRepository.add(productName);
}

public String search(Long productId) {
return productRepository.search(productId);
}

public void update(Long productId, String newName) {
productRepository.update(productId, newName);
}

public void delete(Long productId) {
productRepository.delete(productId);
}

public String getAllProducts() {
return productRepository.getAllProducts();
}
}