๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐Ÿ’ป๐Ÿ’ญ๐ŸŽง๐ŸŒ

[์›น ๊ฐœ๋ฐœ ํ”„๋กœ์ ํŠธ] 5. ๋ชจ์ง‘๊ธ€ ์ˆ˜์ •/์‚ญ์ œ/ ๋ชฉ๋ก ์กฐํšŒ ๊ตฌํ˜„ ๋ณธ๋ฌธ

์›น ๊ฐœ๋ฐœ ํ”„๋กœ์ ํŠธ

[์›น ๊ฐœ๋ฐœ ํ”„๋กœ์ ํŠธ] 5. ๋ชจ์ง‘๊ธ€ ์ˆ˜์ •/์‚ญ์ œ/ ๋ชฉ๋ก ์กฐํšŒ ๊ตฌํ˜„

adorableco 2024. 1. 14. 12:57
๋ฐ˜์‘ํ˜•

์ˆ˜์ •๊ณผ ์‚ญ์ œ๋Š” ์˜ˆ์ƒ๋Œ€๋กœ ์‰ฌ์› ๋‹ค! ์ƒˆ๋กœ์šด ํด๋ž˜์Šค ์ƒ์„ฑ์ด ์—†์œผ๋ฏ€๋กœ ํŒจํ‚ค์ง€ ๊ณ„์ธต ๊ตฌ์กฐ๋Š” ๊ทธ๋Œ€๋กœ๋‹ˆ ์Šคํ‚ต!

 

 


1. ๋ชจ์ง‘๊ธ€ ์‚ญ์ œ

public String deleteById(Long id){
        try{
            postRepository.deleteById(id);
            return "๋ชจ์ง‘๊ธ€ ์‚ญ์ œ ์™„๋ฃŒ";
        }catch (Exception e){
            return "๋ชจ์ง‘๊ธ€ ์‚ญ์ œ ์‹คํŒจ " + e;
        }
    }

 

์•„ Jpa๋ผ๋Š”๊ฑฐ.. ์ •๋ง ๋‹ฌ๋‹ค
๋ชจ์ง‘๊ธ€ id๋ฅผ ์ „๋‹ฌ ์ธ์ž๋กœ ๋ฐ›์•„ JpaRepository ๋ฅผ ์ƒ์†๋ฐ›๋Š” PostRepository์˜ deleteById ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์„ ์‚ญ์ œํ•œ๋‹ค.

 

@DeleteMapping("/api/post/{id}")
    public ResponseEntity<String> deletePostById(@PathVariable Long id) {
        return ResponseEntity.ok().body(postService.deleteById(id));
    }

 

์‚ญ์ œ ์™„๋ฃŒ ์‹œ, ๋ฉ”์„œ๋“œ์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒฐ๊ณผ String์„ response body์— ๋‹ด์•„ ๋ณด๋‚ธ๋‹ค.

 


2. ๋ชจ์ง‘๊ธ€ ์ˆ˜์ •

@Transactional
    public String updatePost(Long id, AddPostRequest dto){
        Post post = postRepository.findById(id).get();
        Match match = matchRepository.findById(post.getMatch().getMatchId()).get();
        Tag tag = tagRepository.findById(post.getTag().getTagId()).get();

        post.setTitle(dto.getTitle());
        post.setContent(dto.getContent());
        match.setMatchDate(dto.getMatch().getMatchDate());
        match.setPlace(dto.getMatch().getPlace());
        match.setHeadCnt(dto.getMatch().getHeadCnt());
        tag.setAgeType(dto.getTag().getAgeType());
        tag.setGenderType(dto.getTag().getGenderType());
        tag.setLevelType(dto.getTag().getLevelType());

        return "๋ชจ์ง‘๊ธ€ ์ˆ˜์ • ์™„๋ฃŒ";

    }

 

