Skip to content

Commit

Permalink
Merge pull request #153 from kkn1125/kkn1125/be/mapper
Browse files Browse the repository at this point in the history
[BE][:hammer_and_wrench: FEAT]: ResponseEntity -> ObjectMapper 변경 + r…
  • Loading branch information
kkn1125 authored Aug 10, 2022
2 parents 04e16a7 + d5d1397 commit 6842cfb
Show file tree
Hide file tree
Showing 35 changed files with 417 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

public interface BillRepository extends MongoRepository<Bill, String> {
public interface BillRepository extends MongoRepository<Bill, String>, BillRepositoryCustom {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.narang.web.repository;

import com.narang.web.entity.Bill;

import java.util.List;

public interface BillRepositoryCustom {
public List<Bill> findByUid(String uid);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.narang.web.repository;

import com.narang.web.entity.Bill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

public class BillRepositoryCustomImpl implements BillRepositoryCustom {
private MongoTemplate billTemplate;

@Autowired
BillRepositoryCustomImpl(MongoTemplate billTemplate) {
this.billTemplate = billTemplate;
}

@Override
public List<Bill> findByUid(String uid) {
Criteria cr = new Criteria("uid").is(uid);
Query q = new Query(cr);
return billTemplate.find(q, Bill.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

public interface CartRepository extends MongoRepository<Cart, String> {
public interface CartRepository extends MongoRepository<Cart, String>, CartRepositoryCustom {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.narang.web.repository;

import com.narang.web.entity.Cart;

public interface CartRepositoryCustom {
public Cart update(Cart cart);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.narang.web.repository;

import com.narang.web.entity.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;

public class CartRepositoryCustomImpl implements CartRepositoryCustom {
private MongoTemplate cartTemplate;

@Autowired
CartRepositoryCustomImpl(MongoTemplate cartTemplate) {
this.cartTemplate = cartTemplate;
}

@Override
public Cart update(Cart cart) {
Cart foundCart = cartTemplate.findById(cart.getId(), Cart.class);
foundCart.replaceIfNotNull(cart);
return cartTemplate.save(foundCart, "cart");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
public interface CommentRepositoryCustom {
public List<Comment> findByDid(String did);

public Boolean update(Comment comment);
public Comment update(Comment comment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
import java.util.List;

public class CommentRepositoryCustomImpl implements CommentRepositoryCustom {
@Autowired
private MongoTemplate commentTemplate;

@Autowired
CommentRepositoryCustomImpl(MongoTemplate commentTemplate) {
this.commentTemplate = commentTemplate;
}

@Override
public List<Comment> findByDid(String did) {
Criteria cr = new Criteria("did").is(did);
Expand All @@ -21,10 +25,9 @@ public List<Comment> findByDid(String did) {
}

@Override
public Boolean update(Comment comment) {
public Comment update(Comment comment) {
Comment foundComment = commentTemplate.findById(comment.getId(), Comment.class);
foundComment.replaceIfNotNull(comment);
commentTemplate.save(foundComment, "comment");
return true;
return commentTemplate.save(foundComment, "comment");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class EmotionRepositoryCustomImpl implements EmotionRepositoryCustom {
this.emotionTemplate = emotionTemplate;
}


@Override
public Optional<Emotion> findByUid(String uid) {
Criteria cr = new Criteria("uid").is(uid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
import java.util.Optional;

public class FaceImageRepositoryCustomImpl implements FaceImageRepositoryCustom {
private static final String COLLECTION = "faceImage";
private static final Class<FaceImage> CLASS = FaceImage.class;

private MongoTemplate faceTemplate;

@Autowired
Expand Down Expand Up @@ -56,7 +53,7 @@ public Boolean deleteByTwo(String uid, List<String> ids) {
q.addCriteria(orCriteria.orOperator(orExp.toArray(new Criteria[orExp.size()])));

System.out.println(q);
faceTemplate.remove(q, COLLECTION);
faceTemplate.remove(q, "faceImage");
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ public Boolean deleteByDid(String did, String uid) {
likeTemplate.remove(q, "like");
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

public interface ProductRepository extends MongoRepository<Product, String> {
public interface ProductRepository extends MongoRepository<Product, String>, ProductRepositoryCustom {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.narang.web.repository;

import com.narang.web.entity.Product;

public interface ProductRepositoryCustom {
public Product update(Product product);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.narang.web.repository;

import com.narang.web.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;

public class ProductRepositoryCustomImpl implements ProductRepositoryCustom {
private MongoTemplate productTemplate;

@Autowired
ProductRepositoryCustomImpl(MongoTemplate productTemplate) {
this.productTemplate = productTemplate;
}

@Override
public Product update(Product product) {
Product foundProduct = productTemplate.findById(product.getId(), Product.class);
foundProduct.replaceIfNotNull(product);
return productTemplate.save(foundProduct, "products");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface UserRepositoryCustom {

public Optional<User> findByEmail(String email);

public Boolean update(User user);
public User update(User user);

public User removeProfileImageById(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@


public class UserRepositoryCustomImpl implements UserRepositoryCustom {
@Autowired
MongoTemplate userTemplate;

@Autowired
UserRepositoryCustomImpl (MongoTemplate userTemplate){
this.userTemplate = userTemplate;
}

@Override
public Optional<User> findByNickName(String nickName) {
Criteria cr = new Criteria("nickName");
Expand Down Expand Up @@ -51,11 +55,10 @@ public Optional<User> findByEmail(String email) {
return Optional.of(userTemplate.findOne(q, User.class));
}

public Boolean update(User user) {
public User update(User user) {
User foundUser = userTemplate.findById(user.getId(), User.class);
foundUser.replaceIfNotNull(user);
userTemplate.save(foundUser, "user");
return true;
return userTemplate.save(foundUser, "user");
}

@Override
Expand Down
61 changes: 30 additions & 31 deletions src/main/java/com/narang/web/restController/BillRestController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.narang.web.restController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.narang.web.entity.Bill;
import com.narang.web.repository.BillRepository;
import com.narang.web.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -13,51 +17,46 @@
@RestController
@RequestMapping("/api")
public class BillRestController {
private BillService billService;

@Autowired
private BillRepository billRepository;
BillRestController(BillService billService) {
this.billService = billService;
}

private String mapper(Object object) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
// properties 설정도 있지만 java 구문으로 해결하는 방법 채택 (보기 쉽게 하기 위함)
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return mapper.writeValueAsString(object);
}

@GetMapping("/bills")
public ResponseEntity<?> getAllBills() {
List<Bill> bills = billRepository.findAll();
if (bills.size() > 0) {
return new ResponseEntity<List<Bill>>(bills, HttpStatus.OK);
} else {
return new ResponseEntity<>("조회할 영수증이 없습니다.", HttpStatus.NOT_FOUND);
}
public String findAll() throws JsonProcessingException {
return mapper(billService.findAll());
}

@GetMapping("/bill/{id}")
public String findById(@PathVariable("id") String id) throws JsonProcessingException {
return mapper(billService.findById(id));
}

@GetMapping("/bills/{uid}")
public ResponseEntity<?> getSingleBill(@PathVariable("uid") String id) {
Optional<Bill> billOptional = billRepository.findById(id);
if (billOptional.isPresent()) {
return new ResponseEntity<>(billOptional.get(), HttpStatus.OK);
} else {
return new ResponseEntity<>("uid가 [" + id + "]인 영수증이 존재하지 않습니다.", HttpStatus.NOT_FOUND);
}
@GetMapping("/bill/uid/{uid}")
public String findByUid(@PathVariable("uid") String uid) throws JsonProcessingException {
return mapper(billService.findByUid(uid));
}

@PostMapping("/bill")
public ResponseEntity<?> createBill(@RequestBody Bill bill) {
try {
billRepository.save(bill);
return new ResponseEntity<Bill>(bill, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
public String insert(Bill bill) {
return billService.insert(bill);
}

/**
* 개발자용 삭제 로직
* (덧, 영수증은 수정할 수 없으므로 수정 로직은 없음)
*/
@DeleteMapping("/bill/{bid}")
public ResponseEntity<?> deleteBill(@PathVariable("bid") String id) {
try{
billRepository.deleteById(id);
return new ResponseEntity<>("bid [" + id + "]인 영수증이 성공적으로 삭제되었습니다.", HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
@DeleteMapping("/bill/{id}")
public Boolean deleteById(@PathVariable("id") String id) {
return billService.deleteById(id);
}
}
Loading

0 comments on commit 6842cfb

Please sign in to comment.