@Transactional ์˜ ํŽธ๋ฆฌํ•จ์„ ์ฒ˜์Œ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์–ด์žˆ๋Š” ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์—”ํ‹ฐํ‹ฐ ์นผ๋Ÿผ์— ์—…๋ฐ์ดํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฐ˜์˜์ด ๋œ๋‹ค๊ณ  ํ•œ๋‹ค. ์œ„ ์ฝ”๋“œ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์—…๋ฐ์ดํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์€ ์นผ๋Ÿผ๋“ค๋„ ๋ชจ๋‘ ์—…๋ฐ์ดํŠธ๊ฐ€ ๋˜์ง€๋งŒ @DynamicUpdate ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์‹ค์ œ ์—…๋ฐ์ดํŠธ๊ฐ€ ์ผ์–ด๋‚œ ์นผ๋Ÿผ๋งŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฐ˜์˜๋œ๋‹ค๊ณ ํ•˜๋‹ˆ ์ถ”ํ›„์— ๋ณ€๊ฒฝ์„ ๊ณ ๋ คํ•ด๋ด์•ผ๊ฒ ๋‹ค. ์‚ฌ์‹ค ์ฝ”๋“œ์— ์‚ฌ์šฉ์€ ํ–ˆ์ง€๋งŒ Jpa ์˜ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ๋“ค์— ๋Œ€ํ•ด ์ž˜ ๋ชจ๋ฅด๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ๋”ฐ๋กœ ๊ณต๋ถ€๋ฅผ ํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์„ ํ–ˆ๋‹ค.

 

    @PutMapping("/api/post/{id}")
    public ResponseEntity<String> modifyPostById(@PathVariable Long id, @RequestBody AddPostRequest dto) {
        return ResponseEntity.ok().body(postService.updatePost(id,dto));
    }

 

์ฐธ๊ณ ๋กœ ์ˆ˜์ • api ์š”์ฒญ ์‹œ request dto๋Š” ๋ชจ์ง‘๊ธ€ ๋“ฑ๋ก request dto๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ๋ฉ”์„œ๋“œ์˜ String์„ ๋ฐ˜ํ™˜๋ฐ›์•„์„œ ์š”์ฒญ ๊ฒฐ๊ณผ๋ฅผ response body์— ๋‹ด์•„ ๋ณด๋‚ธ๋‹ค.


๊ทธ๋ฆฌ๊ณ  ๊ฐœ๋ฐœ ์ค‘์— ์ผ์–ด๋‚œ ํ•œ๊ฐ€์ง€ ์ด์Šˆ..

๋‹น์žฅ ๋ฐฑ์—”๋“œ ์„œ๋ฒ„๋ฅผ ํŒŒ๊ธฐ๋ณด๋‹ค๋Š” ๋ฐ”๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ด์•ผ ํ•  ์ƒํ™ฉ์ด๋ผ ํ”„๋ก ํŠธ ์ชฝ์—์„œ mariadb๋ฅผ ์ผœ๊ณ  ์Šคํ”„๋ง ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ฐ”๋กœ ์‹คํ–‰ํ•ด๋ณด๋ ค๊ณ  ํ•˜๋Š”๋ฐ ์•„๋ž˜์™€ ๊ฐ™์€ ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

ERROR 12568 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper   : (conn=74) Incorrect string value: '\xEC\xB4\x88\xEB\x93\xB1...' for column `springdb`.`matches`.`place` at row 1

 

์—ญ์‹œ ๋‚ด PC์—์„œ ์ž˜๋˜๋˜๊ฒŒ ๋‹ค๋ฅธ PC์—์„œ๋Š” ์ž‘๋™์ด ์•ˆ๋  ์ˆ˜ ์žˆ๊ตฌ๋‚˜.. ์ƒ๊ฐํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ํŒŒ์•…ํ•ด๋ณด๋‹ˆ MySql์— ํ•œ๊ธ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋ ค๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

 

๋ฌธ์ œ ํŒŒ์•… ์ „์— ํ˜น์‹œ๋‚˜ํ•ด์„œ request body์˜ ํ•„๋“œ๋ช…์„ name์—์„œ Name (๋Œ€๋ฌธ์ž ๋ณ€๊ฒฝ)์œผ๋กœ ๋ฐ”๊พธ๋‹ˆ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰์€ ๋๋‹ค. ๊ทธ๋ž˜๋„ ์ด๋ ‡๊ฒŒ ๋œ๋‹ค๊ณ  ํ•ด๋„ ๋ง์ด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์—.... ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด๋‹ˆ ์ธ์ฝ”๋”ฉ ์„ค์ • ์‚ฌํ•ญ์„ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด ๋œ๋‹ค๊ณ  ํ•œ๋‹ค.

 

ALTER TABLE (ํ…Œ์ด๋ธ”๋ช…) convert to charset utf8;

 

๋งฅ์—์„œ ๊ฐœ๋ฐœํ•˜๋˜๊ฑธ ์œˆ๋„์šฐ์—์„œ ์—ด์–ด์„œ ๊ทธ๋Ÿฐ๊ฑด๊ฐ€? ๋ชจ๋ฅด๊ฒ ๋‹ค


๋ชจ์ง‘๊ธ€ ๋ชฉ๋ก ์กฐํšŒ

๋ฏธ๋ฆฌ ๊ฐ„๋žตํ•˜๊ฒŒ ๋งŒ๋“ค์–ด๋‘ฌ์„œ, ๊ฑฐ๊ธฐ์„œ ํ•„ํ„ฐ๋ง์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์„ ๋ฐ›๋Š” ๊ฒƒ๋งŒ ๋” ์ถ”๊ฐ€ํ•˜์˜€๋‹ค.

public interface PostRepository extends JpaRepository<Post, Long> {
    List<Post> findByCategory(String category);
}

 

PostRepository ์— ์นดํ…Œ๊ณ ๋ฆฌ๋กœ post๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ๋ฉ”์„œ๋“œ๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. ์ด์ „๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ PostService ์—๋„ findByCategory ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  postRepository์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•ด๋‘๋ฉด ๋œ๋‹ค!

 

    @GetMapping("/api/posts")
    public ResponseEntity<List<PostResponse>> findPosts(@RequestParam("category") String category) {
        List<PostResponse> posts;
        if (category == null) {
            posts = postService.findAll()
                    .stream()
                    .map((Post post) -> new PostResponse(post))
                    .toList();
        } else {
            posts = postService.findByCategory(category)
                    .stream()
                    .map((Post post) -> new PostResponse(post))
                    .toList();
        }

        return ResponseEntity.ok()
                .body(posts);
    }

 

ํƒœ๊ทธ์˜ ๋‚˜์ด๋Œ€, ์„ฑ๋ณ„, ์ˆ˜์ค€ ๋ณ„๋กœ๋„ ํ•„ํ„ฐ๋ง์„ ํ•ด์•ผํ•˜์ง€๋งŒ ์ผ๋‹จ์€ ์นดํ…Œ๊ณ ๋ฆฌ๋งŒ ๊ตฌํ˜„ํ•ด๋ณธ๋‹ค. requestParam ์œผ๋กœ category ๊ฐ€ ๋“ค์–ด์˜ค๋ฉด findByCategory() ๋ฉ”์„œ๋“œ๋กœ ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋ชจ์ง‘๊ธ€๋งŒ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ณ , ์•„๋ฌด ๊ฒƒ๋„ ๋“ค์–ด์˜ค์ง€ ์•Š์•„ category๊ฐ€ null ์ด๋ผ๋ฉด ์ „์ฒด ๋ชจ์ง‘๊ธ€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก findAll() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•œ๋‹ค.

 

 


๋‹ค์Œ ํ•  ์ผ

  • ๊ฒฝ๊ธฐ ์ฐธ๊ฐ€ ์‹ ์ฒญ ๊ธฐ๋Šฅ ๊ตฌํ˜„

 

์•„์ขŒ์ขŒ

๋ฐ˜์‘ํ˜•